Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DB_Connection {
- //Declare a Connection
- private static Connection con = null;
- // The url
- static String url = "jdbc:postgresql://localhost:5432/my awsome database by nichlas";
- // The username
- static String user = "postgres";
- // The password
- static String psswrd = "Password2961";
- // declare the Statement
- static Statement stmt = null;
- // declare the ResultSet
- static ResultSet rs = null;
- public static Connection connect(String user, String password) throws SQLException {
- System.out.println("--PostgreSQL JDBC connection test--");
- try {
- // Locate postgres JDBCdriver
- Class.forName("org.postgresql.Driver");
- } catch (ClassNotFoundException ex) {
- // Catch exeption
- System.out.println("JDBC is missing.");
- ex.printStackTrace();
- }
- System.out.println("PostgreSQL JDBC driver is registered");
- try {
- // connect to DB = jdbc:postgresql://connection type:port#/DB name, password
- con = DriverManager.getConnection(url, user, password);
- String query = "select * from person";
- stmt = con.createStatement();
- rs = stmt.executeQuery(query);
- }catch (SQLException ex){
- // Catch exception
- ex.printStackTrace();
- }
- return con;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement