JDBC学习(一)

一、JDBC是什么

1. JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。JDBC提供了一种基准,据此可以构建更高级的工具和接口,使数据库开发人员能够编写数据库应用程序。

2.JDBC就是Java与数据库之间的连接,通过Mysql提供的Jar文件,使得Java与Mysql完成连接,可以使用Java语言直接操作Mysql中的表文件。

二、JDBC的提前工作

1.完成Java环境配置,即JRE,继承开发环境的配置。我使用的是Jdk-1.8.0

2.完成Mysql的安装与配置。mysql-8.0.52

3.选择Mysql连接jar包,我这里使用的是mysql-connector-java-8.0.15.jar,本jar包用于Mysql8.0以后的高版本,值的注意的是,在我们经常使用的类中Driver位置变成了"com.mysql.jc.jdbc.Driver"

将jar文件加入到lib文件夹中,添加jar文件,就完成了前提工作。

三、创建数据库连接

完成前面操作以后,我们需要完成数据库的连接,本操作等同于你使用Mysql进入到自己设定的连接中。

Class.forName("com.mysql.jc.jdbc.Driver");     //加载Driver驱动
String url="jdbc:mysql://localhost:3306/emps"   //设置url地址
String username="root"; String password="123";   //设置用户名与密码

Connection conn=DriverManger.getConnection(url,username,password) //通过之前的设置获取到连接

在完成连接之间容易出现的问题就是url地址问题,比如编码不对应等问题存在。

另外,由于获得连接这部分操作需要经常使用且不会因为你的使用而改变,所以我们将这部分代码写入DBUtils工具类中。

四、Statement类与PrepareStatement类

1.Statement对象:使用Connection连接获得该对象,是直接完成表格操作语句的对象,

内置方法:execute(),执行方法,直接执行statement中的sql语句,返回一个boolean参数用于表示是否执行成功(但不推荐使用)

                  executeQuery(),执行查询方法,用于查询语句的执行,会返回一个ResultSet的一个结果集合。

                  executeUpdate,执行方法,常用于删改查操作,会返回一个int类型的值,用于表示有几行发生变化。

2.PreparedStatement对象,与Statement方法类似,但是支持预处理机制,实用性比statement强,安全性也更强。setString,setInt等方法用于设置预处理的值。

五.查询语句


        Connection conn = DBUtil.getConnection();  //通过工具类完成连接的获取   
        String sql="select * from emp";          //设置sql语句
        ResultSet rs = null;                    //由于excuteQuery方法返回值为一个ResultSet对象,所以,我们创建一个空对象,写在外面是为了便于资源的释放
        try {                                          //sql语句的执行需要手动完成异常处理,抛出SQLException异常
            Statement statement = conn.createStatement(sql);     //获得使用Connection的内置方法获得Statement对象,该对象用于数据库表格的直接操作
rs = statement.executeQuery(); // while (rs.next()) { //遍历ResultSet集合 System.out.print(rs.getString(1)); //获得每一个中的值 System.out.print(rs.getString(2)); System.out.print(rs.getString(3)); System.out.println(); } }catch(Exception e){ System.out.println("执行错误"); }finally{ DBUtil.closeRourse(conn,null,rs); //程序结束时,关闭Connection,Statement,ResultSet } }
  public static void select(String job) throws Exception{   //PrepareStatement执行

        Connection conn = DBUtil.getConnection();
        System.out.println(conn);
        String sql="select * from emp where job=? or job=?";
        ResultSet rs = null;
        try {
            PreparedStatement statement = conn.prepareStatement(sql);
            statement.setString(1,job);                  //设置预处理的值
            statement.setString(2,"董事长");
            rs = statement.executeQuery();
            while (rs.next()) {
                System.out.print(rs.getString(1));
                System.out.print(rs.getString(2));
                System.out.print(rs.getString(3));
                System.out.println();
            }
        }catch(Exception e){
            System.out.println("执行错误");
        }finally{
           // DBUtil.closeRourse(conn,null,rs);
        }
    }

五、插入语句

public static void insert(String str) throws Exception{
        Connection conn =DBUtil.getConnection();
        String sql="insert into table_a(`b`) values(?)";
        PreparedStatement ps=null;
        try {
            ps= conn.prepareStatement(sql);
            ps.setString(1,str);
            int len=ps.executeUpdate();
            if(len>0){
                System.out.println("插入成功");
            }else{
                System.out.println("插入失败");
            }
        }catch(Exception e){
            System.out.println("执行错误");
            e.printStackTrace();
        }
    }

六、删除语句

public static void delete(String str) throws Exception{
        Connection conn =DBUtil.getConnection();
        String sql="delete from table_a where b=?";
        PreparedStatement ps=null;
        try {
            ps= conn.prepareStatement(sql);
            ps.setString(1,str);
            int len=ps.executeUpdate();
            if(len>0){
                System.out.println("删除成功");
            }else{
                System.out.println("删除失败");
            }
        }catch(Exception e){
            System.out.println("执行错误");
            e.printStackTrace();
        }
    }

七、修改语句

public static void update(String str,String new) throws Exception{
        Connection conn =DBUtil.getConnection();
        String sql="update table_a set b=? where a=? ";
        PreparedStatement ps=null;
        try {
            ps= conn.prepareStatement(sql);
            ps.setString(1,new);
ps.setString(2,str);
int len=ps.executeUpdate(); if(len>0){ System.out.println("修改成功"); }else{ System.out.println("修改失败"); } }catch(Exception e){ System.out.println("执行错误"); e.printStackTrace(); } }
原文地址:https://www.cnblogs.com/qqwhsj/p/10785236.html