Advertisement
Guest User

Untitled

a guest
Oct 13th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. package me.oddlyoko.lom.mysql;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8.  
  9. /**
  10. * This class allow you to interact with MySQL<br />
  11. *
  12. * @author 0ddlyoko<br />
  13. * <br />
  14. * Created: 12 oct. 2016 (19:46:01);<br />
  15. * Last edit: 12 oct. 2016 (19:46:01);<br />
  16. */
  17. public class MySQL {
  18. private String host = ""; // Host name
  19. private String dbname = ""; // Database name
  20. private String login = ""; // Login
  21. private String passwd = ""; // Password
  22. private int port = 3306; // Port (default is 3306)
  23. private String url; // Url
  24.  
  25. private Connection connection = null; // Connection class
  26. private Statement statement = null; // Statement class
  27. private ResultSet resultat = null; // ResultSet class
  28.  
  29. private String urlPath = "jdbc:mysql://" + host + ":" + port; // Real url
  30. // path
  31.  
  32. /**
  33. * public void setHost: This method will set the host.
  34. *
  35. * @param host
  36. * The host of mySQL.
  37. */
  38. public void setHost(String host) {
  39. this.host = host;
  40. }
  41.  
  42. /**
  43. * public void setDBName: This method will set the database name.
  44. *
  45. * @param name
  46. * The name of the database.
  47. */
  48. public void setDBName(String name) {
  49. this.dbname = name;
  50. }
  51.  
  52. /**
  53. * public void setLogin: This method will set the login.
  54. *
  55. * @param login
  56. * The login.
  57. */
  58. public void setLogin(String login) {
  59. this.login = login;
  60. }
  61.  
  62. /**
  63. * public void setPassword: This method will set the password.
  64. *
  65. * @param passwd
  66. * The password.
  67. */
  68. public void setPassword(String passwd) {
  69. this.passwd = passwd;
  70. }
  71.  
  72. /**
  73. * public void setPort: This method will set the port.
  74. *
  75. * @param port
  76. * The port.
  77. */
  78. public void setPort(int port) {
  79. this.port = port;
  80. }
  81.  
  82. /**
  83. * public void init: This method will init this class.
  84. *
  85. * @throws ClassNotFoundException
  86. * @throws SQLException
  87. */
  88. public void init() throws ClassNotFoundException {
  89. urlPath = "jdbc:mysql://" + host + ":" + port;
  90. this.url = urlPath + "/" + this.dbname;
  91. connectFirstTime();
  92. }
  93.  
  94. /**
  95. * public void connectToDatabase: This method will init first connection.
  96. *
  97. * @throws ClassNotFoundException
  98. * @throws SQLException
  99. */
  100. private void connectFirstTime() throws ClassNotFoundException {
  101. Class.forName("com.mysql.jdbc.Driver");
  102. }
  103.  
  104. /**
  105. * public void connect: This method will connect you to mySQL.
  106. *
  107. * @throws SQLException
  108. */
  109. public void connect() throws SQLException {
  110. connection = DriverManager.getConnection(url, login, passwd);
  111. statement = connection.createStatement();
  112. }
  113.  
  114. /**
  115. * public void disconnect: This method will disconnect you from mySQL.
  116. *
  117. * @throws SQLException
  118. */
  119. public void disconnect() throws SQLException {
  120. if (resultat != null)
  121. resultat.close();
  122. if (statement != null)
  123. statement.close();
  124. if (connection != null)
  125. connection.close();
  126. }
  127.  
  128. /**
  129. * public Results select: This method will select objects in database, and
  130. * put it in Results.
  131. *
  132. * @param name
  133. * The database name.
  134. * @throws SQLException
  135. */
  136. public Results select(String msg) throws SQLException {
  137. return Results.transform(statement.executeQuery(msg));
  138. }
  139.  
  140. /**
  141. * public int update: This method will update the code below.
  142. *
  143. * @param msg
  144. * Message to execute.
  145. *
  146. * @return The integer value
  147. * @throws SQLException
  148. */
  149. public int update(String msg) throws SQLException {
  150. return statement.executeUpdate(msg);
  151. }
  152.  
  153. /**
  154. * public void execute: This method will execute the code below.
  155. *
  156. * @param msg
  157. * Message to execute.
  158. * @throws SQLException
  159. */
  160. public void execute(String msg) throws SQLException {
  161. statement.execute(msg);
  162. }
  163.  
  164. /**
  165. * public void createDB: This method will create the specifical database.
  166. *
  167. * @param name
  168. * The database name.
  169. * @throws SQLException
  170. */
  171. public void createDB(String name) throws SQLException {
  172. url = urlPath;
  173. this.dbname = name;
  174. execute("CREATE DATABASE IF NOT EXISTS " + name);
  175. disconnect();
  176. url = urlPath + "/" + this.dbname;
  177. connect();
  178. }
  179.  
  180. /**
  181. * public void connectToDatabase: This method will connect you to the
  182. * special database.
  183. *
  184. * @param name
  185. * The database name.
  186. * @throws SQLException
  187. */
  188. public void connectToDatabase(String name) throws SQLException {
  189. createDB(name);
  190. }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement