MySQL之JDBC入门详解

01-JDBC入门 

一、JDBC概念

    jdbc : java database connection , java数据库连接

    jdbc是sun公司定义的java程序访问数据库的规范。

二、JDBC操作需要6步

三、入门程序

    1、使用eclipse打开一个新的工作空间

    2、切换到java视图界面

    3、创建java工程:01-jdbc-helloworld

    4、添加mysql驱动包,并解压

    5、创建数据库表:t_user 用户表

    6、创建java包和测试类:UserTest

package com.youzhong.test;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.Date;

public class UserTest {

    public static void main(String[] args) {

        Connection conn = null;

        Statement st = null;

        ResultSet rs = null;

        try {

            // 1、注册驱动

            // 包名+类名 = 全限定名

            Class.forName("com.mysql.jdbc.Driver");

            // 2、创建数据库连接

            // url:数据库连接地址

            // username : 帐号

            // password : 密码

            conn =  DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root");

            // 3、创建SQL执行对象(货车)

            st = conn.createStatement();

            // 4、执行SQL

            rs = st.executeQuery("select * from  t_user");

            // 5、处理结果集

            // ResultSet结果集中有一个游标,刚开始指向第一条记录之前,调用一次next()方法,游标指向下一条记录。

            // 如果next()调用后指向了有效记录,返回true,如果next()调用后没有指向记录,返回false.

            while (rs.next()) {

                // 获取游标指向的这条记录的数据

                // 根据类型获取数据

                int id = rs.getInt(1);// 根据列号获取数据

                // int id = rs.getInt("id");//根据列名获取数据

                String name = rs.getString(2);

                Date birthday = rs.getDate(3);

                System.out.println(id + "\t" +  name + "\t" + birthday);

            }

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            // 6、释放资源  Connection,Statement,ResultSet

            // 资源要保证一定能释放,放在finnaly块中

            try {

                conn.close();

                st.close();

                rs.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

    }

}

02-JDBC-API 

一、什么是API

    API是应用程序接口。

    java的API文档

    API其实就是定义了类,接口,及属性,方法等信息。

二、JDBC的4个API

    java.sql包下的

    1、DriverManager 类

    2、Connection 接口

    3、Statement 接口

    4、ResultSet 接口

03-JDBC的六个步骤 

一、注册驱动

    把驱动类注册到内存中。

Class.forName("com.mysql.jdbc.Driver");

二、创建连接

    Connection代表了数据库连接。

    一个Connection对象就相当于打开了一个数据库连接的CMD窗口。

Connection conn = DriverManager.getConnection(url,username,password);

    url是数据库连接地址: jdbc:mysql://127.0.0.1:3306/test

    

     地址可以简写成:jdbc:mysql:///test ,ip默认是本机,端口默认是3306。

    username是数据库连接的帐号

    password是数据库连接的密码

三、创建SQL执行对象

    Statement是SQL执行对象。

    Statement作用是把SQL从java程序中运送到数据库中执行。相当于一辆货车。

Statement st = conn.createStatement();

四、执行SQL

    执行SQL才是真正的开始运送过程。

ResultSet rs = st.executeQuery(String sql); //select查询语句调用此方法

int count = st.executeUpdate(String sql);//insert,delete,update增删改的语句

//返回的count是增删改语句影响了几条记录

五、处理结果集

    ResultSet是结果集。

    ResultSet结果集内部有一个游标,游标用来指向某一条记录。

    最开始游标指向第一条前面。调用一次rs.next()方法,游标就指向下一条记录。

    如果游标指向了有效记录,next()返回true。如果指到了结果集最后,没有记录了,返回false。

    

while(rs.next()){

    //获取当前记录的数据

    //通过getInt(),getString(),getDate(),getDouble()等方法获取每一列数据

    rs.getXxx(int 列号); // 第1列的列号是1,第2列是2,依此类推

    rs.getXxx(String 列名);

}

    常用的数据库类型与getXxx()方法对照表:

    getDate()只能返回年月日,getTimestamp()可以返回年月日 时分秒。

    getDate(),getTime(), getTimestamp()默认返回的类型全是 java.sql 包下的。

    但是我们可以统一使用 java.util.Date 来接收。(SimpleDateFormat)

六、释放资源

    Connection , Statement , ResultSet 是连接着数据库的资源。

    如果数据库访问完成后资源不释放,将会影响电脑的性能,也影响别人的访问。

    必须释放所有资源。使用 finnally块 可以保证一定释放资源。    

finnally{

    try{

        conn.close();

        st.close();

        rs.close();

    }catch(Exception e){

        e.printStackTrace();

    }

}

04-JDBC-CRUD 

一、什么是CRUD

    CRUD是增删改查,但不仅仅是增删改查。

    除了:查询列表,添加一条,修改一条,删除一条。

    还包括:模糊查询,批量删除,分页查询,查询总数。

二、JDBC完成用户的CRUD

package com.youzhong.test;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.Date;

/**

* 用户的CRUD操作

*

* @author liwei

*

*/

public class UserCRUDTest {

    public static void main(String[] args) {

        

        UserCRUDTest test = new UserCRUDTest();

        

        //1、测试列表

        //test.list();

        

        //2、添加记录

        //test.insert();

        

        //3、修改记录

        //test.update();

        

        //4、删除记录

        //test.delete();

        

        //5、模糊查询

        //test.findNameLike();

        

        //6、批量删除

        //test.deletes();

        

        //7、分页查询

        //test.fenye();

        

        //8、查总数

        test.getTotal();

    }

    // 1、查询列表

    public void list() {

        Connection conn = null;

        Statement st = null;

        ResultSet rs = null;

        try {

            // 1、注册驱动

            Class.forName("com.mysql.jdbc.Driver");

            // 2、创建Connection连接

            conn =  DriverManager.getConnection("jdbc:mysql:///test", "root", "root");

            // 3、创建Statement对象

            st = conn.createStatement();

            // 4、执行SQL

            rs = st.executeQuery("select * from  t_user");

            // 5、处理结果集

            while (rs.next()) {

                int id = rs.getInt(1);

                String name = rs.getString(2);

                Date birthday = rs.getDate(3);

                System.out.println(id + "\t" +  name + "\t" + birthday);

            }

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            // 6、释放资源

            try {

                conn.close();

                st.close();

                rs.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

    }

    // 2、添加记录

    public void insert() {

        Connection conn = null;

        Statement st = null;

        try {

            // 1、注册驱动

            Class.forName("com.mysql.jdbc.Driver");

            // 2、创建Connection连接

            conn =  DriverManager.getConnection("jdbc:mysql:///test", "root", "root");

            // 3、创建Statement对象

            st = conn.createStatement();

            // 4、执行SQL

            int count = st.executeUpdate("insert  into t_user values (null,'张三','2018-08-08')");

            // 5、处理结果集

            System.out.println("添加了"+count+"条记录");

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            // 6、释放资源

            try {

                conn.close();

                st.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

    }

    // 3、修改记录

    public void update() {

        Connection conn = null;

        Statement st = null;

        try {

            // 1、注册驱动

            Class.forName("com.mysql.jdbc.Driver");

            // 2、创建Connection连接

            conn =  DriverManager.getConnection("jdbc:mysql:///test", "root", "root");

            // 3、创建Statement对象

            st = conn.createStatement();

            // 4、执行SQL

            int count = st.executeUpdate("update  t_user set name='杨幂',birthday='2019-05-05'  where id = 9");

            // 5、处理结果集

            System.out.println("修改了"+count+"条记录");

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            // 6、释放资源

            try {

                conn.close();

                st.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

    }

    // 4、删除记录

    public void delete() {

        Connection conn = null;

        Statement st = null;

        try {

            // 1、注册驱动

            Class.forName("com.mysql.jdbc.Driver");

            // 2、创建Connection连接

            conn =  DriverManager.getConnection("jdbc:mysql:///test", "root", "root");

            // 3、创建Statement对象

            st = conn.createStatement();

            // 4、执行SQL

            int count = st.executeUpdate("delete  from t_user where id = 9");

            // 5、处理结果集

            System.out.println("删除了"+count+"条记录");

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            // 6、释放资源

            try {

                conn.close();

                st.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

    }

    // 5、模糊查询

    public void findNameLike() {

        Connection conn = null;

        Statement st = null;

        ResultSet rs = null;

        try {

            // 1、注册驱动

            Class.forName("com.mysql.jdbc.Driver");

            // 2、创建Connection连接

            conn =  DriverManager.getConnection("jdbc:mysql:///test", "root", "root");

            // 3、创建Statement对象

            st = conn.createStatement();

            // 4、执行SQL

            rs = st.executeQuery("select * from  t_user where name like 'x%'");

            // 5、处理结果集

            while (rs.next()) {

                int id = rs.getInt(1);

                String name = rs.getString(2);

                Date birthday = rs.getDate(3);

                System.out.println(id + "\t" +  name + "\t" + birthday);

            }

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            // 6、释放资源

            try {

                conn.close();

                st.close();

                rs.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

    }

    // 6、批量删除

    public void deletes() {

        Connection conn = null;

        Statement st = null;

        try {

            // 1、注册驱动

            Class.forName("com.mysql.jdbc.Driver");

            // 2、创建Connection连接

            conn =  DriverManager.getConnection("jdbc:mysql:///test", "root", "root");

            // 3、创建Statement对象

            st = conn.createStatement();

            // 4、执行SQL

            int count = st.executeUpdate("delete  from t_user where id in (4,6,8)");

            // 5、处理结果集

            System.out.println("删除了"+count+"条记录");

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            // 6、释放资源

            try {

                conn.close();

                st.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

    }

    // 7、分页查询

    public void fenye() {

        Connection conn = null;

        Statement st = null;

        ResultSet rs = null;

        try {

            // 1、注册驱动

            Class.forName("com.mysql.jdbc.Driver");

            // 2、创建Connection连接

            conn =  DriverManager.getConnection("jdbc:mysql:///test", "root", "root");

            // 3、创建Statement对象

            st = conn.createStatement();

            // 4、执行SQL

            //每页2条

            //第1页 :0,2

            //第2页:2,2

            //第3页:4,2

            //6,2

            rs = st.executeQuery("select * from  t_user limit 4,2");

            // 5、处理结果集

            while (rs.next()) {

                int id = rs.getInt(1);

                String name = rs.getString(2);

                Date birthday = rs.getDate(3);

                System.out.println(id + "\t" +  name + "\t" + birthday);

            }

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            // 6、释放资源

            try {

                conn.close();

                st.close();

                rs.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

    }

    // 8、查询总数

    public void getTotal() {

        Connection conn = null;

        Statement st = null;

        ResultSet rs = null;

        try {

            // 1、注册驱动

            Class.forName("com.mysql.jdbc.Driver");

            // 2、创建Connection连接

            conn =  DriverManager.getConnection("jdbc:mysql:///test", "root", "root");

            // 3、创建Statement对象

            st = conn.createStatement();

            // 4、执行SQL

            rs = st.executeQuery("select  count(*) from t_user");

            // 5、处理结果集

            rs.next();

            int total = rs.getInt(1);

            System.out.println("一共有"+total+"条记录");

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            // 6、释放资源

            try {

                conn.close();

                st.close();

                rs.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

    }

}

05-JDBC优化 

一、给CRUD的方法添加参数

    

/**

* 新闻的CRUD操作

*

* @author liwei

*

*/

public class NewsCRUDTest {

    

    public static void main(String[] args) {

        NewsCRUDTest newsTest = new  NewsCRUDTest();

        

        //1、测试列表功能

        //newsTest.list();

        

        //2、添加功能

        //newsTest.insert("濮阳小伙",new  Date(),"濮阳小伙当上了非洲酋长");

        

        //3、修改功能

        //newsTest.update(2,"端午三天",new  Date(),"高速不免费");

        

        //4、删除记录

        newsTest.delete(6);

    }

    // 1、查看列表

    public void list() {

        Connection conn = null;

        Statement st = null;

        ResultSet rs = null;

        try {

            // 1、注册驱动

            Class.forName("com.mysql.jdbc.Driver");

            // 2、创建连接

            conn =  DriverManager.getConnection("jdbc:mysql:///test", "root", "root");

            // 3、创建Sql执行对象

            st = conn.createStatement();

            // 4、执行SQL

            rs = st.executeQuery("select * from  t_news");

            // 5、处理结果集

            while (rs.next()) {

                int id = rs.getInt(1);

                String title = rs.getString(2);

                Date publishTime =  rs.getTimestamp(3);

                //用SimpleDateFormat对java.util.Date进行格式化

                SimpleDateFormat sdf = new  SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

                String content =  rs.getString(4);

                System.out.println(id + "\t" +  title + "\t" + sdf.format(publishTime) + "\t" +  content);

            }

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            // 6、释放资源

            try {

                conn.close();

                st.close();

                rs.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

    }

    // 2、添加记录

    public void insert(String title,Date  publishTime,String content) {

        Connection conn = null;

        Statement st = null;

        try {

            // 1、注册驱动

            Class.forName("com.mysql.jdbc.Driver");

            // 2、创建连接

            conn =  DriverManager.getConnection("jdbc:mysql:///test", "root", "root");

            // 3、创建Sql执行对象

            st = conn.createStatement();

            // 4、执行SQL

            SimpleDateFormat sdf = new  SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            String sql = "insert into t_news  values  (null,'"+title+"','"+sdf.format(publishTime)+"','"+content+"')";

            System.out.println(sql);

            int count = st.executeUpdate(sql);

            // 5、处理结果集

            System.out.println("添加了"+count+"条记录");

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            // 6、释放资源

            try {

                conn.close();

                st.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

    }

    // 3、修改记录

    public void update(int id,String title,Date  publishTime,String content) {

        Connection conn = null;

        Statement st = null;

        try {

            // 1、注册驱动

            Class.forName("com.mysql.jdbc.Driver");

            // 2、创建连接

            conn =  DriverManager.getConnection("jdbc:mysql:///test", "root", "root");

            // 3、创建Sql执行对象

            st = conn.createStatement();

            // 4、执行SQL

            SimpleDateFormat sdf = new  SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            int count = st.executeUpdate("update  t_news set  title='"+title+"',publish_time='"+sdf.format(publishTime)+"',content='"+content+"' where id =  "+id);

            // 5、处理结果集

            System.out.println("修改了"+count+"条记录");

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            // 6、释放资源

            try {

                conn.close();

                st.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

    }

    // 4、删除记录

    public void delete(int id) {

        Connection conn = null;

        Statement st = null;

        try {

            // 1、注册驱动

            Class.forName("com.mysql.jdbc.Driver");

            // 2、创建连接

            conn =  DriverManager.getConnection("jdbc:mysql:///test", "root", "root");

            // 3、创建Sql执行对象

            st = conn.createStatement();

            // 4、执行SQL

            int count = st.executeUpdate("delete  from t_news where id = "+id);

            // 5、处理结果集

            System.out.println("删除了"+count+"条记录");

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            // 6、释放资源

            try {

                conn.close();

                st.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

    }

}

二、定义一个实体类,封装方法的参数,封装列表方法的返回数据

    实体类:项目用到的对应现实世界中物体的类。

                 学生  -> Student类

                 新闻  -> News类

                 商品  -> Goods类

                 订单  -> Order类

                 每一张表对应创建的一个类就是实体类(xxx.xxx.entity)。

                 表  ->  类

                 列  ->  属性

    根据t_news新闻表,就要创建 News 新闻类,放在com.youzhong.entity包下。

public class News {

    private int id;

    private String title;

    private Date publishTime;

    private String content;

    //set , get 方法

    //全参,无参构造方法

    //toString方法

package com.youzhong.test;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.List;

import com.youzhong.entity.News;

/**

* 新闻的CRUD操作

*

* @author liwei

*

*/

public class NewsCRUDTest {

    

    public static void main(String[] args) {

        NewsCRUDTest newsTest = new  NewsCRUDTest();

        

        //1、测试列表功能

        List<News> list = newsTest.list();

        for (News news : list) {

            System.out.println(news);

        }

        

        //2、添加功能

        //newsTest.insert(new News(0,"没啥新闻了",new Date(),"xxx"));

        

        //3、修改功能

        //newsTest.update(new News(7,"还有一条新闻",new Date(),"王老二家的猪丢了"));

        

        //4、删除记录

        //newsTest.delete(6);

    }

    // 1、查看列表

    public List<News> list() {

        List<News> list = new ArrayList<News>();

        Connection conn = null;

        Statement st = null;

        ResultSet rs = null;

        try {

            // 1、注册驱动

            Class.forName("com.mysql.jdbc.Driver");

            // 2、创建连接

            conn =  DriverManager.getConnection("jdbc:mysql:///test", "root", "root");

            // 3、创建Sql执行对象

            st = conn.createStatement();

            // 4、执行SQL

            rs = st.executeQuery("select * from  t_news");

            // 5、处理结果集

            while (rs.next()) {

                int id = rs.getInt(1);

                String title = rs.getString(2);

                Date publishTime =  rs.getTimestamp(3);

                //用SimpleDateFormat对java.util.Date进行格式化

                //SimpleDateFormat sdf = new  SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

                String content =  rs.getString(4);

                //System.out.println(id + "\t" +  title + "\t" + sdf.format(publishTime) + "\t" +  content);

                //把数据封装成News对象

                News news = new  News(id,title,publishTime,content);

                //再把news对象添加到list集合中

                list.add(news);

            }

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            // 6、释放资源

            try {

                conn.close();

                st.close();

                rs.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

        return list;

    }

    // 2、添加记录

    public void insert(News news) {

        Connection conn = null;

        Statement st = null;

        try {

            // 1、注册驱动

            Class.forName("com.mysql.jdbc.Driver");

            // 2、创建连接

            conn =  DriverManager.getConnection("jdbc:mysql:///test", "root", "root");

            // 3、创建Sql执行对象

            st = conn.createStatement();

            // 4、执行SQL

            SimpleDateFormat sdf = new  SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

          String sql = "insert into t_news  values (null,'"+news.getTitle()+"','"+sdf.format(news.getPublishTime())+"','"+news.getContent()+"')";

            System.out.println(sql);

            int count = st.executeUpdate(sql);

            // 5、处理结果集

            System.out.println("添加了"+count+"条记录");

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            // 6、释放资源

            try {

                conn.close();

                st.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

    }

    // 3、修改记录

    public void update(News news) {

        Connection conn = null;

        Statement st = null;

        try {

            // 1、注册驱动

            Class.forName("com.mysql.jdbc.Driver");

            // 2、创建连接

            conn =  DriverManager.getConnection("jdbc:mysql:///test", "root", "root");

            // 3、创建Sql执行对象

            st = conn.createStatement();

            // 4、执行SQL

            SimpleDateFormat sdf = new  SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            int count = st.executeUpdate("update  t_news set  title='"+news.getTitle()+"',publish_time='"+sdf.format(news.getPublishTime())+"',content='"+news.getContent()+"' where id = "+news.getId());

            // 5、处理结果集

            System.out.println("修改了"+count+"条记录");

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            // 6、释放资源

            try {

                conn.close();

                st.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

    }

    // 4、删除记录

    public void delete(int id) {

        Connection conn = null;

        Statement st = null;

        try {

            // 1、注册驱动

            Class.forName("com.mysql.jdbc.Driver");

            // 2、创建连接

            conn =  DriverManager.getConnection("jdbc:mysql:///test", "root", "root");

            // 3、创建Sql执行对象

            st = conn.createStatement();

            // 4、执行SQL

            int count = st.executeUpdate("delete  from t_news where id = "+id);

            // 5、处理结果集

            System.out.println("删除了"+count+"条记录");

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            // 6、释放资源

            try {

                conn.close();

                st.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

    }

}

三、把资源的定义提升为属性

    

package com.youzhong.test;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.List;

import com.youzhong.entity.News;

/**

* 新闻的CRUD操作

*

* @author liwei

*

*/

public class NewsCRUDTest {

    

    private Connection conn = null;

    private Statement st = null;

    private ResultSet rs = null;

    

    public static void main(String[] args) {

        NewsCRUDTest newsTest = new  NewsCRUDTest();

        

        //1、测试列表功能

        List<News> list = newsTest.list();

        for (News news : list) {

            System.out.println(news);

        }

        

        //2、添加功能

        //newsTest.insert(new News(0,"没啥新闻了",new Date(),"xxx"));

        

        //3、修改功能

        //newsTest.update(new News(7,"还有一条新闻",new Date(),"王老二家的猪丢了"));

        

        //4、删除记录

        //newsTest.delete(6);

    }

    // 1、查看列表

    public List<News> list() {

        List<News> list = new ArrayList<News>();

        try {

            // 1、注册驱动

            Class.forName("com.mysql.jdbc.Driver");

            // 2、创建连接

            conn =  DriverManager.getConnection("jdbc:mysql:///test", "root", "root");

            // 3、创建Sql执行对象

            st = conn.createStatement();

            // 4、执行SQL

            rs = st.executeQuery("select * from  t_news");

            // 5、处理结果集

            while (rs.next()) {

                int id = rs.getInt(1);

                String title = rs.getString(2);

                Date publishTime =  rs.getTimestamp(3);

                //用SimpleDateFormat对java.util.Date进行格式化

                //SimpleDateFormat sdf = new  SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

                String content =  rs.getString(4);

                //System.out.println(id + "\t" +  title + "\t" + sdf.format(publishTime) + "\t" +  content);

                //把数据封装成News对象

                News news = new  News(id,title,publishTime,content);

                //再把news对象添加到list集合中

                list.add(news);

            }

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            // 6、释放资源

            try {

                conn.close();

                st.close();

                rs.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

        return list;

    }

    // 2、添加记录

    public void insert(News news) {

        try {

            // 1、注册驱动

            Class.forName("com.mysql.jdbc.Driver");

            // 2、创建连接

            conn =  DriverManager.getConnection("jdbc:mysql:///test", "root", "root");

            // 3、创建Sql执行对象

            st = conn.createStatement();

            // 4、执行SQL

            SimpleDateFormat sdf = new  SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            String sql = "insert into t_news  values  (null,'"+news.getTitle()+"','"+sdf.format(news.getPublishTime())+"','"+news.getContent()+"')";

            System.out.println(sql);

            int count = st.executeUpdate(sql);

            // 5、处理结果集

            System.out.println("添加了"+count+"条记录");

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            // 6、释放资源

            try {

                conn.close();

                st.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

    }

    // 3、修改记录

    public void update(News news) {

        try {

            // 1、注册驱动

            Class.forName("com.mysql.jdbc.Driver");

            // 2、创建连接

            conn =  DriverManager.getConnection("jdbc:mysql:///test", "root", "root");

            // 3、创建Sql执行对象

            st = conn.createStatement();

            // 4、执行SQL

            SimpleDateFormat sdf = new  SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            int count = st.executeUpdate("update  t_news set  title='"+news.getTitle()+"',publish_time='"+sdf.format(news.getPublishTime())+"',content='"+news.getContent()+"' where id = "+news.getId());

            // 5、处理结果集

            System.out.println("修改了"+count+"条记录");

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            // 6、释放资源

            try {

                conn.close();

                st.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

    }

    // 4、删除记录

    public void delete(int id) {

        try {

            // 1、注册驱动

            Class.forName("com.mysql.jdbc.Driver");

            // 2、创建连接

            conn =  DriverManager.getConnection("jdbc:mysql:///test", "root", "root");

            // 3、创建Sql执行对象

            st = conn.createStatement();

            // 4、执行SQL

            int count = st.executeUpdate("delete  from t_news where id = "+id);

            // 5、处理结果集

            System.out.println("删除了"+count+"条记录");

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            // 6、释放资源

            try {

                conn.close();

                st.close();

            } catch (SQLException e) {

                e.printStackTrace();

            }

        }

    }

}

四、封装一个DBUtil工具类,减少重复性代码。

    

//数据库工具类

//封装重复性代码

public class DBUtil {

    // 1、获取数据库连接

    public static Connection getConnection() {

        try {

            // 1、注册驱动 

            Class.forName("com.mysql.jdbc.Driver");

            // 2、创建连接

            Connection conn =  DriverManager.getConnection("jdbc:mysql:///test", "root", "root");

            return conn;

        } catch (Exception e) {

            e.printStackTrace();

        }

        return null;

    }

    // 2、释放资源

    public static void close(Connection  conn,Statement st,ResultSet rs) {

        try {

            if(conn!=null) {

                conn.close();

            }

            if(st!=null) {

                st.close();

            }

            if(rs!=null) {

                rs.close();

            }

        } catch (SQLException e) {

            e.printStackTrace();

        }

    }

}

package com.youzhong.test;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.List;

import com.youzhong.entity.News;

import com.youzhong.util.DBUtil;

/**

* 新闻的CRUD操作

*

* @author liwei

*

*/

public class NewsCRUDTest {

     

     private Connection conn = null;

     private Statement st = null;

     private ResultSet rs = null;

     

     public static void main(String[] args) {

          NewsCRUDTest newsTest = new NewsCRUDTest();

          

          //1、测试列表功能

//        List<News> list = newsTest.list();

//        for (News news : list) {

//            System.out.println(news);

//        }

          

          //2、添加功能

          newsTest.insert(new News(0,"没啥新闻了",new  Date(),"xxx"));

          

          //3、修改功能

          //newsTest.update(new News(7,"还有一条新闻",new  Date(),"王老二家的猪丢了"));

          

          //4、删除记录

          //newsTest.delete(6);

     }

     // 1、查看列表

     public List<News> list() {

          List<News> list = new ArrayList<News>();

          try {

              conn = DBUtil.getConnection();

              // 3、创建Sql执行对象

              st = conn.createStatement();

              // 4、执行SQL

              rs = st.executeQuery("select * from  t_news");

              // 5、处理结果集

              while (rs.next()) {

                   int id = rs.getInt(1);

                   String title = rs.getString(2);

                   Date publishTime = rs.getTimestamp(3);

                   //用SimpleDateFormat对java.util.Date进行格式化

                   //SimpleDateFormat sdf = new  SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

                   String content = rs.getString(4);

                   //System.out.println(id + "\t" + title  + "\t" + sdf.format(publishTime) + "\t" + content);

                   //把数据封装成News对象

                   News news = new  News(id,title,publishTime,content);

                   //再把news对象添加到list集合中

                   list.add(news);

              }

          } catch (Exception e) {

              e.printStackTrace();

          } finally {

              // 6、释放资源

              DBUtil.close(conn, st, rs);

          }

          return list;

     }

     // 2、添加记录

     public void insert(News news) {

          try {

              conn = DBUtil.getConnection();

              // 3、创建Sql执行对象

              st = conn.createStatement();

              // 4、执行SQL

              SimpleDateFormat sdf = new  SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

              String sql = "insert into t_news values  (null,'"+news.getTitle()+"','"+sdf.format(news.getPublishTime())+"','"+news.getContent()+"')";

              System.out.println(sql);

              int count = st.executeUpdate(sql);

              // 5、处理结果集

              System.out.println("添加了"+count+"条记录");

          } catch (Exception e) {

              e.printStackTrace();

          } finally {

              // 6、释放资源

              DBUtil.close(conn,st,rs);

          }

     }

     // 3、修改记录

     public void update(News news) {

          try {

              conn = DBUtil.getConnection();

              // 3、创建Sql执行对象

              st = conn.createStatement();

              // 4、执行SQL

              SimpleDateFormat sdf = new  SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

              int count = st.executeUpdate("update t_news  set  title='"+news.getTitle()+"',publish_time='"+sdf.format(news.getPublishTime())+"',content='"+news.getContent()+"'  where id = "+news.getId());

              // 5、处理结果集

              System.out.println("修改了"+count+"条记录");

          } catch (Exception e) {

              e.printStackTrace();

          } finally {

              // 6、释放资源

              DBUtil.close(conn, st, rs);

          }

     }

     // 4、删除记录

     public void delete(int id) {

          try {

              conn = DBUtil.getConnection();

              // 3、创建Sql执行对象

              st = conn.createStatement();

              // 4、执行SQL

              int count = st.executeUpdate("delete from  t_news where id = "+id);

              // 5、处理结果集

              System.out.println("删除了"+count+"条记录");

          } catch (Exception e) {

              e.printStackTrace();

          } finally {

              // 6、释放资源

              DBUtil.close(conn, st, rs);

          }

     }

}

五、把数据库连接信息编写到 db.properties 文件中

    扩展名/后缀名:.txt , .doc , .jpg , .mp3

    .properties 叫做 属性文件,是java程序特有的一种文件。

    

    

    java.util.Properties类 ,可以读取属性文件中的内容。

    Properties类是Hashtable的子类,Hashtable实现了Map接口。

    Properties也是key-value键值对结构的,key和value固定了是String类型的。

    使用Properties类读取属性文件内容

/**

* 读取属性文件

* @author liwei

*

*/

public class PropertiesTest {

     public static void main(String[] args) throws  IOException {

          //1、创建Properties对象

          Properties props = new Properties();

          

          //2、加载属性文件的字节流

          props.load(PropertiesTest.class.getClassLoader().getResourceAsStream("db.properties"));

          

          //3、获取内容

          String url = props.getProperty("jdbc.url");

          String username =  props.getProperty("jdbc.username");

          String password =  props.getProperty("jdbc.password");

          String hello = props.getProperty("hello");

          

          System.out.println(url);

          System.out.println(username);

          System.out.println(password);

          System.out.println(hello);

     }

}

DBUtil工具类最终版

//数据库工具类

//封装重复性代码

public class DBUtil {

     private static String url;

     private static String username;

     private static String password;

     static {

          try {

              // 1、注册驱动

              Class.forName("com.mysql.jdbc.Driver");

              // 读取db.properties文件

              Properties props = new Properties();

              props.load(DBUtil.class.getClassLoader().getResourceAsStream("db.properties"));

              url = props.getProperty("jdbc.url");

              username =  props.getProperty("jdbc.username");

              password =  props.getProperty("jdbc.password");

          } catch (Exception e) {

              e.printStackTrace();

          }

     }

     // 1、获取数据库连接

     public static Connection getConnection() {

          try {

              // 2、创建连接

              Connection conn =  DriverManager.getConnection(url, username, password);

              return conn;

          } catch (Exception e) {

              e.printStackTrace();

          }

          return null;

     }

     // 2、释放资源

     public static void close(Connection conn, Statement  st, ResultSet rs) {

          try {

              if (conn != null) {

                   conn.close();

              }

              if (st != null) {

                   st.close();

              }

              if (rs != null) {

                   rs.close();

              }

          } catch (SQLException e) {

              e.printStackTrace();

          }

     }

}

六、增删改查功能类的命名规范

    把增删改查的jdbc代码封装到一个类中,起名为 XxxDao 。

    dao : data 数据     access 访问     object 对象

             数据访问对象 -> 访问数据库。

    Dao类放在com.youzhong.dao包下

/**

* 新闻crud操作的类

* @author liwei

*

*/

public class NewsDao {

     private Connection conn = null;

     private Statement st = null;

     private ResultSet rs = null;

     // 1、查看列表

     public List<News> list() {

          List<News> list = new ArrayList<News>();

          try {

              conn = DBUtil.getConnection();

              // 3、创建Sql执行对象

              st = conn.createStatement();

              // 4、执行SQL

              rs = st.executeQuery("select * from  t_news");

              // 5、处理结果集

              while (rs.next()) {

                   int id = rs.getInt(1);

                   String title = rs.getString(2);

                   Date publishTime = rs.getTimestamp(3);

                   // 用SimpleDateFormat对java.util.Date进行格式化

                   // SimpleDateFormat sdf = new  SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

                   String content = rs.getString(4);

                   // System.out.println(id + "\t" +  title + "\t" + sdf.format(publishTime) + "\t"

                   // + content);

                   // 把数据封装成News对象

                   News news = new News(id, title,  publishTime, content);

                   // 再把news对象添加到list集合中

                   list.add(news);

              }

          } catch (Exception e) {

              e.printStackTrace();

          } finally {

              // 6、释放资源

              DBUtil.close(conn, st, rs);

          }

          return list;

     }

     // 2、添加记录

     public void insert(News news) {

          try {

              conn = DBUtil.getConnection();

              // 3、创建Sql执行对象

              st = conn.createStatement();

              // 4、执行SQL

              SimpleDateFormat sdf = new  SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

              String sql = "insert into t_news values  (null,'" + news.getTitle() + "','"

                        +  sdf.format(news.getPublishTime()) + "','" +  news.getContent() + "')";

              System.out.println(sql);

              int count = st.executeUpdate(sql);

              // 5、处理结果集

              System.out.println("添加了" + count + "条记录");

          } catch (Exception e) {

              e.printStackTrace();

          } finally {

              // 6、释放资源

              DBUtil.close(conn, st, rs);

          }

     }

     // 3、修改记录

     public void update(News news) {

          try {

              conn = DBUtil.getConnection();

              // 3、创建Sql执行对象

              st = conn.createStatement();

              // 4、执行SQL

              SimpleDateFormat sdf = new  SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

              int count = st.executeUpdate("update t_news  set title='" + news.getTitle() + "',publish_time='"

              +  sdf.format(news.getPublishTime()) + "',content='" +  news.getContent() + "' where id = "+         news.getId());

              // 5、处理结果集

              System.out.println("修改了" + count + "条记录");

          } catch (Exception e) {

              e.printStackTrace();

          } finally {

              // 6、释放资源

              DBUtil.close(conn, st, rs);

          }

     }

     // 4、删除记录

     public void delete(int id) {

          try {

              conn = DBUtil.getConnection();

              // 3、创建Sql执行对象

              st = conn.createStatement();

              // 4、执行SQL

              int count = st.executeUpdate("delete from  t_news where id = " + id);

              // 5、处理结果集

              System.out.println("删除了" + count + "条记录");

          } catch (Exception e) {

              e.printStackTrace();

          } finally {

              // 6、释放资源

              DBUtil.close(conn, st, rs);

          }

     }

}

06-优化后jdbc开发

项目结构及起名如下图:

StudentDao如下图:

package com.youzhong.dao;

import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.Statement;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.List;

import com.youzhong.entity.Student;

import com.youzhong.util.DBUtil;

public class StudentDao {

    // 三个资源

    private Connection conn = null;

    private Statement st = null;

    private ResultSet rs = null;

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    // 1、列表

    public List<Student> list() {

        List<Student> list = new ArrayList<Student>();

        try {

            conn = DBUtil.getConnection();

            st = conn.createStatement();

            rs = st.executeQuery("select * from t_student");

            while (rs.next()) {

                int id = rs.getInt(1);

                String name = rs.getString(2);

                String sex = rs.getString(3);

                String phone = rs.getString(4);

                Date birthday = rs.getDate(5);

                Student stu = new Student(id, name, sex, phone, birthday);

                list.add(stu);

            }

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            DBUtil.close(conn, st, rs);

        }

        return list;

    }

    // 2、添加

    public void insert(Student student) {

        try {

            conn = DBUtil.getConnection();

            st = conn.createStatement();

            st.executeUpdate("insert into t_student values (null,'" + student.getName() + "','" + student.getSex()

                    + "','" + student.getPhone() + "','" + sdf.format(student.getBirthday()) + "')");

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            DBUtil.close(conn, st, rs);

        }

    }

    // 3、修改

    public void update(Student student) {

        try {

            conn = DBUtil.getConnection();

            st = conn.createStatement();

            st.executeUpdate("update t_student set name='" + student.getName() + "',sex='" + student.getSex()

                    + "',birthday='" + sdf.format(student.getBirthday()) + "' where id = " + student.getId());

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            DBUtil.close(conn, st, rs);

        }

    }

    // 4、删除

    public void delete(int id) {

        try {

            conn = DBUtil.getConnection();

            st = conn.createStatement();

            st.executeUpdate("delete from t_student where id = " + id);

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            DBUtil.close(conn, st, rs);

        }

    }

    // 5、模糊

    public List<Student> nameLike(String mohu) {

        List<Student> list = new ArrayList<Student>();

        try {

            conn = DBUtil.getConnection();

            st = conn.createStatement();

            rs = st.executeQuery("select * from t_student where name like '%" + mohu + "%'");

            while (rs.next()) {

                int id = rs.getInt(1);

                String name = rs.getString(2);

                String sex = rs.getString(3);

                String phone = rs.getString(4);

                Date birthday = rs.getDate(5);

                Student stu = new Student(id, name, sex, phone, birthday);

                list.add(stu);

            }

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            DBUtil.close(conn, st, rs);

        }

        return list;

    }

    // 6、批量删除

    public void deletes(int[] ids) {

        for (int id : ids) {

            delete(id);

        }

    }

    

    // 7、分页查询

    public List<Student> fenye(int start,int size) {

        List<Student> list = new ArrayList<Student>();

        try {

            conn = DBUtil.getConnection();

            st = conn.createStatement();

            rs = st.executeQuery("select * from t_student limit "+start+","+size);

            while (rs.next()) {

                int id = rs.getInt(1);

                String name = rs.getString(2);

                String sex = rs.getString(3);

                String phone = rs.getString(4);

                Date birthday = rs.getDate(5);

                Student stu = new Student(id, name, sex, phone, birthday);

                list.add(stu);

            }

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            DBUtil.close(conn, st, rs);

        }

        return list;

    }

    // 8、查总数

    public int getTotal() {

        try {

            conn = DBUtil.getConnection();

            st = conn.createStatement();

            rs = st.executeQuery("select count(*) from t_student");

            rs.next();

            return rs.getInt(1);

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            DBUtil.close(conn, st, rs);

        }

        return 0;

    }

    // 9、根据id查询

    public Student getById(int id) {

        try {

            conn = DBUtil.getConnection();

            st = conn.createStatement();

            rs = st.executeQuery("select * from t_student where id = "+id);

            if (rs.next()) {

                String name = rs.getString(2);

                String sex = rs.getString(3);

                String phone = rs.getString(4);

                Date birthday = rs.getDate(5);

                Student stu = new Student(id, name, sex, phone, birthday);

                return stu;

            }

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            DBUtil.close(conn, st, rs);

        }

        return null;

    }

}

07-jdbc开发注册登录功能 

注册登录功能是最常见,最基本的功能。

注册的本质:添加功能。

登录的本质:查询功能。

一、注册功能:

    添加帐号,密码。

// 1、注册

     public void zhuce(User user) {

          try {

              conn = DBUtil.getConnection();

              st = conn.createStatement();

              //先查询要注册的帐号是否存在

              String sql1 = "select * from t_user where  username = '"+user.getUsername()+"'";

              rs = st.executeQuery(sql1);

              if(rs.next()) {

                   //如果有记录,说明帐号已存在

                   System.out.println("帐号已存在");

                   return;

              }

              

              //不存在再注册

              String sql2 = "insert into t_user values  (null,'" + user.getUsername() + "','" +  user.getPassword() + "')";

              int count = st.executeUpdate(sql2);

              if (count > 0) {

                   System.out.println("注册成功");

              }

          } catch (Exception e) {

              e.printStackTrace();

          } finally {

              DBUtil.close(conn, st, rs);

          }

     }

二、登录功能

    根据帐号和密码进行查询。

// 2、登录

     public User denglu(User user) {

          try {

              conn = DBUtil.getConnection();

              st = conn.createStatement();

              String sql = "select * from t_user where  username = '"+user.getUsername()+"' and password =  '"+user.getPassword()+"'";

              rs = st.executeQuery(sql);

              //登录查询最多有一条记录

              if(rs.next()) {

                   //查到记录,登录成功

                   int id = rs.getInt(1);

                   String username = rs.getString(2);

                   String password = rs.getString(3);

                   return new User(id,username,password);

              }

          } catch (Exception e) {

              e.printStackTrace();

          }finally {

              DBUtil.close(conn, st, rs);

          }

          return null;

     }

三、上面的登录功能有bug

    万能帐号可以直接登录:' or 2=2  or ''='

    因为万能帐号对登录的sql进行了注入 ->  sql注入。

    sql注入就是传入了一个sql片断,被拼接到了sql字符串中,改变了sql原来的意思。

    

    使用 PreparedStatement 代替 Statement 解决sql注入问题。

    PreparedStatement 也是JDBC的一个API接口。

08-PreparedStatement使用 

一、jdbc的API

    位于java.sql包下

    DriverManager 类     -   驱动管理器

    Connection 接口        -    数据库连接

    Statement 接口         -     SQL执行对象(货车)  

    ResultSet 接口            -   结果集

    PreparedStatement 接口    -    预编译SQL执行对象(防暴汽车)

二、PreparedStatement简介 

    PreparedStatement是Statement的子接口。

    使用PreparedStatement解决登录功能的SQL注入问题:

// 2、登录

     public User denglu(User user) {

          try {

              conn = DBUtil.getConnection();

              // 使用Statement执行的SQL要用字符串拼接,使用PreparedStatement执行的SQL不需要用字符串拼接,参数用?占位

              String sql = "select * from t_user where  username = ? and password = ?";

              // 创建PreparedStatement

              ps = conn.prepareStatement(sql);

              

              //给?占位符赋值

              //第一个参数是?所占的位置

              ps.setString(1, user.getUsername());

              ps.setString(2, user.getPassword());

              

              rs = ps.executeQuery();

              

              // 登录查询最多有一条记录

              if (rs.next()) {

                   // 查到记录,登录成功

                   int id = rs.getInt(1);

                   String username = rs.getString(2);

                   String password = rs.getString(3);

                   return new User(id, username,  password);

              }

          } catch (Exception e) {

              e.printStackTrace();

          } finally {

              DBUtil.close(conn, st, rs);

          }

          return null;

     }

三、PreparedStatement开发步骤:

    1、先写出SQL,参数使用?占位符(?两边不需要加单引号)

String sql = "select * from t_user where username = ? and  password = ?";

    2、使用conn连接对象创建PreparedStatement

         把sql传入,进行预编译(预检查)

PreparedStatement ps = conn.prepareStatement(sql);

    3、给ps对象中的sql占位符赋值

         第1个?号所占的位置是1,第2个问号所占的位置是2.

         赋值的时候,set方法使用的?占位符必须与实际?占位符匹配。

ps.setString(1,"xxx");

ps.setString(2,"yyy");

ps.setInt(?号位置,整数);

ps.setDouble(?号位置,小数);

//日期类型

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

ps.setString(?号位置,sdf.format(new Date()));

    4、使用ps执行SQL

ResultSet rs = ps.executeQuery(); //执行查询

int count = ps.executeUpdate(); //执行增删改

球员项目

package com.youzhong.dao;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.List;

import com.youzhong.entity.Player;

import com.youzhong.util.DBUtil;

public class PlayerDao {

    private Connection conn = null;

    private PreparedStatement ps = null;

    private ResultSet rs = null;

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    // 1、列表

    public List<Player> list() {

        List<Player> list = new ArrayList<Player>();

        try {

            conn = DBUtil.getConnection();

            String sql = "select * from t_player";

            ps = conn.prepareStatement(sql);

            rs = ps.executeQuery();

            while(rs.next()) {

                int id = rs.getInt(1);

                String name = rs.getString(2);

                String teamName = rs.getString(3);

                int num = rs.getInt(4);

                String position = rs.getString(5);

                Date birthday = rs.getDate(6);

                Player player = new Player(id,name,teamName,num,position,birthday);

                list.add(player);

            }

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            DBUtil.close(conn, ps, rs);

        }

        return list;

    }

    // 2、添加

    public void insert(Player player) {

        try {

            conn = DBUtil.getConnection();

            String sql = "insert into t_player values (null,?,?,?,?,?)";

            ps = conn.prepareStatement(sql);

            ps.setString(1, player.getName());

            ps.setString(2, player.getTeamName());

            ps.setInt(3, player.getNum());

            ps.setString(4, player.getPosition());

            ps.setString(5, sdf.format(player.getBirthday()));

            ps.executeUpdate();

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            DBUtil.close(conn, ps, rs);

        }

    }

    // 3、修改

    public void update(Player player) {

        try {

            conn = DBUtil.getConnection();

            String sql = "update t_player set name=?,team_name=?,num=?,position=?,birthday=? where id=?";

            ps = conn.prepareStatement(sql);

            ps.setString(1, player.getName());

            ps.setString(2, player.getTeamName());

            ps.setInt(3, player.getNum());

            ps.setString(4, player.getPosition());

            ps.setString(5, sdf.format(player.getBirthday()));

            ps.setInt(6, player.getId());

            ps.executeUpdate();

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            DBUtil.close(conn, ps, rs);

        }

    }

    // 4、删除

    public void delete(int id) {

        try {

            conn = DBUtil.getConnection();

            String sql = "delete from t_player where id=?";

            ps = conn.prepareStatement(sql);

            ps.setInt(1, id);

            ps.executeUpdate();

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            DBUtil.close(conn, ps, rs);

        }

    }

    // 5、模糊

    public List<Player> nameLike(String mohu) {

        List<Player> list = new ArrayList<Player>();

        try {

            conn = DBUtil.getConnection();

            String sql = "select * from t_player where name like ?";

            ps = conn.prepareStatement(sql);

            ps.setString(1, "%"+mohu+"%");

            rs = ps.executeQuery();

            while(rs.next()) {

                int id = rs.getInt(1);

                String name = rs.getString(2);

                String teamName = rs.getString(3);

                int num = rs.getInt(4);

                String position = rs.getString(5);

                Date birthday = rs.getDate(6);

                Player player = new Player(id,name,teamName,num,position,birthday);

                list.add(player);

            }

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            DBUtil.close(conn, ps, rs);

        }

        return list;

    }

    // 6、批量

    public void deletes(int[] ids) {

        for (int id : ids) {

            delete(id);

        }

    }

    // 7、分页

    public List<Player> fenye(int start,int size) {

        List<Player> list = new ArrayList<Player>();

        try {

            conn = DBUtil.getConnection();

            String sql = "select * from t_player limit ?,?";

            ps = conn.prepareStatement(sql);

            ps.setInt(1, start);

            ps.setInt(2, size);

            rs = ps.executeQuery();

            while(rs.next()) {

                int id = rs.getInt(1);

                String name = rs.getString(2);

                String teamName = rs.getString(3);

                int num = rs.getInt(4);

                String position = rs.getString(5);

                Date birthday = rs.getDate(6);

                Player player = new Player(id,name,teamName,num,position,birthday);

                list.add(player);

            }

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            DBUtil.close(conn, ps, rs);

        }

        return list;

    }

    // 8、查总数

    public int getTotal() {

        try {

            conn = DBUtil.getConnection();

            String sql = "select count(*) from t_player";

            ps = conn.prepareStatement(sql);

            rs = ps.executeQuery();

            rs.next();

            return rs.getInt(1);

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            DBUtil.close(conn, ps, rs);

        }

        return 0;

    }

    // 9、根据id查询

    public Player getById(int id) {

        try {

            conn = DBUtil.getConnection();

            String sql = "select * from t_player where id = ?";

            ps = conn.prepareStatement(sql);

            ps.setInt(1, id);

            rs = ps.executeQuery();

            if(rs.next()) {

                String name = rs.getString(2);

                String teamName = rs.getString(3);

                int num = rs.getInt(4);

                String position = rs.getString(5);

                Date birthday = rs.getDate(6);

                Player player = new Player(id,name,teamName,num,position,birthday);

                return player;

            }

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            DBUtil.close(conn, ps, rs);

        }

        return null;

    }

}

四、PreparedStatement与Statement的区别?

    1、PreparedStatement安全,没有sql注入的风险。Statement不安全,会被SQL注入

    2、PreparedStatement执行的sql是?号占位,不用字符串拼接,更简单。Statement执行的SQL用字符串拼接,容易出错。

    3、PreparedStatement会对SQL进行预编译,速度更快。Statement速度慢。

09-事务 

事务是数据库中一个非常重要的概念。

一、什么是事务

    事务指逻辑上的一组操作,要么全成功,要么全失败,不能有中间状态。

    比如转帐操作:A -> B 转100元

        sql1,A-100元:

update t_card set money = money - 100 where username = 'A';

        sql2,B+100元:

update t_card set money = money + 100 where username = 'B';

二、事务的四大特性:

    1、原子性:这一组操作是一个整体,就像一个原子一样,不可分隔。

    2、一致性:事务提交前和提交后,数据库的数据状态一致。

            A->B转100元    B->A转300元

            A    1000        900                 1200

            B     2000        2100               1800

    3、隔离性:当事务并发执行时,要把每个事务合理的隔离开,不能产生影响

            

                                老妈存钱1000元    1000+1000 = 2000    

    A 帐户1000元

                                 商场消费500元     1000-500 = 500

    把2000存入数据库不对,把500存入数据库也不对。

    应该先执行一件,再执行一件,先存1000,结果是2000,存入数据库。再消费500,用2000-500,结果剩下1500。

        再比如,我们要办两件事,吃饭,上厕所,需要隔离开。

    4、持久性:当事务提交后,数据就永远保存下来。不会因为断电或电脑进水等情况而丢失。(数据存到硬盘上)

三、mysql控制事务的语句(TCL)

     start transaction   开启事务

     commit                 提交事务

     rollback                 回滚事务

START TRANSACTION;

update t_card set money = money - 100 where username = 'A';

asdfasdfasdaasdfs

update t_card set money = money + 100 where username = 'B';

COMMIT;

四、使用JDBC控制事务

    conn.setAutoCommit(false);   开启事务 (关闭自动提交)

    conn.commit();                       提交事务

    conn.rollback();                      回滚事务

// 2、添加

     public void insert(Player player) {

          try {

              conn = DBUtil.getConnection();

              //开启事务

              conn.setAutoCommit(false);

              String sql = "insert into t_player values  (null,?,?,?,?,?)";

              ps = conn.prepareStatement(sql);

              ps.setString(1, player.getName());

              ps.setString(2, player.getTeamName());

              ps.setInt(3, player.getNum());

              ps.setString(4, player.getPosition());

              ps.setString(5,  sdf.format(player.getBirthday()));

              ps.executeUpdate();

              //提交事务

              //conn.commit();

              

              //回滚事务

              conn.rollback();

          } catch (Exception e) {

              e.printStackTrace();

          } finally {

              DBUtil.close(conn, ps, rs);

          }

     }

    使用JDBC事务保证转帐的安全性

public class CardDao {

     private Connection conn = null;

     private PreparedStatement ps = null;

     private ResultSet rs = null;

     private SimpleDateFormat sdf = new  SimpleDateFormat("yyyy-MM-dd");

     // 转帐

     public void zhuanzhang(String username1, String  username2, double money) {

          try {

              conn = DBUtil.getConnection();

              //开事务

              conn.setAutoCommit(false);

              

              String sql1 = "update t_card set money =  money - ? where username = ?";

              ps = conn.prepareStatement(sql1);

              ps.setDouble(1, money);

              ps.setString(2, username1);

              ps.executeUpdate();

              

              //int i = 1/0;//抛异常

              

              String sql2 = "update t_card set money =  money + ? where username = ?";

              ps = conn.prepareStatement(sql2);

              ps.setDouble(1, money);

              ps.setString(2, username2);

              ps.executeUpdate();

              

              //提交事务

              conn.commit();

          } catch (Exception e) {

              //回滚事务

              try {

                   conn.rollback();

              } catch (SQLException e1) {

                   e1.printStackTrace();

              }

              e.printStackTrace();

          } finally {

              DBUtil.close(conn, ps, rs);

          }

     }

}

五、事务的四个隔离级别

    1、读未提交 

    2、读已提交

    3、可重复读

    4、序列化

    上面的四个隔离级别1,2,3,4是由低到高的。

    最低的级别是1读未提交,最高的级别是4序列化。

    MySQL默认隔离级别是 3 可重复读。

    隔离级别越低,两个事务隔离性越差,互相的影响最大,数据越不安全,效率最高。

    隔离级别越高,两个事务隔离性越好,互相的影响最小,数据越安全,效率越低。

    

六、事务不同隔离级别导致的不同问题

1

读未提交 

脏读,不可重复读,幻读

2

读已提交 

不可重复读,幻读

3

可重复读

幻读

4

序列化

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/464280.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Vue3版本的uniapp项目运行至鸿蒙系统

新建Vue3版本的uniapp项目 注意&#xff0c;先将HbuilderX升级至最新版本&#xff0c;这样才支持鸿蒙系统的调试与运行&#xff1b; 按照如下图片点击&#xff0c;快速升级皆可。 通过HbuilderX创建 官方文档指导链接 点击HbuilderX中左上角文件->新建->项目 创建vue3…

html简易流程图

效果图 使用htmlcssjs&#xff0c;无图片&#xff0c;没用Canvas demo: <!DOCTYPE html> <html> <head><link href"draw.css" rel"stylesheet" /><script src"draw.js" type"text/javascript"></…

人工智能原理实验一:知识的表示与推理实验

一、实验目的 本实验课程是计算机、智能、物联网等专业学生的一门专业课程&#xff0c;通过实验&#xff0c;帮助学生更好地掌握人工智能相关概念、技术、原理、应用等&#xff1b;通过实验提高学生编写实验报告、总结实验结果的能力&#xff1b;使学生对智能程序、智能算法等有…

针对解决前后端BUG的个人笔记

1-IDEA Q&#xff1a;Required Java version 17 is not supported by SDK 1.8. The maximum supported Java version is 8. A: 我们只知道IDEA页面创建Spring项目&#xff0c;其实是访问spring initializr去创建项目。故我们可以通过阿里云国服去间接创建Spring项目。将https…

Linux操作系统 ------(3.文本编译器Vim)

目录 1.前言 2.本章学习目标 3.vim的三种工作模式 3.1一般模式‌ 3.2编辑模式‌ 3.3命令行模式‌ 4.运行vim 5.vim 不同工作模式下的常见命令 6.一般模式下的功能键 6.1移动光标类 6.2删除、复制和粘贴类 6.3查找替换类 7.从一般模式进入编辑模式 8.命令行模式下的…

4种鼓励创业创新的方法

随着市场趋于饱和&#xff0c;许多企业&#xff0c;尤其是初创企业&#xff0c;很难在竞争中保持领先地位。技术为企业彻底改变其营销和管理策略铺平了道路。另一个经过实践检验的成功渗透特定市场的方法是在办公室内部激发创新&#xff0c;从员工到品牌皆如此。 那么究竟如何…

【华为HCIP实战课程31(完整版)】中间到中间系统协议IS-IS路由汇总详解,网络工程师

一、IS-IS的汇总 1、可以有效减少在LSP中发布的路由条目,减小对系统资源的占用。 2、会减少LSP报文的扩散,接收到该LSP报文的其他设备路由表中只会出现一条聚合路由。 3、可以避免网络中的路由震荡,提高了网络的稳定性。 4、被聚合的路由可以是IS-IS路由,也可以是被引入…

后端java——如何为你的网页设置一个验证码

目录 1、工具的准备 2.基本方法 3.实现类 4.实践 HTML文件&#xff1a; Java文件1:创建验证码 Java文件2:验证验证码 本文通过HUTOOL实现&#xff1a;Hutool参考文档Hutool&#xff0c;Java工具集https://hutool.cn/docs/#/ 1、工具的准备 如果我们通过hutool来实现这个…

1、Qt6 Quick 简介

一、Qt6 Quick 简介 1、Qt Quick简介 Qt Quick 是 Qt 6 中使用的用户界面技术的总称。它是在 Qt 4 中引入的&#xff0c;现在在 Qt 6 中进行了扩展。Qt Quick 本身是几种技术的集合&#xff1a; QML——用户界面标记语言JavaScript - 动态脚本语言Qt C - 高度可移植的增强型…

微服务系列二:跨微服务请求优化,注册中心+OpenFeign

目录 前言 一、纯 RestTemplate 方案存在的缺陷 二、注册中心模式介绍 三、注册中心技术&#xff1a;Nacos 3.1 Docker部署Nacos 3.2 服务注册 3.3 服务发现 四、代码优化&#xff1a;OpenFeign工具 4.1 OpenFeign快速入门 4.2 连接池的必要性 4.3 抽取服务、最佳实…

andrular输入框input监听值传递

效果图&#xff1a; step1: E:\projectgood\ajnine\untitled4\src\app\apple\apple.component.html <button mat-button (click)“openDialog()”>Open dialog step2: E:\projectgood\ajnine\untitled4\src\app\apple\apple.component.ts import {Component, inject}…

像`npm i`作为`npm install`的简写一样,使用`pdm i`作为`pdm install`的简写

只需安装插件pdm-plugin-i即可&#xff1a; pdm plugin add pdm-plugin-i 然后就可以愉快地pdm i了&#xff0c;例如&#xff1a; git clone https://github.com/waketzheng/fast-dev-cli cd fast-dev-cli python -m pip install --user pipx pipx install pdm pdm plugin a…

qt QTabWidget详解

1、概述 QTabWidget是Qt框架中的一个控件&#xff0c;它提供了一个标签页式的界面&#xff0c;允许用户在不同的页面&#xff08;或称为标签&#xff09;之间切换。每个页面都可以包含不同的内容&#xff0c;如文本、图像、按钮或其他小部件。QTabWidget非常适合用于创建具有多…

关于wordpress instagram feed 插件 (现更名为Smash Balloon Social Photo Feed)

插件地址&#xff1a; Smash Balloon Social Photo Feed – Easy Social Feeds Plugin – WordPress 插件 | WordPress.org China 简体中文 安装后&#xff0c;配置教程&#xff1a; Setting up the Instagram Feed Pro WordPress Plugin - Smash Balloon 从这里面开始看就…

初始JavaEE篇——多线程(5):生产者-消费者模型、阻塞队列

找往期文章包括但不限于本期文章中不懂的知识点&#xff1a; 个人主页&#xff1a;我要学编程程(ಥ_ಥ)-CSDN博客 所属专栏&#xff1a;JavaEE 文章目录 阻塞队列生产者—消费者模型生产者—消费者模型的优势&#xff1a;生产者—消费者模型的劣势&#xff1a; Java标准库中的阻…

用ChatGPT提升工作效率:从理论到实际应用

伴人工智能技术的迅速演进&#xff0c;像ChatGPT这类语言模型已成为提升工作效率的关键工具。这类模型不仅具备处理海量数据的能力&#xff0c;还能自动化许多日常任务&#xff0c;从而提高决策的准确性。本文将深入探讨如何在工作中利用ChatGPT等AI工具提升效率&#xff0c;涵…

如何修改网络ip地址:一步步指南‌

在当今这个数字化时代&#xff0c;网络已成为我们日常生活与工作中不可或缺的一部分。无论是浏览网页、在线办公还是享受流媒体服务&#xff0c;稳定的网络连接和适当的IP地址管理都是确保良好体验的关键。然而&#xff0c;出于隐私保护、绕过地理限制或测试网络环境等需要&…

ENSP (虚拟路由冗余协议)VRRP配置

VRRP&#xff08;Virtual Router Redundancy Protocol&#xff0c;虚拟路由冗余协议&#xff09;是一种用于提高网络可用性和可靠性的协议。它通过在多个路由器之间共享一个虚拟IP地址&#xff0c;确保即使一台路由器发生故障&#xff0c;网络依然能够正常运行&#xff0c;防止…

SpringCloud Alibaba-05 Seata分布式事务处理

一次业务操作需要跨多个数据源或需要跨多个系统进行远程调用&#xff0c;就会产生分布式事务问题。但是关系型数据库提供的能力是基于单机事务的&#xff0c;一旦遇到分布式事务场景&#xff0c;就需要通过更多其他技术手段来解决问题。 1.四大模式&#xff1a; Seata AT模式(主…

非线性数据结构之图

一、有向图&#xff08;Directed Graph&#xff09; 1. 定义 有向图是一个由顶点&#xff08;节点&#xff09;和有方向的边&#xff08;弧&#xff09;组成的图。在有向图中&#xff0c;每条边都有一个起点和一个终点&#xff0c;表示从一个顶点到另一个顶点的关系。 2. 特…