Advertisement
Guest User

Untitled

a guest
Feb 13th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1.  
  2. import java.sql.*;
  3.  
  4. public class DbResolver {
  5.  
  6. private Connection connection = null;
  7.  
  8. public void connect(){
  9. System.out.println("-------- PostgreSQL "
  10. + "JDBC Connection Testing ------------");
  11.  
  12. try {
  13.  
  14. Class.forName("org.postgresql.Driver");
  15.  
  16. } catch (ClassNotFoundException e) {
  17.  
  18. System.out.println("Where is your PostgreSQL JDBC Driver? "
  19. + "Include in your library path!");
  20. e.printStackTrace();
  21. return;
  22.  
  23. }
  24.  
  25. System.out.println("PostgreSQL JDBC Driver Registered!");
  26.  
  27. connection = null;
  28.  
  29. try {
  30.  
  31. connection = DriverManager.getConnection(
  32. "jdbc:postgresql://127.0.0.1:5432/eventapp", "event-app-user",
  33. "123456");
  34.  
  35. } catch (SQLException e) {
  36.  
  37. System.out.println("Connection Failed! Check output console");
  38. e.printStackTrace();
  39. return;
  40.  
  41. }
  42.  
  43. if (connection != null) {
  44. System.out.println("You made it, take control your database now!");
  45. } else {
  46. System.out.println("Failed to make connection!");
  47. }
  48. }
  49.  
  50. public boolean select(String query) {
  51. Statement stmt = null;
  52. boolean succesfullLogin = false;
  53. try {
  54. stmt = connection.createStatement();
  55. ResultSet rs = stmt.executeQuery(query);
  56. succesfullLogin = rs.next();
  57. } catch (SQLException e) {
  58. System.err.println("Sql error " + e.getMessage());
  59. }
  60. finally {
  61. try {
  62. if (stmt != null) {
  63. stmt.close();
  64. }
  65. } catch (SQLException e) {
  66. System.err.println("problem with connection closing " + e.getMessage());
  67. }
  68. }
  69. return succesfullLogin;
  70. }
  71.  
  72. public void insert(String query) {
  73. Statement stmt = null;
  74. try {
  75. stmt = connection.createStatement();
  76. stmt.executeQuery(query);
  77. } catch (SQLException e) {
  78. System.err.println("Sql error " + e.getMessage());
  79. }
  80. finally {
  81. try {
  82. if (stmt != null) {
  83. stmt.close();
  84. }
  85. } catch (SQLException e) {
  86. System.err.println("problem with connection closing " + e.getMessage());
  87. }
  88. }
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement