当前位置:   article > 正文

java连接mysql增删改查(01单增删改查)_java连接mysq 增删改查

java连接mysq 增删改查

0.创建数据库

  1. // 1)、创建数据库
  2. CREATE DATABASE jdbc DEFAULT CHARACTER SET UTF8;
  3. // 2)、切换数据库
  4. USE jdbc;
  5. // 3)、创建数据库表
  6. CREATE TABLE user(
  7. `user_id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT '主键',
  8. `user_name` VARCHAR(20) NOT NULL COMMENT '用户名',
  9. `price` double(10,2) DEFAULT 0.0 COMMENT '价格',
  10. `create_time` DATETIME DEFAULT NULL COMMENT '创建时间'
  11. )ENGINE=InnoDB AUTO_INCREMENT=1000 DEFAULT CHARSET=UTF8 COMMENT="用户表";
  12. // 4)、插入用户
  13. INSERT INTO user(user_name,price,create_time)VALUES('admin',100.12,now());

1.增加

  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.SQLException;
  4. import java.sql.Statement;
  5. public class JdbcDemo {
  6. public static void main(String[] args) throws ClassNotFoundException, SQLException {
  7. //1.加载数据库驱动
  8. Class.forName("com.mysql.jdbc.Driver");
  9. //2.创建数据库连接
  10. String url="jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
  11. String user="root";
  12. String password="root";
  13. Connection connection = DriverManager.getConnection(url, user, password);
  14. //3.创建statement对象
  15. Statement statement = connection.createStatement();
  16. //4.准备sql语句
  17. String sql="INSERT INTO user(user_name,price,create_time) VALUES(\"李雷\",200.2,'2021-12-12')";
  18. //5.执行sql
  19. int row = statement.executeUpdate(sql);
  20. System.out.println("row" + row);
  21. //6.关闭连接
  22. connection.close();
  23. }
  24. }

2.删除

  1. public class JdbcDemo3 {
  2. public static void main(String[] args) throws ClassNotFoundException, SQLException {
  3. //1.加载数据库驱动
  4. Class.forName("com.mysql.jdbc.Driver");
  5. //2.创建数据库连接
  6. String url="jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
  7. String user="root";
  8. String password="root";
  9. Connection connection = DriverManager.getConnection(url, user, password);
  10. //3.创建statement对象
  11. Statement statement = connection.createStatement();
  12. //4.准备sql
  13. String sql="DELETE FROM user WHERE user_id=1000";
  14. //5.将sql语句发送到MYSQL服务器
  15. int row = statement.executeUpdate(sql);
  16. System.out.println("row" + row);
  17. //6.关闭连接
  18. connection.close();
  19. }
  20. }

3.修改

  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.SQLException;
  4. import java.sql.Statement;
  5. public class JdbcDemo2 {
  6. public static void main(String[] args) throws ClassNotFoundException, SQLException {
  7. // TODO Auto-generated method stub
  8. //1.加载数据库驱动
  9. Class.forName("com.mysql.jdbc.Driver");
  10. //2.创建数据库连接
  11. String url="jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
  12. String user="root";
  13. String password="root";
  14. Connection connection = DriverManager.getConnection(url, user, password);
  15. //3.创建statement对象
  16. Statement statement = connection.createStatement();
  17. //4.准备sql
  18. String sql="UPDATE user SET user_name='admin1',price=12.12,create_time=now()" +
  19. "WHERE user_id=1001";
  20. //5.将sql语句发送到MYSQL服务器
  21. int row = statement.executeUpdate(sql);
  22. System.out.println("row:" + row);
  23. //6.关闭连接
  24. connection.close();
  25. }
  26. }

4.查询单个

  1. import java.sql.Connection;
  2. import java.sql.Date;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. public class JdbcDemo4 {
  8. public static void main(String[] args) throws ClassNotFoundException, SQLException {
  9. //1.加载数据库驱动
  10. Class.forName("com.mysql.jdbc.Driver");
  11. //2.创建数据库连接
  12. String url="jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
  13. String user="root";
  14. String password="root";
  15. Connection connection = DriverManager.getConnection(url, user, password);
  16. //3.创建statement对象
  17. Statement statement = connection.createStatement();
  18. //4.准备sql
  19. String sql="SELECT * FROM user where user_id = 10001";
  20. //5.将sql语句发送到MYSQL服务器
  21. ResultSet rs = statement.executeQuery(sql);
  22. //6.处理结果集
  23. if(rs.next()) {
  24. //处理结果
  25. System.out.println("结果集有数据");
  26. int userId = rs.getInt("user_id");
  27. String username = rs.getString("user_name");
  28. double price =rs.getDouble("price");
  29. Date date = rs.getDate("create_time");
  30. System.out.println("userId:"+userId);
  31. System.out.println("username:"+username);
  32. System.out.println("price:"+price);
  33. System.out.println("date:"+date);
  34. }else{
  35. System.out.println("结果集无数据");
  36. }
  37. //7.关闭连接
  38. connection.close();
  39. rs.close();
  40. }
  41. }

5.查询结果集

  1. import java.sql.Connection;
  2. import java.sql.Date;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. public class JdbcDemo4 {
  8. public static void main(String[] args) throws ClassNotFoundException, SQLException {
  9. //1.加载数据库驱动
  10. Class.forName("com.mysql.jdbc.Driver");
  11. //2.创建数据库连接
  12. String url="jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
  13. String user="root";
  14. String password="root";
  15. Connection connection = DriverManager.getConnection(url, user, password);
  16. //3.创建statement对象
  17. Statement statement = connection.createStatement();
  18. //4.准备sql
  19. String sql="SELECT * FROM user";
  20. //5.将sql语句发送到MYSQL服务器
  21. ResultSet rs = statement.executeQuery(sql);
  22. //6.处理结果集
  23. while(rs.next()) {
  24. //处理结果
  25. System.out.println("结果集有数据");
  26. int userId = rs.getInt("user_id");
  27. String username = rs.getString("user_name");
  28. double price =rs.getDouble("price");
  29. Date date = rs.getDate("create_time");
  30. System.out.println("userId:"+userId);
  31. System.out.println("username:"+username);
  32. System.out.println("price:"+price);
  33. System.out.println("date:"+date);
  34. }
  35. //7.关闭连接
  36. connection.close();
  37. rs.close();
  38. }
  39. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/575439
推荐阅读
相关标签
  

闽ICP备14008679号