Folks,
Below is the Example code for JDBC Connection Using postgresql Driver.
import java.sql.*;
public class JdbcPostgreConnection {
public static void main( String args[]) throws SQLException {
Connection con = null ;
try {
// Load the Driver class.
Class.forName("org.postgresql.Driver"); //Step 1
// If you are using any other database then load the right driver here.
//Create the connection using the static getConnection method
con = DriverManager.getConnection ("jdbc:postgresql://localhost:5432/dbName","username","password");//Step 2
//Create a Statement class to execute the SQL statement
Statement stmt = con.createStatement();//Step 3
//Execute the SQL statement and get the results in a Resultset
ResultSet rs = stmt.executeQuery("select * from dbName.user");//Step 4
// Iterate through the ResultSet, displaying two values
// for each row using the getString method
while (rs.next())
System.out.println("Name= " + rs.getString("user_name") + " Date= " + rs.getString("user_password"));
}
catch (SQLException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
// Close the connection
con.close();//Step 5
}
}
}
Below is the Example code for JDBC Connection Using postgresql Driver.
import java.sql.*;
public class JdbcPostgreConnection {
public static void main( String args[]) throws SQLException {
Connection con = null ;
try {
// Load the Driver class.
Class.forName("org.postgresql.Driver"); //Step 1
// If you are using any other database then load the right driver here.
//Create the connection using the static getConnection method
con = DriverManager.getConnection ("jdbc:postgresql://localhost:5432/dbName","username","password");//Step 2
//Create a Statement class to execute the SQL statement
Statement stmt = con.createStatement();//Step 3
//Execute the SQL statement and get the results in a Resultset
ResultSet rs = stmt.executeQuery("select * from dbName.user");//Step 4
// Iterate through the ResultSet, displaying two values
// for each row using the getString method
while (rs.next())
System.out.println("Name= " + rs.getString("user_name") + " Date= " + rs.getString("user_password"));
}
catch (SQLException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
// Close the connection
con.close();//Step 5
}
}
}
No comments:
Post a Comment