Guest User

Untitled

a guest
Oct 31st, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. import java.sql.*;
  2. import com.mysql.jdbc.Driver;
  3.  
  4. public class DBConnector {
  5.  
  6. Connection conn = null;
  7.  
  8. public DBConnector(String DBName, String username, String password) {
  9.  
  10. try {
  11. String url = "jdbc:mysql://localhost/" + DBName;
  12. Class.forName("com.mysql.jdbc.Driver").newInstance();
  13. conn = DriverManager.getConnection(url, username, password);
  14. System.out.println("Database connection established");
  15. } catch (Exception e) {
  16. e.printStackTrace();
  17. }
  18. }
  19.  
  20. public DBConnector(String host, String DBName, String username, String password) {
  21.  
  22. try {
  23. String url = "jdbc:mysql://" + host + "/";
  24. Class.forName("com.mysql.jdbc.Driver").newInstance();
  25. conn = DriverManager.getConnection(url, username, password);
  26. System.out.println("Database connection established");
  27. } catch (Exception e) {
  28. e.printStackTrace();
  29. }
  30. }
  31.  
  32. public void update(String query) throws SQLException {
  33. Statement s;
  34.  
  35. s = conn.createStatement();
  36. s.executeUpdate(query);
  37. // s.e
  38. s.close();
  39. }
  40.  
  41. public void executed(String query) throws SQLException {
  42. try {
  43. System.out.println("***");
  44.  
  45. Statement stmt = conn.createStatement();
  46. int updateCount = stmt.executeUpdate(query);
  47. System.out.println(updateCount + " rows inserted.");
  48.  
  49. stmt.close();
  50. // updateCount contains the number of updated rows
  51. } catch (SQLException e) {
  52. }
  53. }
  54.  
  55. public ResultSet query(String query) {
  56. try {
  57. Statement s = conn.createStatement();
  58. s.executeQuery(query);
  59. ResultSet rs = s.getResultSet();
  60. s.close();
  61. return rs;
  62. } catch (Exception k) {
  63. k.printStackTrace();
  64. return null;
  65. }
  66. }
  67.  
  68. public void closeConnection() {
  69. try {
  70. conn.close();
  71. } catch (SQLException e) {
  72. e.printStackTrace();
  73. }
  74. }
  75. }
Add Comment
Please, Sign In to add comment