简易的DBUtils工具类

1.properties配置文件

通常情况下,我们习惯使用properties文件,此文件我们将做如下要求:

  1. 文件位置:任意,建议src下
  2. 文件名称:任意,扩展名为properties
  3. 文件内容:一行一组数据,格式是“key=value”。key命名自定义,如果是多个单词,习惯使用点分隔。例如:jdbc.driver。value值不支持中文,如果需要使用非英文字符,将进行unicode转换。

创建配置文件

在项目跟目录下,创建文件,输入“db.properties”文件名。

文件中的内容

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

加载配置文件: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("db.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);
		}
	}
}

使用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);
		}
	}
}

也可用以下方法读取配置问件信息

package cn.itcast.demo1;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

/*
 *  加载properties配置文件
 *  IO读取文件,键值对存储到集合
 *  从集合中以键值对方式获取数据库的连接信息,完成数据库的连接
 */
public class PropertiesDemo {
	public static void main(String[] args) throws Exception{
		//使用类的加载器
		InputStream in = PropertiesDemo.class.getClassLoader().getResourceAsStream("database.properties");
		System.out.println(in);
		Properties pro = new Properties();
		pro.load(in);
		//获取集合中的键值对
		String driverClass=pro.getProperty("driverClass");
		String url = pro.getProperty("url");
		String username = pro.getProperty("username");
		String password = pro.getProperty("password");
		Class.forName(driverClass);
		Connection con = DriverManager.getConnection(url, username, password);
		System.out.println(con);
		
	}
}

读取文件获取连接,执行一次,static{}

package cn.itcast.jdbcutil;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

/*
 *  编写数据库连接的工具类,JDBC工具类
 *  获取连接对象采用读取配置文件方式
 *  读取文件获取连接,执行一次,static{}
 */
public class JDBCUtilsConfig {
	private static Connection con ;
	private static String driverClass;
	private static String url;
	private static String username;
	private static String password;
	
	static{
		try{
			readConfig();
			Class.forName(driverClass);
			con = DriverManager.getConnection(url, username, password);
		}catch(Exception ex){
			throw new RuntimeException("数据库连接失败");
		}
	}
	
	private static void readConfig()throws Exception{
		InputStream in = JDBCUtilsConfig.class.getClassLoader().getResourceAsStream("database.properties");
		 Properties pro = new Properties();
		 pro.load(in);
		 driverClass=pro.getProperty("driverClass");
		 url = pro.getProperty("url");
		 username = pro.getProperty("username");
		 password = pro.getProperty("password");
	}
	
	
	public static Connection getConnection(){
		return con;
	}
	
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值