zoukankan      html  css  js  c++  java
  • Java 学习笔记(16)——Java数据库操作

    数据库操作是程序设计中十分重要的一个部分,Java内置JDBC来操作数据库

    JDBC使用

    JDBC——Java Database connecting Java数据库连接;本质上JDBC定义了操作数据库的一套接口,作为应用程序的开发人员来说只需要创建接口对应的对象即可,而接口的实现由各个数据库厂商去完成。要在应用程序中使用JDBC,需要根据数据库的不同导入对应的jar包。

    使用步骤如下:

    1. 导入相应jar包
    2. 注册驱动
    3. 获取数据库连接对象
    4. 定义sql语句
    5. 获取执行sql语句的对象
    6. 执行sql并获取结果集对象
    7. 从结果集中获取数据
    8. 释放资源

    相关对象的描述

    DriverManager

    在使用JDBC之前需要先注册驱动,也就是告诉JDBC,我们需要导入哪个jar包,这个工作由DriverManager对象来实现,可以调用它里面的方法 registerDriver 来实现,该方法的定义如下:

    static void registerDriver(Driver driver);
    

    这个方法需要传入一个driver 对象,driver对象是具体的数据库厂商来实现,后续相关操作其实是根据这个driver对象来调用相关代码,实现同一套接口操作不同数据库

    我们查阅相关实现类的代码如下:

    public class Driver extends NonRegisteringDriver implements java.sql.Driver {
        //
        // Register ourselves with the DriverManager
        //
        static {
            try {
                java.sql.DriverManager.registerDriver(new Driver());
            } catch (SQLException E) {
                throw new RuntimeException("Can't register driver!");
            }
        }
    
        /**
         * Construct a new driver and register it with DriverManager
         * 
         * @throws SQLException
         *             if a database error occurs.
         */
        public Driver() throws SQLException {
            // Required for Class.forName().newInstance()
        }
    }
    

    在Driver对象中发现,它在静态代码块中执行了registerDriver方法,也就是说我们只要加载对应的类,类就会自动帮助我们进行注册的操作。所以在第一步注册驱动的代码中可以这样写:

    Class.forName("org.mariadb.jdbc.Driver"); //加载对应的Driver类到内存中
    

    Connection对象

    注册了驱动之后就是获取数据库的连接对象,在DriverManager中使用getConnection方法获取,它的定义如下:

    static Connection getConnection(String url); 
    static Connection getConnection(String url, Properties info);
    static Connection getConnection(String url, String user, String password);
    

    上述3个方法中,常用的是第3个,参数分别为: 连接字串、用户名、密码
    连接字串的格式为: jdbc:数据库类型://数据库IP:端口/数据库名称,比如 jdbc:mariadb://localhost:3306/test

    获取连接字串的代码如下:

    Connection conn = DriverManager.getConnection("jdbc:mariadb://localhost:3306/study", "root", "root");
    

    执行sql语句

    获取连接对象之后,需要向数据库传递sql语句并执行它,执行sql语句需要使用对象 Statement, 常用的方法如下:

    boolean execute(String sql);
    ResultSet executeQuery(String sql);
    int executeUpdate(String sql);
    

    一般可以使用execute来执行相关操作,如果是查询语句,可以使用executeQuery来执行并获取返回的结果集,如果需要执行DELTE、UPDATE、INSERT等语句可以使用executeUpdate来更新数据库

    我们可以通过 Connection对象的createStatement方法获取一个Statement对象,代码如下:

    Statement statement = conn.createStatement();
    String strSql = "INSERT INTO student VALUES(2, '2b', 28, 78.9, '2017-12-30', NULL)";
    statement.execute(strSql);
    statement.close(); //最后别忘了关闭对象
    

    获取返回结果

    如果我们执行了像insert、delete、update等等语句,可能不需要关注具体的返回结果,但是如果使用的是select语句,则需要获取返回的结果

    获取select语句返回的结果可以使用 executeQuery 方法,该方法会返回一个结果集对象

    可以将结果集对象想象成一个二维的数组,保存了查询到的相关数据,每一行代表一条数据,行中的每一列是一个字段的数据。结果集中使用游标来遍历每一行数据。使用get相关函数来获取对应索引的数据。一行遍历完了使用next移动到下一行;其中get相关方法主要有:

    Blob getBlob(int columnIndex); 
    Blob getBlob(String columnLabel);
    boolean getBoolean(int columnIndex);
    boolean getBoolean(String columnLabel);
    byte getByte(int columnIndex); 
    byte getByte(String columnLabel);
    byte[] getBytes(int columnIndex);
    byte[] getBytes(String columnLabel);
    Date getDate(int columnIndex); 
    Date getDate(int columnIndex, Calendar cal); 
    Date getDate(String columnLabel); 
    Date getDate(String columnLabel, Calendar cal);
    double getDouble(int columnIndex);
    double getDouble(String columnLabel);
    float getFloat(int columnIndex);
    float getFloat(String columnLabel);
    int getInt(int columnIndex);
    int getInt(String columnLabel);
    long getLong(int columnIndex);
    long getLong(String columnLabel);
    

    在获取了结果之后需要关闭对应对象清理资源,这部分只需要调用对应的cloase方法即可

    最终一个完整的demo 如下:

    public class JDBCDemo1 {
        public static void main(String[] args) {
            Connection conn = null;
            Statement statement = null;
            ResultSet resultSet = null;
            try {
                Class.forName("org.mariadb.jdbc.Driver");
                conn = DriverManager.getConnection("jdbc:mariadb://localhost:3306/test", "root", "root");
                String sql = "select * from student";
                statement = conn.createStatement();
                resultSet = statement.executeQuery(sql);
    
                while (resultSet.next()){
                    int id = resultSet.getInt(1); //注意:这里面的索引是从1开始的
                    String name = resultSet.getString(2);
                    int age = resultSet.getInt(3);
                    double score = resultSet.getDouble(4);
                    Date birthday = resultSet.getDate(5);
                    Timestamp insertTime = resultSet.getTimestamp(6);
    
                    System.out.println(id + "	" + name + "	" + age + "	" + score + "	" + birthday + "	" + insertTime);
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                try{
                    if (resultSet != null){
                        resultSet.close();
                    }
    
                    if (statement != null){
                        statement.close();
                    }
                    if (conn != null){
                        conn.close();
                    }
                }catch (SQLException e){
                    e.printStackTrace();
                }
            }
    
        }
    }
    

    参数化查询

    我们知道使用sql拼接的方式来执行sql语句容易造成sql注入漏洞,即使针对某些关键字进行过滤也很难消除这个漏洞,一旦存在sql注入,那么数据库中的数据很容易就会被黑客窃取。而使用参数化查询的方式可以从根本上消除这个漏洞。

    jdbc中参数化查询使用的对象是 PreparedStatement, 它与Statement对象不同在于,它会提前将sql语句进行编译,后续只会接收固定类型的参数;而Statement只会简单的去执行用户输入的sql语句。

    在进行参数化查询的时候需要先准备sql语句,但是在查询参数中需要使用 ? 做标记,表示这个位置是一个参数,后续在真正执行前再传入,比如说可以准备这样的sql语句 update student set score = 100 where name = ?

    准备好sql语句之后,需要设置对应参数位置的值,我们可以使用 setXxx 方法来设置,setXxx 方法与之前介绍的get方法类似,根据不同的数据类型 Xxx 有不同的取值。

    设置完参数之后,与Statement 一样,调用对应的execute方法来执行即可.

    String sql = "update student set score = 100 where name = ?";
    ps = conn.prepareStatement(sql);
    ps.setString(1, "2b");
    ps.executeUpdate();
    

    数据库连接池

    在需要频繁操作数据库的应用中,使用数据库连接池技术可以对数据库操作进行一定程度的优化。原理请自行百度。

    如果要自己实现数据库连接池需要实现 javax.sql.DataSource 的getConnection方法。当然我学习Java只是为了学习一下Web相关的内容,并不想太过于深入,所以自然不会去管怎么实现的,只要调用第三方实现,然后使用就好了。

    常见的开源的第三方库有: Apache commons-dbcp、C3P0 、Apache Tomcat内置的连接池(apache dbcp)、druid(由阿里巴巴提供)。

    本着支持国产的心态,这次使用的主要是 druid。

    druid 连接池需要提供一个配置文件来保存数据库的相关内容

    driverClassName=org.mariadb.jdbc.Driver
    url=jdbc:mariadb://localhost:3306/study
    username=root
    password=masimaro_1992
    # 初始化时连接池中保留连接数
    initialSize=5
    # 最大连接数
    maxActive=10
    # 最大时间,超过这个时间没有任何操作则会关闭连接
    maxWait=3000
    

    在使用时主要需要如下步骤:

    1. 加载配置文件
    2. 调用 DruidDataSourceFactory.createDataSource 方法传入 配置,获取到 DataSource 对象
    3. 调用DataSource.getConnection 方法获取Connection 对象
    4. 执行后续操作

    相关代码如下:

    Connection conn = null;
    Statement statement = null;
    
    Properties properties = new Properties();
    try {
        properties.load(JDBCDemo3.class.getResourceAsStream("druid.properties"));
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        conn = dataSource.getConnection();
        statement = conn.createStatement(); 
    
        //do something
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    

  • 相关阅读:
    [BZOJ3997][TJOI2015]组合数学(Dilworth定理+DP)
    [BZOJ4000][TJOI2015]棋盘(状压DP+矩阵快速幂)
    BZOJ2462[Beijing2011]矩阵模板(二维Hash)
    [BZOJ2458][BeiJing2011]最小三角形(分治)
    [HDU5354]Bipartite Graph(CDQ分治+并查集)
    [NOIP2017]时间复杂度(模拟)
    [Luogu2540][NOIP2016]斗地主增强版(搜索+DP)
    [Luogu1979][NOIP2013]华容道(BFS+SPFA)
    WQS二分题集
    [CC-XXOR]Chef and Easy Problem
  • 原文地址:https://www.cnblogs.com/lanuage/p/11409060.html
Copyright © 2011-2022 走看看