当前位置:   article > 正文

MySQL——statement对象详解

MySQL——statement对象详解

JDBC中的statement对象用于向数据库发送SQL语句,想完成对数据库的增删改查,只需要通过这个对象向数据库发送增删改查语句即可。

Statement对象的executeUpdate方法,用于向数据库发送增、删、改的sql语句,executeUpdate执行完成后,将会返回一个整数(即增删改语句导致了数据库几行数据发生了变化)。

Statement.executeQuery方法用于向数据库发送查询语句,executeQuery方法返回代表查询结果的ResultSet对象。

CRUD操作-create

 使用executeUpdate(String sql)方法完成数据添加操作,示例操作:

  1. Statement st = conn.createStatement();
  2. String sql = "insert into user(... .) values(... . .)"
  3. int num = st.executeUpdate(sql);
  4. if(num > 0){
  5. System.out.println("插入成功!");
  6. }

CRUD操作-delete

 使用executeUpdate(String sql)方法完成数据删除操作,示例操作:

  1. Statement st = conn.createStatement();
  2. String sql = "delete from user where id=1"
  3. int num = st.executeUpdate(sql);
  4. if(num > 0){
  5. System.out.println("删除成功!");
  6. }

CRUD操作-update

 使用executeUpdate(String sql)方法完成数据修改操作,示例操作:

  1. Statement st = conn.createStatement();
  2. String sql = "update user set name='' where name=''"
  3. int num = st.executeUpdate(sql);
  4. if(num > 0){
  5. System.out.println("修改成功!");
  6. }

CRUD操作-read

使用executeQuery(String sql)方法完成数据查询操作,示例操作:

  1. Statement st = conn.createStatement();
  2. String sql = "select * from user where id=1"
  3. ResultSet rs = st.executeQuery(sql);
  4. while(rs.next()){
  5. // 根据获取列的数据类型,分别调用rs的相应方法映射到java对象中
  6. }

代码实现

1. 提取工具类

  1. public class jdbcUtils {
  2. private static String driver = null;
  3. private static String url = null;
  4. private static String username = null;
  5. private static String password = null;
  6. static {
  7. try {
  8. InputStream in = jdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");// 获取具体资源
  9. Properties properties = new Properties();
  10. properties.load(in);
  11. driver = properties.getProperty("driver");
  12. url = properties.getProperty("url");
  13. username = properties.getProperty("username");
  14. password = properties.getProperty("password");
  15. // 1.驱动只用加载一次
  16. Class.forName(driver);
  17. } catch (IOException | ClassNotFoundException e) {
  18. throw new RuntimeException(e);
  19. }
  20. }
  21. // 获取连接
  22. public static Connection getConnection() throws SQLException {
  23. return DriverManager.getConnection(url,username,password);
  24. }
  25. // 释放连接资源
  26. public static void release(Connection conn, Statement stmt, ResultSet rs) throws SQLException {
  27. if (rs!=null){
  28. rs.close();
  29. }
  30. if (stmt!=null){
  31. stmt.close();
  32. }
  33. if (conn!=null){
  34. conn.close();
  35. }
  36. }
  37. }

2. 编写增删改的方法,executeUpdate

增:

  1. public class TestInsert {
  2. public static void main(String[] args) throws SQLException {
  3. Connection conn = null;
  4. Statement st = null;
  5. ResultSet rs = null;
  6. try {
  7. conn = jdbcUtils.getConnection(); // 获取数据库连接
  8. st = conn.createStatement(); // 获得SQL的执行对象
  9. String sql = "INSERT INTO users(id,`NAME`,`PASSWORD`,`email`,`birthday`)" +
  10. "VALUES(4,'chenyang','123456','888@qq.com','2024-7-5')";
  11. int i = st.executeUpdate(sql);
  12. if (i>0){
  13. System.out.println("插入成功!");
  14. }
  15. } catch (SQLException e) {
  16. throw new RuntimeException(e);
  17. }finally {
  18. jdbcUtils.release(conn,st,rs);
  19. }
  20. }
  21. }

删:

  1. public class TestDelete {
  2. public static void main(String[] args) throws SQLException {
  3. Connection conn = null;
  4. Statement st = null;
  5. ResultSet rs = null;
  6. try {
  7. conn = jdbcUtils.getConnection();
  8. st = conn.createStatement();
  9. String sql = "DELETE FROM users WHERE id=4";
  10. int i = st.executeUpdate(sql);
  11. if (i>0){
  12. System.out.println("删除成功!");
  13. }
  14. } catch (SQLException e) {
  15. throw new RuntimeException(e);
  16. }finally {
  17. jdbcUtils.release(conn,st,rs);
  18. }
  19. }

改:

  1. public class TestUpdate {
  2. public static void main(String[] args) throws SQLException {
  3. Connection conn = null;
  4. Statement st = null;
  5. ResultSet rs = null;
  6. try {
  7. conn = jdbcUtils.getConnection();
  8. st = conn.createStatement();
  9. String sql = "UPDATE users SET `NAME`='chenyang8' WHERE id=4";
  10. int i = st.executeUpdate(sql);
  11. if (i>0){
  12. System.out.println("修改成功!");
  13. }
  14. } catch (SQLException e) {
  15. throw new RuntimeException(e);
  16. }finally {
  17. jdbcUtils.release(conn,st,rs);
  18. }
  19. }
  20. }

3. 查询,executeQuery 

  1. public class TestSelect {
  2. public static void main(String[] args) throws SQLException {
  3. Connection conn = null;
  4. Statement st = null;
  5. ResultSet rs = null;
  6. try {
  7. conn = jdbcUtils.getConnection();
  8. st = conn.createStatement();
  9. //SQl
  10. String sql = "select * from users where id = 1";
  11. rs = st.executeQuery(sql); //查询完毕会返回一个结果集
  12. if (rs.next()){
  13. System.out.println(rs.getString("NAME"));
  14. }
  15. } catch (SQLException e) {
  16. throw new RuntimeException(e);
  17. }finally {
  18. jdbcUtils.release(conn,st,rs);
  19. }
  20. }
  21. }

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号