JDBC 创建数据库实例

  1. //1、注册驱动
  2. Class.forName("com.mysql.cj.jdbc.Driver");
  3. //2、连接数据库
  4. Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306", "root", "root");
  5. //3、创建数据库
  6. Statement stmt = conn.createStatement();
  7. String sql = "create database t_student";
  8. stmt.executeUpdate(sql);
  9. stmt.close();
  10. conn.close();

JDBC 选择数据库实例

  1. Class.forName("com.mysql.cj.jdbc.Driver");
  2. Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/t_students", "root", "root");
  3. conn.close();

JDBC 删除数据库实例

  1. Class.forName("com.mysql.cj.jdbc.Driver");
  2. Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/t_students", "root", "root");
  3. Statement stmt = conn.createStatement();
  4. String sql = "drop database t_students";
  5. stmt.executeUpdate(sql);
  6. stmt.close();
  7. conn.close();

JDBC 创建表实例

  1. Class.forName("com.mysql.cj.jdbc.Driver");
  2. Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/t_students", "root", "root");
  3. Statement stmt = conn.createStatement();
  4. String sql = "create table registration " +
  5. "(id integer not NULL, " +
  6. " first varchar(255), " +
  7. " last varchar(255), " +
  8. " age integer, " +
  9. " primary key ( id ))";
  10. stmt.executeUpdate(sql);
  11. conn.close();

JDBC 删除表实例

  1. Class.forName("com.mysql.cj.jdbc.Driver");
  2. Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/t_students", "root", "root");
  3. Statement stmt = conn.createStatement();
  4. String sql = "drop table registration";
  5. stmt.executeUpdate(sql);
  6. conn.close();

JDBC 插入记录实例

  1. Class.forName("com.mysql.cj.jdbc.Driver");
  2. Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/t_students", "root", "root");
  3. Statement stmt = conn.createStatement();
  4. String sql = "insert into registration values (100, 'Zara', 'Ali', 18)";
  5. stmt.executeUpdate(sql);
  6. sql = "insert into registration values (101, 'Mahnaz', 'Fatma', 25)";
  7. stmt.executeUpdate(sql);
  8. sql = "insert into registration values (102, 'Zaid', 'Khan', 30)";
  9. stmt.executeUpdate(sql);
  10. sql = "insert into registration values(103, 'Sumit', 'Mittal', 28)";
  11. stmt.executeUpdate(sql);
  12. conn.close();

JDBC 查询记录实例

  1. Class.forName("com.mysql.cj.jdbc.Driver");
  2. Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/t_students", "root", "root");
  3. Statement stmt = conn.createStatement();
  4. String sql = "select id, first, last, age from registration";
  5. ResultSet rs = stmt.executeQuery(sql);
  6. while(rs.next()){
  7. int id = rs.getInt("id");
  8. int age = rs.getInt("age");
  9. String first = rs.getString("first");
  10. String last = rs.getString("last");
  11. System.out.print("ID: " + id);
  12. System.out.print(", Age: " + age);
  13. System.out.print(", First: " + first);
  14. System.out.println(", Last: " + last);
  15. }
  16. rs.close();
  17. conn.close();

JDBC 更新记录实例

  1. Class.forName("com.mysql.cj.jdbc.Driver");
  2. Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/t_students", "root", "root");
  3. Statement stmt = conn.createStatement();
  4. String sql = "update registration set age = 30 where id in (100, 101)";
  5. stmt.executeUpdate(sql);
  6. sql = "select id, first, last, age from registration";
  7. ResultSet rs = stmt.executeQuery(sql);
  8. while(rs.next()){
  9. int id = rs.getInt("id");
  10. int age = rs.getInt("age");
  11. String first = rs.getString("first");
  12. String last = rs.getString("last");
  13. System.out.print("ID: " + id);
  14. System.out.print(", Age: " + age);
  15. System.out.print(", First: " + first);
  16. System.out.println(", Last: " + last);
  17. }
  18. rs.close();
  19. conn.close();

JDBC 删除记录实例

  1. Class.forName("com.mysql.cj.jdbc.Driver");
  2. Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/t_students", "root", "root");
  3. Statement stmt = conn.createStatement();
  4. String sql = "delete from registration where id = 101";
  5. stmt.executeUpdate(sql);
  6. sql = "select id, first, last, age from registration";
  7. ResultSet rs = stmt.executeQuery(sql);
  8. while(rs.next()){
  9. int id = rs.getInt("id");
  10. int age = rs.getInt("age");
  11. String first = rs.getString("first");
  12. String last = rs.getString("last");
  13. System.out.print("ID: " + id);
  14. System.out.print(", Age: " + age);
  15. System.out.print(", First: " + first);
  16. System.out.println(", Last: " + last);
  17. }
  18. rs.close();
  19. conn.close();

JDBC WHERE 子句实例

  1. Class.forName("com.mysql.cj.jdbc.Driver");
  2. Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/t_students", "root", "root");
  3. Statement stmt = conn.createStatement();
  4. String sql = "SELECT id, first, last, age FROM Registration";
  5. ResultSet rs = stmt.executeQuery(sql);
  6. while (rs.next()) {
  7. int id = rs.getInt("id");
  8. int age = rs.getInt("age");
  9. String first = rs.getString("first");
  10. String last = rs.getString("last");
  11. System.out.print("ID: " + id);
  12. System.out.print(", Age: " + age);
  13. System.out.print(", First: " + first);
  14. System.out.println(", Last: " + last);
  15. }
  16. sql = "SELECT id, first, last, age FROM Registration WHERE id >= 101 ";
  17. rs = stmt.executeQuery(sql);
  18. while (rs.next()) {
  19. int id = rs.getInt("id");
  20. int age = rs.getInt("age");
  21. String first = rs.getString("first");
  22. String last = rs.getString("last");
  23. //Display values
  24. System.out.print("ID: " + id);
  25. System.out.print(", Age: " + age);
  26. System.out.print(", First: " + first);
  27. System.out.println(", Last: " + last);
  28. }
  29. rs.close();
  30. conn.close();

JDBC LIKE 子句实例

  1. Class.forName("com.mysql.cj.jdbc.Driver");
  2. Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/t_students", "root", "root");
  3. Statement stmt = conn.createStatement();
  4. String sql = "select id, first, last, age from registration";
  5. ResultSet rs = stmt.executeQuery(sql);
  6. while (rs.next()) {
  7. int id = rs.getInt("id");
  8. int age = rs.getInt("age");
  9. String first = rs.getString("first");
  10. String last = rs.getString("last");
  11. System.out.print("ID: " + id);
  12. System.out.print(", Age: " + age);
  13. System.out.print(", First: " + first);
  14. System.out.println(", Last: " + last);
  15. }
  16. sql = "select id, first, last, age from registration where first like '%za%' ";
  17. rs = stmt.executeQuery(sql);
  18. while (rs.next()) {
  19. int id = rs.getInt("id");
  20. int age = rs.getInt("age");
  21. String first = rs.getString("first");
  22. String last = rs.getString("last");
  23. System.out.print("ID: " + id);
  24. System.out.print(", Age: " + age);
  25. System.out.print(", First: " + first);
  26. System.out.println(", Last: " + last);
  27. }
  28. rs.close();
  29. conn.close();

JDBC 排序实例

  1. Class.forName("com.mysql.cj.jdbc.Driver");
  2. Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/t_students", "root", "root");
  3. Statement stmt = conn.createStatement();
  4. String sql = "select id, first, last, age from registration order by first asc";
  5. ResultSet rs = stmt.executeQuery(sql);
  6. while (rs.next()) {
  7. int id = rs.getInt("id");
  8. int age = rs.getInt("age");
  9. String first = rs.getString("first");
  10. String last = rs.getString("last");
  11. System.out.print("ID: " + id);
  12. System.out.print(", Age: " + age);
  13. System.out.print(", First: " + first);
  14. System.out.println(", Last: " + last);
  15. }
  16. sql = "select id, first, last, age from registration order by first desc";
  17. rs = stmt.executeQuery(sql);
  18. while (rs.next()) {
  19. int id = rs.getInt("id");
  20. int age = rs.getInt("age");
  21. String first = rs.getString("first");
  22. String last = rs.getString("last");
  23. System.out.print("ID: " + id);
  24. System.out.print(", Age: " + age);
  25. System.out.print(", First: " + first);
  26. System.out.println(", Last: " + last);
  27. }
  28. rs.close();
  29. conn.close();