Advertisement
Guest User

Untitled

a guest
Nov 16th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. public class DB_Connection {
  2.  
  3. //Declare a Connection
  4. private static Connection con = null;
  5. // The url
  6. static String url = "jdbc:postgresql://localhost:5432/my awsome database by nichlas";
  7. // The username
  8. static String user = "postgres";
  9. // The password
  10. static String psswrd = "Password2961";
  11. // declare the Statement
  12. static Statement stmt = null;
  13. // declare the ResultSet
  14. static ResultSet rs = null;
  15.  
  16.  
  17. public static Connection connect(String user, String password) throws SQLException {
  18. System.out.println("--PostgreSQL JDBC connection test--");
  19.  
  20. try {
  21. // Locate postgres JDBCdriver
  22. Class.forName("org.postgresql.Driver");
  23. } catch (ClassNotFoundException ex) {
  24. // Catch exeption
  25. System.out.println("JDBC is missing.");
  26. ex.printStackTrace();
  27.  
  28. }
  29. System.out.println("PostgreSQL JDBC driver is registered");
  30.  
  31. try {
  32. // connect to DB = jdbc:postgresql://connection type:port#/DB name, password
  33. con = DriverManager.getConnection(url, user, password);
  34. String query = "select * from person";
  35. stmt = con.createStatement();
  36. rs = stmt.executeQuery(query);
  37.  
  38.  
  39.  
  40. }catch (SQLException ex){
  41. // Catch exception
  42. ex.printStackTrace();
  43.  
  44. }
  45.  
  46. return con;
  47. }
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement