原生JDBC

jdbc

1、简介:

mysql:

安装,卸载,环境变量,SQL

功能自动完成

DDL,DML,DQL,DCL

事务,begin==start transaction ,set autocommit=0/fasle;

commit; rollback; set autocommit=1/true;

事务的传播特性。

视图,索引,触发器,存储过程,函数

给定一个主键,XXX-XX

根据-前面的升序排,后面降序排

2、安装idea maven

注意:你安装完之后,如果jar下载不下来,那么极有可能是你的maven环境没对。

如果你的jar包能下载,在工具里面能看到,在本地仓库也能看到,但是就是在idea无法使用,一运行就报找不到jar包的错误,那么极有可能是你idea环境和maven环境比匹配造成的。

idea可以装多个,自己选用。建议大家使用2020以前的版本,比较稳定可靠,不会有太大问题。2020这个版本经常出一些莫名其妙的问题。

maven版本不要超过3.6,建议3.3这个版本,3.8.1暂时不要用。

idea就是类似eclipse的一款java开发工具。

maven是一个项目构建的工具,主要是帮你管理项目和依赖的jar包的。如果没有maven,我们要用jar包需要自己去网上找,如果有了maven,这些jar包就不用到网上去找了,只需要去找到这些jar包的gav坐标,唯一代表了这个jar文件,maven就会自动从远程仓库去下载这个jar包到你的本地。非常方便。

2.1 idea的安装和使用

见文档

2.2 maven的安装和使用

3、JDBC

java database connecivity

java数据库技术----重要,无论你是多么高级的框架,如果你要操作数据库,就离不开jdbc。

jdbc的核心思想就是设计与实现分离。

设计:java ----- 提出了数据库访问 操作的规范------接口

实现:各个数据库--------实现了这个规范-------------数据库驱动

给我们的启示:

我们写的用的都是java的包,java.sql,javax.sql 这些接口或者类会自动找到驱动的类去执行。

3.1、crud七大步骤:重点

jdbc的增删改:

public class TestUpdate {
    public static void main(String[] args) throws Exception {
        //1、加载驱动--告诉jdbc数据库驱动在哪里
        Class.forName("com.mysql.jdbc.Driver");
        //2、获取连接--建立java应用程序和数据库的连接---通往你家金库的一条路
        //url:不同数据库有不同的写法
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/office", "root", "root");
        //3、编写sql---你想到数据库里面干嘛
        //增
        //String  addSql = "insert into dept values(60,'测试部','北京')";
        //改
        //String updateSql = "update dept set dname = '测量部' where deptNo = 60";
        //删
        String deleteSql = "delete from dept where deptNo = 60";
        //4、获取命令对象
        Statement statement = connection.createStatement();
        //5、执行命令
        //int result = statement.executeUpdate(addSql);
        //int result = statement.executeUpdate(updateSql);
        int result = statement.executeUpdate(deleteSql);
        //6、处理结果
        System.out.println(result);
        //7、关闭资源
        statement.close();
        connection.close();
    }
}

3.2 jdbc的查询

public class TestSelect {
    public static void main(String[] args) throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/office", "root", "root");
        String sql = "select * from emp order by hiredate";
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);//结果集,对应查询出来的虚拟表
        while(rs.next()){//如果有,指针就会指向下一条数据,没有就返回false
            System.out.println(rs.getInt("empNo")+"--"+rs.getString("ename")+"--"+rs.getDate("hiredate"));
        }
        rs.close();
        stmt.close();
        conn.close();
    }
}

3.3 sql注入

public class TestInjection {
    public static void main(String[] args) throws Exception {
        Scanner input = new Scanner(System.in);
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/office","root","root");
        System.out.println("请输入用户名:");
        String name = input.nextLine();
        System.out.println("请输入密码:");
        String password = input.nextLine();
        String sql = "select count(*) as count from emp where ename='"+name+"' and mgr='"+password+"'";
        System.out.println(sql);
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        rs.next();//这个就是结果集的指针往下移动一条
       if(rs.getInt("count")>0){
           System.out.println("登录成功");
       }else{
           System.out.println("登录失败");
       }
       rs.close();
       stmt.close();
       conn.close();
    }
​
}
这样就可能存在注入的风险

解决方案

使用Statement这个接口的子接口:PreparedStatement

Statement的坏处:

1、Statement拼接字符串非常繁琐
2、Statement不防注入
3、Statement执行的sql语句是在执行的时候给的,效率低
  

PreparedStatement好处:

1、使用占位符简化了拼接字符串
2、防止注入
3、预编译提高效率
    代码如下:

public class TestStatement {
    public static void main(String[] args) throws Exception {
        Scanner input = new Scanner(System.in);
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/office","root","root");
        System.out.println("请输入用户名:");
        String name = input.nextLine();
        System.out.println("请输入密码:");
        String password = input.nextLine();
        //这两个问号就是占位符
        String sql = "select count(*) as count from emp where ename=? and mgr=?";
        System.out.println(sql);
        //预编译
        PreparedStatement stmt = conn.prepareStatement(sql);
        stmt.setString(1,name);//从1开始
        stmt.setString(2,password);
        System.out.println(sql);
        //注意这里的执行就不能放sql了
        ResultSet rs = stmt.executeQuery();
        rs.next();//这个就是结果集的指针往下移动一条
       if(rs.getInt("count")>0){
           System.out.println("登录成功");
       }else{
           System.out.println("登录失败");
       }
       rs.close();
       stmt.close();
       conn.close();
    }
​
}
​
​

3.4 模糊查询

演示模糊查询:
  
  public class TestLike {
    public static void main(String[] args) throws Exception {
        Scanner input = new Scanner(System.in);
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/office","root","root");
        System.out.println("请输入用户名:");
        String name = input.nextLine();
        //这两个问号就是占位符
        String sql = "select * from emp where ename like ?";//concat()函数来拼接字符串
        System.out.println(sql);
        //预编译
        PreparedStatement stmt = conn.prepareStatement(sql);
        stmt.setString(1,"%"+name+"%");
        System.out.println(sql);
        //注意这里的执行就不能放sql了
        ResultSet rs = stmt.executeQuery();
      while(rs.next()){
          System.out.println(rs.getString("ename")+"--"+rs.getString("mgr"));
      }
       rs.close();
       stmt.close();
       conn.close();
    }
​
}

3.5 使用java调用存储过程

public class TestProcedure {
    public static void main(String[] args) throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/myschool","root","root");
        CallableStatement cs = conn.prepareCall("{call myprocedure01()}");
        ResultSet rs = cs.executeQuery();
        while(rs.next()){//循环行
            for(int i=0;i<rs.getMetaData().getColumnCount();i++){//循环列
                System.out.print(rs.getObject(i+1)+"  ");
            }
            System.out.println();
        }
        rs.close();
        cs.close();
        conn.close();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值