原⽣jdbc与预处理对象PreparedStatment 4

JDBC⼯具类

“获得数据库连接”操作,将在以后的增删改查所有功能中都存在,可以封装⼯具类JDBCUtils。提供获取连接对象的⽅法,从⽽达到代码的重复利⽤。

该⼯具类提供⽅法: public static Connection getConnection() 。代码如下:

  • jdbc.properties⽂件(保存在src同级别的resources目录下,并将resources目录右键-->Make Directory as --> Test Resoyrces Root)
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test_wensong?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
jdbc.user=root
jdbc.password=root
  • JDBC⼯具类(src目录下即可)
package JDBCTest;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * JDBC 工具类
 */
public class JDBCUtils {
    private static String driver;
    private static String url;
    private static String user;
    private static String password;
    static {try {
        //使⽤类加载器, 读取配置⽂件
        // 使用ClassLoader加载properties配置文件生成对应的输入流
        //该方式只能读取类路径下的配置文件,有局限但是如果配置文件在类路径下比较方便。
        InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties prop = new Properties();
        // 使用properties对象加载输入流
        prop.load(is);
        //获取key对应的value值
        driver = prop.getProperty("jdbc.driver");
        url = prop.getProperty("jdbc.url");
        user = prop.getProperty("jdbc.user");
        password = prop.getProperty("jdbc.password");
        //注册驱动
        Class.forName(driver);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    }
    /**
     * 返回连接对象 Connection
     */
    public static Connection getConnection() throws SQLException {
        Connection conn = DriverManager.getConnection(url, user, password);
        return conn;
    }
    /**
     * 释放资源
     */
    public static void close(ResultSet rs, Statement stat, Connection
            conn) throws SQLException {
        if (rs != null) {
            rs.close();
        }
        if (stat != null) {
            stat.close();
        }
        /*看Connection来⾃哪⾥,
        如果Connection是从连接池⾥⾯获得的, close()⽅法其实是归还;
        如果Connection是创建的, 就是销毁
         */
        if (conn != null) {
            conn.close();
        }
    }
}

补充:

Properties类的重要方法
Properties 类存在于胞 Java.util 中,该类继承自 Hashtable
1. getProperty ( String  key) ,   用指定的键在此属性列表中搜索属性。也就是通过参数 key ,得到 key 所对应的 value。
2. load ( InputStream  inStream) ,从输入流中读取属性列表(键和元素对)。通过对指定的文件(比如说上面的 jdbc.properties 文件)进行装载来获取该文件中的所有键 - 值对。以供 getProperty ( String  key) 来搜索。
3. setProperty ( String  key, String  value) ,调用 Hashtable 的方法 put 。他通过调用基类的put方法来设置 键 - 值对。 
4. store ( OutputStream  out, String  comments) ,   以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。
5. clear () ,清除所有装载的 键 - 值对。该方法在基类中提供。

 

使用JDBC⼯具类 完成查询

package JDBCTest;

import org.junit.Test;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCTest {
    @Test
    public void JDBCTest() throws SQLException {
        /**
         * 使⽤JDBC⼯具类, 完成查询所有分类
         * 1.通过JDBC⼯具类, 获得连接
         * 2.获得执⾏sql语句的对象
         * 3.执⾏sql语句, 并返回结果
         * 4.处理结果
         * 5.释放资源
         */
        //通过JDBC工具类,获取连接
        Connection conn =JDBCUtils.getConnection();
        //执行查询语句
        Statement stat = conn.createStatement();
        ResultSet rs =stat.executeQuery("select * from tb_student");
        //遍历
        while(rs.next()){
            //获取name,age
            int age = rs.getInt("age");
            String name = rs.getString("name");
            System.out.println("student age: "+age+" name: "+name);
        }
        //释放资源
        JDBCUtils.close(rs,stat,conn);
    }

}

猜你喜欢

转载自blog.csdn.net/weixin_40959890/article/details/107746568