Advertisement
Guest User

Untitled

a guest
Nov 25th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 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 mysql.jdbc;
  7.  
  8. import com.mysql.jdbc.Connection;
  9. import com.mysql.jdbc.Statement;
  10. import java.sql.DriverManager;
  11. import java.sql.PreparedStatement;
  12. import java.sql.ResultSet;
  13. import java.sql.SQLException;
  14.  
  15. /**
  16. *
  17. * @author CCNE
  18. */
  19. public class MySQLJDBC {
  20.  
  21. /**
  22. * @param args the command line arguments
  23. */
  24. public static void main(String[] args) throws ClassNotFoundException, SQLException {
  25. Class.forName("com.mysql.jdbc.Driver");
  26. //question1();
  27. //question2("huong", "vu");
  28.  
  29. // Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "12345678");
  30. // java.sql.Statement stmt = con.createStatement();
  31. // String query = "SELECT * FROM t";
  32. // ResultSet rs = stmt.executeQuery(query);
  33. // System.out.print(rs.getMetaData().getColumnCount());
  34. }
  35. private static void question1() throws SQLException {
  36. Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/sakila", "root", "12345678");
  37. java.sql.Statement stmt = con.createStatement();
  38. String query = "SELECT f.title FROM film f JOIN film_actor fa WHERE fa.actor_id = 1 GROUP BY f.title";
  39. ResultSet rs = stmt.executeQuery(query);
  40. while(rs.next()) {
  41. System.out.println("ten phim: " + rs.getString("title"));
  42. }
  43. con.close();
  44. }
  45. private static void question2(String first_name, String last_name) throws SQLException {
  46. Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/sakila", "root", "12345678");
  47.  
  48. String query = "INSERT INTO actor (first_name, last_name, last_update) "
  49. + "value(?, ?, NOW());";
  50.  
  51. PreparedStatement pstm = con.prepareStatement(query, java.sql.Statement.RETURN_GENERATED_KEYS);
  52.  
  53. pstm.setString(1, first_name);
  54. pstm.setString(2, last_name);
  55.  
  56. pstm.executeUpdate();
  57. ResultSet rs = pstm.getGeneratedKeys();
  58. while(rs.next()) {
  59. System.out.println("them thanh cong, id: " + rs.getInt(1));
  60. }
  61.  
  62. con.close();
  63. }
  64. private static void question4() {
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement