什么是JDBC?
解释:JDBC全称 Java DataBase Connectivity,是使用java语言操作关系型数据库的一套标准接口,可以适应多种关系型数据库,比如oracle和DB2。
一、执行流程:
- 编写Java代码
- 将内嵌的SQL发送到MySQL服务端
- MySQL服务端接收SQL语句并且执行该SQL语句
- 将SQL语句执行结果返回给Java代码
二、根据执行流程,连接过程为:
1、注册驱动
Class.forname("com.mysql.jdbc.Driver")
2、获取连接
Connection connection=DriverManager.getConnection(url,username,password);
3、定义SQL语句
String sql="select* from xxx";
4、获取执行SQL对象
Statement statement = conn.createStatement();
5、执行SQL
Statement statement=statement.createStatement();
三、涉及到的API
DriverManger:工具类
(1)注册驱动
(2)获取数据库的连接
Statement:
public class Jdbc01 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1.注册驱动,可以省略不写
        Class.forName("com.mysql.jdbc.Driver");
//2.构建数据库连接对象的参数,url,username,password
        String url="jdbc:mysql:///xxx";
        String username="root";
        String password="root";
//3.获取数据库连接对象
        Connection connection = DriverManager.getConnection(url,username,password);
//4.定义SQL语句
        String sql="update xxx set ordered=1111 where id=1";
//5.获取执行SQL对象,执行SQL
        Statement statement = connection.createStatement();
//6.返回结果集
        int resultSet = statement.executeUpdate(sql);
        System.out.println("受影响的行数"+resultSet);
        statement.close();
        connection.close();
    }
}
Connection:数据库连接对象
(1)获取执行SQL对象
- 普通执行SQL对象
 Statement createStatement()
- 预编译SQL的执行SQL对象,防止SQL注入
 PreparedStatement prepareStatement(sql)
- 执行存储过程对象
 CallableStatement prepareCall(sql)
 (2)管理事务
- Mysql中对事物进行控制的语句
 事物开启:
BEGIN/START TRANSA;
事物提交:
COMMIT;
事物回滚:
ROLLBACK;
- 对应JDBC事物管理
 事物开启
setAutoCommit(false|true); true为自动提交事物,false为手动提交事务
事物提交
commit();
事物回滚
rollback();
模拟事物开启,提交以及回滚
public class Jdbc02 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //jdbc的事务管理,开启、提交、回滚,connection支持对事物的管理
        //mysql是默认提交的事务
            Class.forName("com.mysql.jdbc.Driver");
            String url="jdbc:mysql:///xxx";
            String username="root";
            String pasword="root";
            //获取连接
            Connection connection = DriverManager.getConnection(url,username,pasword);
            //定义sql
            String sql1="update tb_xxx set ordered=1111 where id=1";
            String sql2="update tb_ xxx ordered=1111 where id=2";
            //获取执行SQL的对象
            Statement statement = connection.createStatement();
        try {
            //开启事物,true为自动提交事物,false为手动
            connection.setAutoCommit(false);
            int count1 = statement.executeUpdate(sql1);
            int i=3/0;
            System.out.println("影响的行数"+count1);
            int count2 = statement.executeUpdate(sql2);
            System.out.println("影响的行数"+count2);
            //成功的时候进行事物的提交
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            //失败的时候进行回滚事物
            connection.rollback();
        }finally {
//无论成功还是失败,这里都需要关闭相应的资源
            statement.close();
            connection.close();
        }
    }
}
Statement:
(1)执行sql语句
- excuteUpdate 返回的是int类型,操作的行数
- excuteQuery 返回值为结果集
 ResultSet
 (1)获取查询结果,使用游标:返回false,当前行为无效行,没有数据,返回则有效。
 (2)获取数据:getXxx
public class Jdbc03 {
    public static void main(String[] args) throws SQLException {
        String url="jdbc:mysql:///xxx";
        String userName="root";
        String password="root";
        Connection connection = DriverManager.getConnection(url, userName, password);
        String sql="select * from xxx";
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);
        while (resultSet.next()){
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            System.out.println(id);
            System.out.println(name);
        }
        resultSet.close();
        statement.close();
        connection.close();
    }
}
SQL注入
为什么要使用PreparedStatement?
createStatement存在SQL注入问题,preparedStatement可以预防SQL注入问题
SQL注入:SQL注入是通过操作输入来修改事先定义好的SQL语句,用以达到执行代码对服务器进行攻击的方法。
模拟简单的SQL注入:首先新建一张sql表,只有username和password就行,随意插入两条数据。
public class Jdbc04 {
    //模拟SQL注入问题
    public static void main(String[] args) throws SQLException {
       String url="jdbc:mysql:///xxx";
        String username="root";
        String password="root";
        Connection connection = DriverManager.getConnection(url,username,password);
        //模拟登陆,接收到账号和密码
        String name="张三";
        String pwd="' or  '1'='1 ";
        //定义SQL
        String sql="select * from tb_user where username='"+name+"' and password='"+pwd+"'";
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);
        //判断是否登陆成功
        if (resultSet.next()){
            System.out.println("登陆成功");
            System.out.println(sql);
        }else {
            System.out.println("登录失败");
        }
        //释放资源
        resultSet.close();
        statement.close();
        connection.close();
    }
}
结果是登录成功,因为or 1=1恒成立,SQL语句是这样的:
select * from tb_user where username='张三' and password='' or  '1'='1 '
preparedStatement的使用:
(1)获取preparedStatement对象
(2)设置参数值
(3)执行SQL
public class Jdbc05 {
       @Test
    public void jdbcPrepareStatement() throws SQLException {
         String url="jdbc:mysql:///xxx";
        String username = "root";
        String password = "root";
        Connection connection = DriverManager.getConnection(url, username, password);
        String name="张三";
        String pwd="' or  '1'='1 ";
        String sql="select * from tb_user where username=? and password=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,name);
        preparedStatement.setString(2,pwd);
        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next()){
            System.out.println("登陆成功!");
        }  else{
               System.out.println("登录失败!");
           }
        resultSet.close();
        preparedStatement.close();
        connection.close();
       }
}
总结:preparedStatement的性能更好,而且将敏感字符进行转义。
【信息由网络或者个人提供,如有涉及版权请联系COOY资源网邮箱处理】
© 版权声明
本平台(www.cooy.cn)的一切软件、教程及内容信息仅限用于学习和研究,付费仅为收集整理归类费用;
不得将上述内容用于商业或者非法用途,否则一切后果用户自行承担负责。本平台资源、内容、信息均来自来自用户上传,版权争议及其他问题与本平台无关。
您必须在下载后的24个小时之内从您的电脑或手机中彻底删除上述下载内容,如果您喜欢该程序或内容,请支持正版以获取更好的服务。我们非常重视版权问题,如有侵权请发送邮件至下方邮件(655465@qq.com),敬请谅解!
如发现违法违规内容,请联系下方邮箱举报,我们收到后将会第一时间处理。
THE END
    



暂无评论内容