Advertisement
WentaoHu

DBUtil

Dec 5th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. package OR3.util;
  2.  
  3. import com.sun.rowset.CachedRowSetImpl;
  4.  
  5. import java.sql.*;
  6.  
  7. public class DBUtil {
  8. // Declare JDBC Driver
  9. private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  10.  
  11. // Connection
  12. private static Connection conn = null;
  13.  
  14. // Connect to DB
  15. public static void dbConnect() throws SQLException, ClassNotFoundException {
  16. // Setting Mysql JDBC Driver
  17. try {
  18. Class.forName(JDBC_DRIVER);
  19. } catch (ClassNotFoundException e) {
  20. System.out.println("Where is your Mysql JDBC Driver?\n");
  21. e.printStackTrace();
  22. throw e;
  23. }
  24.  
  25. System.out.println("Mysql JDBC Driver Registered!");
  26.  
  27. // Establish the Mysql Connection
  28. try {
  29. conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/OR3","root","1017");
  30. } catch (SQLException e) {
  31. System.out.println("Connection Failed! Check output console" + e);
  32. e.printStackTrace();
  33. throw e;
  34. }
  35. }
  36.  
  37. // Close Connection
  38. public static void dbDisconnect() throws SQLException {
  39. try {
  40. if (conn != null && !conn.isClosed()) {
  41. conn.close();
  42. }
  43. } catch (Exception e){
  44. throw e;
  45. }
  46. }
  47.  
  48. // DB Execute Query Operation
  49. public static ResultSet dbExecuteQuery(String queryStmt) throws SQLException, ClassNotFoundException {
  50. //Declare statement, resultSet and CachedResultSet as null
  51. Statement stmt = null;
  52. ResultSet resultSet = null;
  53. CachedRowSetImpl crs = null;
  54. try {
  55. //Connect to DB
  56. dbConnect();
  57. System.out.println("Select statement: " + queryStmt + "\n");
  58.  
  59. //Create statement
  60. stmt = conn.createStatement();
  61.  
  62. //Execute select (query) operation
  63. resultSet = stmt.executeQuery(queryStmt);
  64.  
  65. // CachedRowSet Implementation
  66. // In order to prevent "java.sql.SQLRecoverableException: Closed Connection: next" error
  67. // We are using CachedRowSet
  68. crs = new CachedRowSetImpl();
  69. crs.populate(resultSet);
  70. } catch (SQLException e) {
  71. System.out.println("Problem occurred at executeQuery operation : " + e);
  72. throw e;
  73. } finally {
  74. if (resultSet != null) {
  75. //Close resultSet
  76. resultSet.close();
  77. }
  78. if (stmt != null) {
  79. //Close Statement
  80. stmt.close();
  81. }
  82. //Close connection
  83. dbDisconnect();
  84. }
  85. //Return CachedRowSet
  86. return crs;
  87. }
  88.  
  89. // DB Execute Update (For Update/Insert/Delete) Operation
  90. public static void dbExecuteUpdate(String sqlStmt) throws SQLException, ClassNotFoundException {
  91. //Declare statement as null
  92. Statement stmt = null;
  93. try {
  94. //Connect to DB
  95. dbConnect();
  96. //Create Statement
  97. stmt = conn.createStatement();
  98. //Run executeUpdate operation with given sql statement
  99. stmt.executeUpdate(sqlStmt);
  100. } catch (SQLException e) {
  101. System.out.println("Problem occurred at executeUpdate operation : " + e);
  102. throw e;
  103. } finally {
  104. if (stmt != null) {
  105. // Close statement
  106. stmt.close();
  107. }
  108. // Close connection
  109. dbDisconnect();
  110. }
  111. }
  112.  
  113.  
  114.  
  115. public static boolean isLogin(String userLogin, String pwLogin) throws SQLException {
  116. PreparedStatement ps = null;
  117. ResultSet rs = null;
  118. String query = "SELECT * FROM accounts WHERE user = ? and password = ?";
  119. try {
  120. ps = conn.prepareStatement(query);
  121. ps.setString(1, userLogin);
  122. ps.setString(2, pwLogin);
  123.  
  124. rs = ps.executeQuery();
  125. if(rs.next()) {
  126. return true;
  127. }
  128. else {
  129. return false;
  130. }
  131. } catch (Exception e) {
  132. throw e;
  133. }
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement