MySQL之JDBC入门详解

news2024/11/26 18:29:33

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.coloradmin.cn/o/2233866.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

ctfshow——web(总结持续更新)

文章目录 1、基础知识部分2、php伪协议2.1 php://input协议2.2 data://text/plain协议 3、webshell连接工具3.1 蚁剑连接一句话木马 4、各个web中间件重要文件路径4.1 Nginx 5、sqlmap使用6、php特性6.1 md5加密漏洞6.2 php特殊符号 7、TOP 10漏洞7.1 SQL注入7.2 代码执行7.3 文…

数论——约数(完整版)

2、约数 一个数能够整除另一数&#xff0c;这个数就是另一数的约数。 如2&#xff0c;3&#xff0c;4&#xff0c;6都能整除12&#xff0c;因此2&#xff0c;3&#xff0c;4&#xff0c;6都是12的约数。也叫因数。 1、求一个数的所有约数——试除法 例题&#xff1a; 给定…

python: Parent-child form operations using ttkbootstrap

# encoding: utf-8 # 版權所有 2024 ©塗聚文有限公司 # 許可資訊查看&#xff1a;言語成了邀功的功臣&#xff0c;還需要行爲每日來值班嗎&#xff1f; # 描述&#xff1a; 主、子表單 窗體傳值 Parent-child form operations # Author : geovindu,Geovin Du 塗聚文. …

读书笔记#深入理解Java虚拟机(第三版)# Java内存模型与线程

深入理解Java虚拟机&#xff08;第三版&#xff09;# 高效并发 chap12 Java内存模型与线程 概述 在许多场景下&#xff0c;让计算机同时去做几件事情&#xff0c;不仅是因为计算机的运算能力强大了&#xff0c;还有一个很重要的原因是计算机的运算速度与它的存储和通信子系统的…

文心一言 VS 讯飞星火 VS chatgpt (383)-- 算法导论24.5 3题

三、对引理 24.10 的证明进行改善&#xff0c;使其可以处理最短路径权重为 ∞ ∞ ∞ 和 − ∞ -∞ −∞ 的情况。引理 24.10(三角不等式)的内容是&#xff1a;设 G ( V , E ) G(V,E) G(V,E) 为一个带权重的有向图&#xff0c;其权重函数由 w : E → R w:E→R w:E→R 给出&…

阿里云-部署CNI flannel集群网络

环境 1.一台阿里云作为k8s-master:8.130.XXX.231&#xff08;阿里云私有IP&#xff09; 2.Vmware 两个虚拟机分别作为 k8s-node1:192.168.40.131 k8s-node2:192.168.40.131 3.安装Docker 部署过程 k8s-master,k8s-node1,k8s-node2 初始操作 # 关闭防火墙 systemctl stop fi…

「C/C++」C++设计模式 之 抽象工厂模式(Abstract Factory)

✨博客主页何曾参静谧的博客&#x1f4cc;文章专栏「C/C」C/C程序设计&#x1f4da;全部专栏「VS」Visual Studio「C/C」C/C程序设计「UG/NX」BlockUI集合「Win」Windows程序设计「DSA」数据结构与算法「UG/NX」NX二次开发「QT」QT5程序设计「File」数据文件格式「PK」Parasoli…

免费在线绘图:创意与效率的结合

在数字化时代&#xff0c;绘图已成为各行业人士的基本技能。无论你是设计师、学生、创作者还是爱好者&#xff0c;免费的在线绘图软件都是释放创意和表达思想的理想选择。本文将介绍七款功能全面、免费的在线绘图软件&#xff0c;帮助你轻松实现创作愿景。只需网络连接&#xf…

【教程】Git 标准工作流

目录 前言建仓&#xff0c;拉仓&#xff0c;关联仓库修改代码更新本地仓库&#xff0c;并解决冲突提交代码&#xff0c;合入代码其他常用 Git 工作流删除本地仓库和远程仓库中的文件日志打印commit 相关 前言 Git 是日常开发中常用的版本控制工具&#xff0c;配合代码托管仓库…

基于springboot+vue实现的任务管理系统(源码+L文)4-103

第4章 系统设计 4.1 总体功能设计 员工&#xff0c;经理&#xff0c;管理员都需要登录才能进入任务管理系统&#xff0c;使用者登录时会在后台判断使用的权限类型&#xff0c;包括一般使用者和管理者,一般使用者为员工和经理&#xff0c;对员工只能提供任务信息显示查询&…

vue2中使用vue-awesome-swiper实现轮播

swiper官方文档&#xff1a;Swiper中文网-轮播图幻灯片js插件,H5页面前端开发 1.安装 注意&#xff1a;swiper和vue-awesome-swiper的版本一定一定一定要相对应&#xff0c;版本对应如下&#xff1a; Swiper 5-6 vue-awesome-swiper4.1.1(vue2) Swiper 4.x vue-awesome-swi…

Node.js 入门指南:从零开始构建全栈应用

​&#x1f308;个人主页&#xff1a;前端青山 &#x1f525;系列专栏&#xff1a;node.js篇 &#x1f516;人终将被年少不可得之物困其一生 依旧青山,本期给大家带来node.js篇专栏内容:node.js-入门指南&#xff1a;从零开始构建全栈应用 前言 大家好&#xff0c;我是青山。作…

favicon是什么文件?如何制作网站ico图标?

一般我们做网站的话&#xff0c;都会制作一个独特的ico图标&#xff0c;命名为favicon.ico。这个ico图标一般会出现在浏览器网页标题前面。如下图红色箭头所示&#xff1a; 部分博客导航大全也会用到所收录网站的ico图标。比如boke123导航新收录的网站就不再使用网站首页缩略图…

“大跳水”的全新奥迪A3,精准狙击年轻人的心

文/王俣祺 导语&#xff1a;随着传统豪华品牌在国内市场的全面崩盘&#xff0c;奥迪再一次坐不住了。这次&#xff0c;奥迪“割肉”的目标瞄准了被称为“年轻人第一台豪车”的奥迪A3&#xff0c;这款车问世以来&#xff0c;就凭借出色的性能与品质收获了一大批年轻粉丝。如今&a…

两台手机如何提词呢,一台手机后置高清摄像一台手机前置提词+实时监测状态的解决方案来喽

拍视频只会用前置摄像头可不行啊&#xff0c; 后置高清才会更有流量&#xff0c; 你看哦&#xff0c;我用的是后置摄像头拍摄&#xff0c; 然后前面就用来提词&#xff0c; 它不光能提词&#xff0c; 和其他家不一样的是&#xff0c; 还能把后面手机画面投影到前面手机 这样呀&…

[SWPUCTF 2021 新生赛]easy_sql的write up

开启NSSCTF靶场&#xff0c;在浏览器中访问链接&#xff0c;看到让我们输入点什么还有标签页名字提示&#xff1a; "参数是wllm" 直接/?wllm1访问一下&#xff1a; 这里就直接用sqlmap直接爆破了&#xff1a; 查看数据库有哪些&#xff1a; python sqlmap.py -u …

Tr2 CYT2B75使用记录(二):GPIO、串口、CAN(FD)和busoff检测、看门狗和复位原因

目录 概述GPIO串口1.FIFO2.中断 CANFDRX Buffer and FIFO ElementTX Buffer Elementbusoff 检测 看门狗复位原因 待梳理 概述 GPIO 1.如何定位IO的作用 2.读取电平必须为输入模式 串口 熟悉手册串口特性如下&#xff1a; ■ 数据帧大小可从4位编程到16位 ■ STOP位的可编程…

Android13 系统/用户证书安装相关分析总结(二) 如何增加一个安装系统证书的接口

一、前言 接着上回说&#xff0c;最初是为了写一个SDK的接口&#xff0c;需求大致是增加证书安装卸载的接口&#xff08;系统、用户&#xff09;。于是了解了一下证书相关的处理逻辑&#xff0c;在了解了功能和流程之后&#xff0c;发现settings中支持安装的证书&#xff0c;只…

【Java语言】继承和多态(一)

继承 继承就是实现代码的复用&#xff1b;简而言之就是重复的代码作为父类&#xff08;基类或超类&#xff09;&#xff0c;而不同的可以作为子类&#xff08;派生类&#xff09;。如果子类想要继承父类的成员就一定需要extends进行修饰&#xff08;如&#xff1a;&#xff08;…

04_CC2530+Uart串口通信

04_CC2530UART串口通信 串口通信基本概念 串行通信: 数据字节一位位地依次传送的通信方式, 串行通信的速度慢, 但用的传输线条数少, 成本低&#xff0c;适用于远距离的数据传送并行通信: 数据字节的各位同事传送的通信方式, 优点是数据传送速度快, 缺点是占用的传输线条数多,…