5.0 JDBC工具类与Properties配置文件

**

5.1 未优化的工具类

**
因为每次操作都要重复的进行连接和关闭数据库的重复任务,为了避免,使用工具类。
“获得数据库连接”操作,将在以后的增删改查所有功能中都存在,可以封装工具类JDBCUtils。提供获取连接对象的方法,从而达到代码的重复利用。
该工具类提供方法:public static Connection getConn ()。代码如下:

/*
 * JDBC工具类
 */
public class JDBCUtils {
	public static final  String DRIVERNAME = "com.mysql.jdbc.Driver";
	public static final  String URL = "jdbc:mysql://localhost:3306/mydb";
	public static final  String USER = "root";
	public static final  String PASSWORD = "root";
// 上述变量都做成了静态最终
	static {
		try {
			Class.forName(DRIVERNAME);
		} catch (ClassNotFoundException e) {
			System.out.println("数据库驱动注册失败!");
		}
	}
	//提供获取连接的方法
	public static Connection getConnection() throws Exception {
		// 2. 获得连接
		Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
		// 返回连接
		return conn;
	}
}

**

2.使用properties配置文件

**
在项目跟目录下,创建文件,输入“jdbc.properties”文件名。
文件中的内容

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mydb
user=root
password=root

**

3.加载配置文件:Properties对象

**

对应properties文件处理,开发中也使用Properties对象进行。我们将采用加载properties文件获得流,然后使用Properties对象进行处理。
JDBCUtils.java中编写代码
public class JDBCUtils {

	private static String driver;
	private static String url;
	private static String user;
	private static String password;
	// 静态代码块
	static {
		try {
			// 1 使用Properties处理流
			// 使用load()方法加载指定的流
			Properties props = new Properties();
			Reader is = new FileReader("jdbc.properties");
			props.load(is);
			// 2 使用getProperty(key),通过key获得需要的值,
			driver = props.getProperty("driver");
			url = props.getProperty("url");
			user = props.getProperty("user");
			password = props.getProperty("password");
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 获得连接
	 */
	public static Connection getConnection() {
		try {
			// 1 注册驱动
			Class.forName(driver);
			// 2 获得连接
			Connection conn = DriverManager.getConnection(url, user, password);
			return conn;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}

**

4.使用JDBCUtils工具类

**
//测试类

public class Demo {
	@Test
	public void insert(){
		try{
			//1,获取连接对象
			Connection conn = JDBCUtils.getConnection();
			//2,指定要执行的SQL语句
			String sql = "INSERT INTO zhangwu(name,money,parent) VALUES(?,?,?)";
			//4,获取SQL语句的执行对象 PreparedStatement
			PreparedStatement ppstat = conn.prepareStatement(sql);
			//5,执行SQL语句
			ppstat.setString(1, "股票收入");
			ppstat.setDouble(2, 5000);
			ppstat.setString(3, "收入");
			int line = ppstat.executeUpdate();
			//6,处理结果集
			System.out.println("line=" + line);
			//7,关闭连接
			ppstat.close();
			conn.close();
		} catch(SQLException e){
			throw new RuntimeException(e);
		}
	}
}

**

5.优化后的JDBCUtils工具类(最终版本)

**
将关闭资源和关闭连接也写进配置文件里。

使用配置文件后的:
mport java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * 
 * @Description 操作数据库的工具类
 * @version
 * @date 上午9:10:02
 *
 */
public class JDBCUtils {
	
	/**
	 * 
	 * @Description 获取数据库的连接
	 * @author shkstart
	 * @date 上午9:11:23
	 * @return
	 * @throws Exception
	 */
	public static Connection getConnection() throws Exception {
		// 1.读取配置文件中的4个基本信息
		InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");

		Properties pros = new Properties();
                // Reader is = new FileReader("jdbc.properties");也可以比使用InputStream更简单。
		pros.load(is);

		String user = pros.getProperty("user");
		String password = pros.getProperty("password");
		String url = pros.getProperty("url");
		String driverClass = pros.getProperty("driverClass");

		// 2.加载驱动
		Class.forName(driverClass);

		// 3.获取连接
		Connection conn = DriverManager.getConnection(url, user, password);
		return conn;
	}
	/**
	 * 
	 * @Description 关闭连接和Statement的操作 
	 * @author shkstart
	 * @date 上午9:12:40
	 * @param conn
	 * @param ps
	 */
	public static void closeResource(Connection conn,Statement ps){
		try {
			if(ps != null)
				ps.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			if(conn != null)
				conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 
	 * @Description 关闭资源操作
	 * @author shkstart
	 * @date 上午10:21:15
	 * @param conn
	 * @param ps
	 * @param rs
	 */
	public static void closeResource(Connection conn,Statement ps,ResultSet rs){
		try {
			if(ps != null)
				ps.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			if(conn != null)
				conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			if(rs != null)
				rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值