Mysql: JDBC(Java 操作数据库-mysql)

JDBC(Java连接数据库)

maven

<!--    jdbc添加数据库依赖-->
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

测试代码

import java.sql.*;

/**
 * @ProjectName: jdbc
 * @PackageName: PACKAGE_NAME
 * @Author: zy7y
 * @Date: 2020/8/26 下午5:38
 * @Description: jdbc
 */
public class TestJdbc01 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        // 1. 加载驱动
        Class.forName("com.mysql.jdbc.Driver");

        // 2. 新建连接
        String url = "jdbc:mysql://192.168.0.222:3306/school?useUnicode=true&characterEncoding=utf8&useSSL=true";
        String username = "root";
        String password = "root";

        // 建立连接 得到操作数据库的对象
        Connection connection = DriverManager.getConnection(url,username,password);

        // 3. 执行sql
        // 执行sql的对象
        Statement statement = connection.createStatement();

        String sql = "SELECT * FROM student";

        // 执行sql,得到结果集
        ResultSet resultSet = statement.executeQuery(sql);

        while (resultSet.next()){
            System.out.println("id=" + resultSet.getObject("studentno"));
        }
        // 4. 释放连接
        resultSet.close(); // 关闭结果集对象
        statement.close(); // 关闭执行sql对象
        connection.close(); // 关闭数据库对象
    }
}

Connection数据库对象

        //  设置 关闭自动提交
        connection.setAutoCommit(false);
        // 事务提交
        connection.commit();
        // 事务回滚
        connection.rollback();

Statement执行SQL的对象

 // 执行sql,得到结果集 resultSet
        ResultSet resultSet = statement.executeQuery(sql);
        // 执行任何sql语句
        statement.execute(sql);
        // 更新、插入、删除使用,返回一个受影响的行数
        statement.executeUpdate(sql); 

ResultSet 结果集

            // 获得任意类型的列数据
            resultSet.getObject("id");
            // 获得String类型的列数据
            resultSet.getString("name");
            resultSet.getInt("age");
            resultSet.getFloat("money");
            resultSet.getDate("date");

释放连接

        resultSet.close(); // 关闭结果集对象
        statement.close(); // 关闭执行sql对象
        connection.close(); // 关闭数据库对象

SQL注入

https://www.liaoxuefeng.com/wiki/1252599548343744/1321748435828770

sql存在漏洞,使用or拼接sql,使用Statement拼字符串非常容易引发SQL注入的问题,这是因为SQL参数往往是从方法参数传入的。,如果用户的输入是一个精心构造的字符串,就可以拼出意想不到的SQL,这个SQL也是正确的,但它查询的条件不是程序设计的意图。例如:name = "bob' OR pass=", pass = " OR pass='"

SELECT * FROM user WHERE login='bob' OR pass=' AND pass=' OR pass=''

PreparedStatement执行SQL的对象(安全、效率高)

import java.sql.*;

/**
 * @ProjectName: jdbc
 * @PackageName: PACKAGE_NAME
 * @Author: zy7y
 * @Date: 2020/8/26 下午5:38
 * @Description: jdbc
 */
public class TestJdbc01 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        // 1. 加载驱动
        Class.forName("com.mysql.jdbc.Driver");

        // 2. 新建连接
        String url = "jdbc:mysql://192.168.0.222:3306/school?useUnicode=true&characterEncoding=utf8&useSSL=true";
        String username = "root";
        String password = "root";

        // 建立连接 得到操作数据库的对象
        Connection connection = DriverManager.getConnection(url,username,password);

        // 3. 执行sql PreparedStatement 使用?进行占位,代替参数
        String sql = "SELECT * FROM student where studentname = ?";

        // 执行sql的对象,
        PreparedStatement preparedStatement = connection.prepareStatement(sql); // 预编译sql,先写sql不执行

        // 给参数附值(占位符位置,插入的值)
        preparedStatement.setObject(1,"张伟");


        // 执行sql,得到结果集 resultSet
        ResultSet resultSet = preparedStatement.executeQuery();

        while (resultSet.next()){
            System.out.println("id=" + resultSet.getObject("studentno"));

        }
        // 4. 释放连接
        resultSet.close(); // 关闭结果集对象
        preparedStatement.close(); // 关闭执行sql对象
        connection.close(); // 关闭数据库对象
    }
}
posted @ 2020-08-26 21:49  zy7y  阅读(190)  评论(0编辑  收藏  举报