Advertisement
Guest User

Untitled

a guest
Oct 25th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package databasefunz;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. import java.sql.Statement;
  13.  
  14. /**
  15. *
  16. * @author Simon_
  17. */
  18. public class DB_Connection {
  19.  
  20. // Declare a Connection
  21. private static Connection con = null;
  22. // The url
  23. private static final String url = "jdbc:postgresql://localhost:5432/SQL_1";
  24. // The username
  25. private static final String user = "postgres";
  26. // driver
  27. private static final String DRIVER = "org.postgresql.Driver";
  28. // The password
  29. private static final String psswrd = "root";
  30. /*
  31. // declaring the Statement
  32. Statement stmt = null;
  33. // declaring the ResultSet
  34. ResultSet rs = null;
  35. */
  36. public static Connection connect() {
  37. System.out.println("--PostgreSQÆ connection test--");
  38.  
  39. try {
  40. // Locate postgres JDBC driver
  41. Class.forName(DRIVER);
  42. } catch (ClassNotFoundException ex) {
  43. // Catch exception
  44. ex.printStackTrace();
  45. System.out.println("JDBC is missing");
  46. }
  47. System.out.println("PostgreSQL JDBC driver is registerd");
  48.  
  49. try {
  50. con = DriverManager.getConnection(url, user, psswrd);
  51. } catch (SQLException ex) {
  52. ex.printStackTrace();
  53. }
  54. if (con != null) {
  55. System.out.println("connection successful");
  56. } else {
  57. System.out.println("connection un-succesful");
  58. }
  59. return con;
  60. }
  61.  
  62. public void closeConnection() {
  63. if (con != null) {
  64. try {
  65. con.close();
  66. } catch (SQLException ex) {
  67. ex.printStackTrace();
  68. }
  69. System.out.println("The connection is now closed");
  70. }
  71. }
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement