Advertisement
Guest User

Untitled

a guest
May 10th, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. package nl.nielsha.ebay.mysql;
  2.  
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.ArrayList;
  9.  
  10. public class Connection {
  11. // Proporties
  12. private String hostname;
  13. private String port;
  14. private String username;
  15. private String password;
  16. private String database;
  17.  
  18. private java.sql.Connection connection;
  19.  
  20. // Constructors
  21. public Connection()
  22. {
  23. this.hostname = "localhost";
  24. this.port = "3306";
  25. this.username = "";
  26. this.password = "";
  27. }
  28.  
  29. public Connection(String username, String password, String database)
  30. {
  31. this.hostname = "localhost";
  32. this.port = "3306";
  33. this.username = username;
  34. this.password = password;
  35. this.database = database;
  36. }
  37.  
  38. public Connection(String hostname, String username, String password, String database)
  39. {
  40. this.hostname = hostname;
  41. this.port = "3306";
  42. this.username = username;
  43. this.password = password;
  44. this.database = database;
  45. }
  46.  
  47. public Connection(String hostname, String port, String username, String password, String database)
  48. {
  49. this.hostname = hostname;
  50. this.port = port;
  51. this.username = username;
  52. this.password = password;
  53. this.database = database;
  54. }
  55.  
  56. // Functions
  57. public void connect()
  58. {
  59. String url = "jdbc:mysql://"+this.hostname+":"+this.port+"/"+this.database;
  60. try
  61. {
  62. this.connection = DriverManager.getConnection(url, this.username, this.password);
  63. } catch(SQLException ex)
  64. {
  65. this.connection = null;
  66. }
  67. }
  68.  
  69. public void disconnect()
  70. {
  71. try {
  72. this.connection.close();
  73. } catch (SQLException e) {
  74. this.connection = null;
  75. }
  76. this.connection = null;
  77. }
  78.  
  79. public ResultSet preparedStatement(String query, ArrayList<Object> values) throws SQLException
  80. {
  81. if(!this.isConnected())
  82. this.connect();
  83.  
  84. PreparedStatement ps = this.connection.prepareStatement(query);
  85. for(int i = 1; i <= values.size(); i++)
  86. {
  87. ps.setObject(i, values.get(i-1));
  88. }
  89. ps.execute();
  90.  
  91. return ps.getResultSet();
  92. }
  93.  
  94. public ResultSet query(String query) throws SQLException
  95. {
  96. if(!this.isConnected())
  97. this.connect();
  98.  
  99. Statement sm = this.connection.createStatement();
  100.  
  101. return sm.executeQuery(query);
  102. }
  103.  
  104. public int updateQuery(String query) throws SQLException
  105. {
  106. if(this.isConnected())
  107. this.connect();
  108.  
  109. Statement sm = this.connection.createStatement();
  110.  
  111. return sm.executeUpdate(query);
  112. }
  113.  
  114. // Getters and setters
  115. public String getHostname()
  116. {
  117. return this.hostname;
  118. }
  119.  
  120. public void setHostname(String hostname)
  121. {
  122. this.hostname = hostname;
  123. }
  124.  
  125. public String getPort()
  126. {
  127. return this.port;
  128. }
  129.  
  130. public void setPort(String port)
  131. {
  132. this.port = port;
  133. }
  134.  
  135. public String getUsername()
  136. {
  137. return this.username;
  138. }
  139.  
  140. public void setUsername(String username)
  141. {
  142. this.username = username;
  143. }
  144.  
  145. public String getPassword()
  146. {
  147. return this.password;
  148. }
  149.  
  150. public void setPassword(String password)
  151. {
  152. this.password = password;
  153. }
  154.  
  155. public String getDatabase()
  156. {
  157. return this.database;
  158. }
  159.  
  160. public void setDatabase(String database)
  161. {
  162. this.database = database;
  163. }
  164.  
  165. public java.sql.Connection getConnection()
  166. {
  167. return this.connection;
  168. }
  169.  
  170. public boolean isConnected()
  171. {
  172. try {
  173. return this.connection != null && !this.connection.isClosed();
  174. } catch (SQLException e) {
  175. return false;
  176. }
  177. }
  178.  
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement