JDBC的封装
使用资源文件存储数据库连接信息
- 在包里添加资源文件:
每一行放一个参数,行内有参数名和参数值
mysqlDriver=com.mysql.jdbc.Driver
mysqlURL=jdbc:mysql://localhost:3306/jdbc
mysqlUser=root
mysqlPwd=root
oracleDriver=oracle.jdbc.driver.OracleDriver
oracleURL=jdbc:oracle:thin:@localhost:1521:orcl
oracleUser=scott
oraclePwd=root
前四行为mysql的资源,后四行为oracle的资源
2. 工具类util如下:
package com.first.jdbc;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class util {
static Properties pros = null; //可以帮助读取和处理资源文件中的信息
static //加载util类的时候调用
{
pros = new Properties();
try {
pros.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("jd.properties"));
//读取src资源文件下的内容
} catch (IOException e) {
e.printStackTrace();
}
}
public static Connection getMysqlConn(){
try {
//获得mysql的连接
Class.forName(pros.getProperty("mysqlDriver"));
//pros.getProperty("mysqlDriver")相当于com.mysql.jdbc.Driver
return DriverManager.getConnection(pros.getProperty("mysqlURL"),
pros.getProperty("mysqlUser"),pros.getProperty("mysqlPwd"));
//pros.getProperty("mysqlURL"),
//pros.getProperty("mysqlUser"),pros.getProperty("mysqlPwd")相当于
//"jdbc:mysql://localhost:3306/jdbc","root","root"
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static Connection getOracleConn(){
try {
Class.forName(pros.getProperty("oracleDriver"));
return DriverManager.getConnection(pros.getProperty("oracleURL"),
pros.getProperty("oracleUser"),pros.getProperty("oraclePwd"));
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
//关闭resultSet,statement,connection的封装和重载
public static void close(ResultSet rs,Statement ps,Connection conn){
try {
if(rs!=null){
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(ps!=null){
ps.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(conn!=null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(Statement ps,Connection conn){
try {
if(ps!=null){
ps.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(conn!=null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close(Connection conn){
try {
if(conn!=null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
- 加入工具类后的封装版本:
package com.first.jdbc;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 测试使用JDBCUtil工具类来简化JDBC开发
*
*/
public class Demo11 {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = util.getMysqlConn();
ps = conn.prepareStatement("insert into t_user (username) values (?)");//查询
ps.setString(1, "代红");
ps.execute();
} catch (Exception e) {
e.printStackTrace();
}finally{
util.close(rs, ps, conn);
}
}
}