Advertisement
Guest User

Untitled

a guest
Nov 19th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.02 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package jdbc;
  7. import java.sql.*;
  8. import java.util.Calendar;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11. /**
  12. *
  13. * @author Yuki_Khiem
  14. */
  15.  
  16. public class JDBC {
  17.  
  18. /**
  19. * @param args the command line arguments
  20. */
  21. String url = "jdbc:mysql://127.0.0.1";
  22. String port = "3306";
  23. String database = "sakila";
  24. String user = "root";
  25. String password = "";
  26. Statement stmt = null;
  27. ResultSet rs = null;
  28. Connection con = null;
  29. public JDBC() {
  30.  
  31. }
  32. // create a connection to the database
  33. public Connection Connect() {
  34.  
  35. System.out.println("Get connection ...");
  36. try {
  37. Class.forName("com.mysql.jdbc.Driver");
  38. con = DriverManager.getConnection(url + ":" + port + "/" + database, user, password);
  39. //info Driver and Product
  40. DatabaseMetaData dm = (DatabaseMetaData) con.getMetaData();
  41. System.out.println("Driver name: " + dm.getDriverName());
  42. System.out.println("Driver version: " + dm.getDriverVersion());
  43. System.out.println("Product name: " + dm.getDatabaseProductName());
  44. System.out.println("Product version: " + dm.getDatabaseProductVersion());
  45. System.out.println("---------------Done--------------" + "\n");
  46. } catch (SQLException sqle) {
  47. System.out.println("Connection Failed!");
  48. System.out.println("SQLException: " + sqle.getMessage() + "SQLState: " + sqle.getSQLState());
  49. } catch (ClassNotFoundException e) {
  50. System.out.println("Connection Failed!");
  51. System.out.println("ERROR: " + e.getMessage());
  52. }
  53. return con;
  54. }
  55. // query
  56. public String getQuery(String q) {
  57. return q;
  58. }
  59. // Result query
  60. public int ResultCau1(Connection con, String query) {
  61. try {
  62. stmt = con.createStatement();
  63. rs = stmt.executeQuery(query);
  64. System.out.println("Query 1: " + query + "\n");
  65. if (!rs.next()) {
  66. System.out.println("No data found in query!! \n");
  67. return 0;
  68. }
  69. do {
  70. int count = 1;
  71. String resultQuery = "";
  72. while (rs.getString(count) != null) {
  73. resultQuery += rs.getString(count) + " ";
  74. count++;
  75. }
  76. System.out.println(resultQuery);
  77. }
  78. while (rs.next());
  79. System.out.println("\n");
  80. } catch (SQLException sqle) {
  81. System.out.println("SQLException: " + sqle.getMessage() + " SQLState: " + sqle.getSQLState());
  82. } catch (Exception e) {
  83. System.out.println("Exception: " + e.getMessage());
  84. }
  85. return 0;
  86. }
  87. public int ResultCau2(Connection con, String query) {
  88. try {
  89. stmt = con.createStatement();
  90. con.setAutoCommit(false);
  91. System.out.println("Query 2: " + query + "\n");
  92. stmt.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);
  93. rs = stmt.getGeneratedKeys();
  94. while (rs.next()) {
  95. System.out.println("Result: Automatically generated key value = " + rs.getInt(1) + "\n");
  96. }
  97. con.commit();
  98. System.out.println("Inserted records to table. \n");
  99. } catch (SQLException sqle) {
  100. System.out.println("SQLException: " + sqle.getMessage() + " SQLState: " + sqle.getSQLState());
  101. } catch (Exception e) {
  102. System.out.println("Exception: " + e.getMessage());
  103. }
  104. return 0;
  105. }
  106. public int ResultCau32(Connection con) {
  107. Calendar calendar = Calendar.getInstance();
  108. Timestamp currentTimestamp = new java.sql.Timestamp(calendar.getTime().getTime());
  109. String query = "INSERT INTO actor \n" + "VALUES (null, ?, ?, ?)";
  110. System.out.println("Query 32: " + query + "\n");
  111. try {
  112. PreparedStatement Pstmt = con.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
  113. Pstmt.setString(1, "Thanh");
  114. Pstmt.setString(2, "Thanh");
  115. Pstmt.setTimestamp(3, currentTimestamp);
  116. int i = Pstmt.executeUpdate();
  117. con.setAutoCommit(false);
  118. con.commit();
  119. rs = Pstmt.getGeneratedKeys();
  120. while (rs.next()) {
  121. System.out.println("Result: Automatically generated key value = " + rs.getInt(1));
  122. }
  123. System.out.println(i + " records inserted \n");
  124. } catch (SQLException sqle) {
  125. System.out.println("SQLException: " + sqle.getMessage() + "SQLState: " + sqle.getSQLState());
  126. } catch (Exception e) {
  127. System.out.println("Exception: " + e.getMessage());
  128. }
  129. return 0;
  130. }
  131. public int ResultCau31(Connection con) {
  132. String query = "SELECT * FROM film \n" +
  133. " JOIN film_actor ON film.film_id = film_actor.film_id \n" +
  134. " WHERE film_actor.actor_id = ?";
  135. try {
  136. PreparedStatement Pstmt = con.prepareStatement(query);
  137. Pstmt.setInt(1, 1);
  138. rs = Pstmt.executeQuery();
  139. System.out.println("Query 31: " + query + "\n");
  140. if (!rs.next()) {
  141. System.out.println("No data found in query!! \n");
  142. return 0;
  143. }
  144. do {
  145. int count = 1;
  146. String resultQuery = "";
  147. while (rs.getString(count) != null) {
  148. resultQuery += rs.getString(count) + " ";
  149. count++;
  150. }
  151. System.out.println(resultQuery);
  152. }
  153. while (rs.next());
  154. System.out.println("\n");
  155. } catch (SQLException sqle) {
  156. System.out.println("SQLException: " + sqle.getMessage() + " SQLState: " + sqle.getSQLState());
  157. } catch (Exception e) {
  158. System.out.println("Exception: " + e.getMessage());
  159. }
  160. return 0;
  161. }
  162. public int ResultCau4(Connection con, String query1, String query2) throws SQLException {
  163. try {
  164. stmt = con.createStatement();
  165. con.setAutoCommit(false);
  166. System.out.println("Query 4 add record 1: " + query1 + "\n");
  167. stmt.executeUpdate(query1);
  168. System.out.println("Inserted record 1 to table. \n");
  169. System.out.println("Query 4 add record 2: " + query2 + "\n");
  170. stmt.executeUpdate(query2);
  171. System.out.println("Inserted record 2 to table. \n");
  172. con.commit();
  173. } catch (SQLException sqle) {
  174. con.rollback();
  175. System.out.println("SQLException: " + sqle.getMessage() + " SQLState: " + sqle.getSQLState());
  176. } catch (Exception e) {
  177. System.out.println("Exception: " + e.getMessage());
  178. }
  179. return 0;
  180. }
  181. //Close connect
  182. public void Close() {
  183. if (con != null)
  184. try {
  185. con.close();
  186. } catch (SQLException sqle) {
  187. System.out.println("SQLException: " + sqle.getMessage() + " SQLState: " + sqle.getSQLState());
  188. }
  189. }
  190. public static void main(String[] args) throws SQLException {
  191. JDBC Gt1 = new JDBC();
  192. String cau1 = "SELECT * FROM film \n" +
  193. " JOIN film_actor ON film.film_id = film_actor.film_id \n" +
  194. " WHERE film_actor.actor_id = 1";
  195. String cau2 = "INSERT INTO actor \n" +
  196. "VALUES (NULL, 'Thanh', 'Xuan', CURRENT_TIMESTAMP)";
  197. String cau41 = "INSERT INTO rental VALUES (NULL, '2018-11-22 00:00:00', '367', '459', '2018-11-30 00:00:00', '1', CURRENT_TIMESTAMP)";
  198. String cau42 = "INSERT INTO rental VALUES (NULL, '2018-11-23 00:00:00', '389', '142', '2018-11-30 00:00:00', '1', CURRENT_TIMESTAMP)";
  199. Connection conn = Gt1.Connect();
  200. //Cau 1
  201. Gt1.ResultCau1(conn, Gt1.getQuery(cau1));
  202. // Cau 2
  203. Gt1.ResultCau2(conn, Gt1.getQuery(cau2));
  204. //cau 3
  205. Gt1.ResultCau31(conn);
  206. Gt1.ResultCau32(conn);
  207. //cau 4
  208. Gt1.ResultCau4(conn, cau41, cau42);
  209. Gt1.Close();
  210. }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement