• jdbc操作mysql


    一.两种连接方式

    1.基础的数据库连接方式

    //使用反射加载驱动并注册驱动
    Class.forName("com.mysql.cj.jdbc.Driver");
    String url = "jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8";
    String user = "root";
    String password = "密码";
    //获取连接
    Connection connection = DriverManager.getConnection(url, user, password);
    Statement statement = connection.createStatement();
    String sql = "insert into studentinfo(name, age) values(\"男神\", 23)";
    //执行sql语句
    statement.execute(sql);
    //关闭连接
    statement.close();
    connection.close();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    2.搭配配置文件的数据库连接方式

    相比上一种方式,使用配置文件更加灵活

    //使用反射加载驱动并注册驱动
    Class.forName("com.mysql.cj.jdbc.Driver");
    Properties properties = new Properties();
    properties.load(new FileInputStream("src\\mysql.properties"));
    String url = properties.getProperty("Url");
    String user = properties.getProperty("User");
    String password = properties.getProperty("Password");
    //获取连接
    Connection connection = DriverManager.getConnection(url, user, password);
    Statement statement = connection.createStatement();
    String sql = "insert into studentinfo(name, age) values(\"渣男\", 90)";
    //执行sql语句
    statement.execute(sql);
    //关闭连接
    statement.close();
    connection.close();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    mysql.properties内容:

    Url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8
    User=root
    Password=密码
    
    • 1
    • 2
    • 3

    二.查询并获取结果集

     //使用反射加载驱动并注册驱动
    Class.forName("com.mysql.cj.jdbc.Driver");
    Properties properties = new Properties();
    properties.load(new FileInputStream("src\\mysql.properties"));
    String url = properties.getProperty("Url");
    String user = properties.getProperty("User");
    String password = properties.getProperty("Password");
    //获取连接
    Connection connection = DriverManager.getConnection(url, user, password);
    Statement statement = connection.createStatement();
    String sql = "select name, age from studentinfo";
    //执行sql语句
    ResultSet resultSet = statement.executeQuery(sql);
    //循环遍历结果集
    while(resultSet.next()){
    	String name = resultSet.getString(1);	//获取得到的数据的第一列
    	int age = resultSet.getInt(2);			//获取得到的数据的第二列
    	System.out.println("name : " + name + "\tage : " + age);
    }
    //关闭连接
    resultSet.close();
    statement.close();
    connection.close();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    三.使用PreparedStatement替代Statement

    PreparedStatement相比Statement执行效率更高,而且解决了sql注入问题,因此建议使用PreparedStatement

    1.增删改操作(executeUpdate)

    //使用反射加载驱动并注册驱动
    Class.forName("com.mysql.cj.jdbc.Driver");
    Properties properties = new Properties();
    properties.load(new FileInputStream("src\\mysql.properties"));
    String url = properties.getProperty("Url");
    String user = properties.getProperty("User");
    String password = properties.getProperty("Password");
    //获取连接
    Connection connection = DriverManager.getConnection(url, user, password);
    Statement statement = connection.createStatement();
    String sql = "insert into studentinfo(name, age) values(?, ?)";     //参数使用?代替
    PreparedStatement preparedStatement = connection.prepareStatement(sql);
    preparedStatement.setString(1, "王麻子");  //为第一个参数赋值
    preparedStatement.setInt(2, 25);          //为第二个参数赋值
    //执行sql语句
    int res = preparedStatement.executeUpdate();
    System.out.println(res > 0 ? "执行成功" : "执行失败");
    //关闭连接
    preparedStatement.close();
    connection.close();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    2.查询操作(executeQuery)

    //使用反射加载驱动并注册驱动
    Class.forName("com.mysql.cj.jdbc.Driver");
    Properties properties = new Properties();
    properties.load(new FileInputStream("src\\mysql.properties"));
    String url = properties.getProperty("Url");
    String user = properties.getProperty("User");
    String password = properties.getProperty("Password");
    //获取连接
    Connection connection = DriverManager.getConnection(url, user, password);
    
    String sql = "select id, name, age from studentinfo where id > ?";     //参数使用?代替
    PreparedStatement preparedStatement = connection.prepareStatement(sql);
    preparedStatement.setInt(1, 2);          //为第一个参数赋值
    //执行sql语句
    ResultSet resultSet = preparedStatement.executeQuery();
    while(resultSet.next()){
    	int id = resultSet.getInt(1);
    	String name = resultSet.getString(2);
    	int age = resultSet.getInt(3);
    	System.out.println("id : " + id + "\tname : " + name + "\tage : " + age);
    }
    //关闭连接
    resultSet.close();
    preparedStatement.close();
    connection.close();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
  • 相关阅读:
    【MATLAB教程案例5】常见无线通信信道的matlab模拟和仿真分析——自由空间损耗模型,Okumura-Hata模型以及COST231 Hata模型
    【COMP329 LEC4 Locomotion and Kinematics】
    【Java 进阶篇】JavaScript JSON 语法入门:轻松理解数据的序列化和反序列化
    mysql的约束和表关系
    力扣练习——48 找到小镇的法官
    Python爬虫入门基础学习(二)
    OpenCV笔记整理【绘制图形文字】
    el-dropdown(下拉菜单)的入门学习
    实现定时器
    中国棉花行业兼并重组机会研究及决策咨询报告
  • 原文地址:https://blog.csdn.net/m0_51765966/article/details/127952808