Advertisement
Guest User

Untitled

a guest
Mar 6th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. package jdbc;
  2.  
  3. import java.sql.*;
  4. import static java.lang.System.*;
  5.  
  6. public class MySqlDb
  7. {
  8. private Connection connection;
  9. private Statement statement;
  10.  
  11. public MySqlDb(String url, String dbName, String username, String password)
  12. throws SQLException {
  13.  
  14. connection = DriverManager.getConnection(
  15. "jdbc:mysql://" + url + ":3306/" + dbName,
  16. username,
  17. password);
  18. }
  19.  
  20. public String select(String tableName, String fields[]) {
  21. return select(tableName, fields, "1 = 1");
  22. }
  23.  
  24. public String select(String tableName, String fields[], String crits) {
  25. String selectStatement = "SELECT * "
  26. + "FROM " + tableName + " "
  27. + "WHERE " + crits;
  28. String ret = "";
  29.  
  30. try {
  31. statement = connection.createStatement();
  32. ResultSet result = statement.executeQuery(selectStatement);
  33.  
  34. while (result.next()) {
  35. for (String field : fields) {
  36. String currentFieldValue = result.getString(field);
  37.  
  38. if (currentFieldValue != null) {
  39. ret += result.getString(field) + "t";
  40. }
  41. }
  42.  
  43. ret = ret.substring(0, ret.length() - 1) + "n";
  44. }
  45.  
  46. } catch (SQLException e) {
  47. err.println(createSqlExceptionInfo(e));
  48. } finally {
  49. resetStatement();
  50. }
  51.  
  52. return ret;
  53. }
  54.  
  55. public void insert(String sqlInsert) {
  56. try {
  57. statement = connection.createStatement();
  58.  
  59. statement.executeUpdate(sqlInsert);
  60. } catch (SQLException e) {
  61. err.println(createSqlExceptionInfo(e));
  62. } finally {
  63. resetStatement();
  64. }
  65. }
  66.  
  67. public void update(String sqlUpdate) {
  68. try {
  69. statement = connection.createStatement();
  70.  
  71. statement.executeUpdate(sqlUpdate);
  72. } catch (SQLException e) {
  73. err.println(createSqlExceptionInfo(e));
  74. } finally {
  75. resetStatement();
  76. }
  77. }
  78.  
  79. public void delete(String sqlDelete) {
  80. try {
  81. statement = connection.createStatement();
  82.  
  83. statement.executeUpdate(sqlDelete);
  84. } catch (SQLException e) {
  85. err.println(createSqlExceptionInfo(e));
  86. } finally {
  87. resetStatement();
  88. }
  89. }
  90.  
  91. public boolean closeConnection() {
  92. try {
  93. connection.close();
  94.  
  95. return true;
  96. } catch (SQLException e) {
  97. err.println(createSqlExceptionInfo(e));
  98. }
  99.  
  100. return false;
  101. }
  102.  
  103. public static String createSqlExceptionInfo(SQLException e) {
  104. String ret = "SQL-State:t" + e.getSQLState() + "n";
  105. ret += "SQL error-code:t" + e.getErrorCode() + "n";
  106. ret += "Description:t" + e.getMessage();
  107.  
  108. return ret;
  109. }
  110.  
  111. private void resetStatement() {
  112. if (statement != null) {
  113. statement = null;
  114. }
  115. }
  116. }
  117.  
  118. package jdbc;
  119.  
  120. import static java.lang.System.*;
  121. import java.sql.SQLException;
  122.  
  123. public class Jdbc
  124. {
  125. public static void main(String[] args)
  126. {
  127. MySqlDb mysql = null;
  128.  
  129. try {
  130. mysql = new MySqlDb("localhost", "seminar_db", "root", "root");
  131. } catch (SQLException e) {
  132. MySqlDb.createSqlExceptionInfo(e);
  133. }
  134.  
  135. if (mysql != null) {
  136.  
  137. mysql.insert(
  138. "INSERT INTO Seminar (thema, beschreibung) " +
  139. "VALUES ('Java', "
  140. + "'Introduction to Java.')");
  141. mysql.insert(
  142. "INSERT INTO Seminar (thema, beschreibung) " +
  143. "VALUES ('Java 2', "
  144. + "'Advanced Java.')");
  145. mysql.insert(
  146. "INSERT INTO Seminar (thema, beschreibung) " +
  147. "VALUES ('PHP', "
  148. + "'Get started with PHP.')");
  149. mysql.insert(
  150. "INSERT INTO Seminar (thema, beschreibung) " +
  151. "VALUES ('XML', "
  152. + "'XML-Introduction.')");
  153. mysql.insert(
  154. "INSERT INTO Seminar (thema, beschreibung) " +
  155. "VALUES ('HTML/CSS', "
  156. + "'Webdesign course')");
  157. mysql.insert(
  158. "INSERT INTO Seminar (thema, beschreibung) " +
  159. "VALUES ('JavaScript', "
  160. + "'Webprogramming')");
  161. }
  162.  
  163. String[] fields = new String[2];
  164. fields[0] = "thema";
  165. fields[1] = "beschreibung";
  166.  
  167. out.println(mysql.select("Seminar", fields));
  168.  
  169. mysql
  170. .update(
  171. "UPDATE Seminar SET beschreibung = 'PHP beginner' WHERE thema = 'PHP'"
  172. );
  173.  
  174. out.println(mysql.select("Seminar", fields, "thema = 'PHP'"));
  175.  
  176. if (mysql.closeConnection() == true) {
  177. out.println("Database connection has been closed.");
  178. }
  179. }
  180. }
  181.  
  182. Java Introduction to Java.
  183. Java 2 Advanced Java.
  184. PHP Get started with PHP.
  185. XML XML-Introduction.
  186. HTML/CSS Webdesign course
  187. JavaScript Webprogramming
  188.  
  189. PHP PHP beginner
  190.  
  191. Database connection has been closed.
  192. BUILD SUCCESSFUL (total time: 0 seconds)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement