jdbc封装(其中一种):
·创建数据库连接工具类JdbcUtil.java,将数据库参数写在配置文件jdbc.properties中
·dao(数据访问)层创建BaseDao.java类,该类是所有数据访问层进行增删改查操作类的父类
·配置文件jdbc.properties
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/数据库名?useUnicode=true&characterEncoding=utf-8
username=root
password=admin
·解析配置文件工具类ConfigManager.java
/**
* @author 落俗
*解析配置文件
*/
public class ConfigManager {
private static Properties props = null;
static{
InputStream is = null;
is = ConfigManager.class.getClassLoader().getResourceAsStream("jdbc.properties");
if(is == null){
throw new RuntimeException("文件读取失败");
}
props = new Properties();
try{
props.load(is);
}catch (IOException e){
e.printStackTrace();
}finally{
try{
is.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
public static String getProperty(String key){
return props.getProperty(key);
}
}
·数据库连接工具类JdbcUtil.java
/**
* @author 落俗
*
*/
public class JdbcUtils {
//读取配置文件
private static String driver = ConfigManager.getProperty("driverClass");
private static String url = ConfigManager.getProperty("url");
private static String username = ConfigManager.getProperty("username");
private static String password = ConfigManager.getProperty("password");
private static Connection con = null;
//静态代码块加载数据库驱动
static{
try{
Class.forName(driver);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
//打开数据库连接
public static Connection getConnection(){
//Connection con = null;
try {
if(con == null || con.isClosed()){
con = DriverManager.getConnection(url,username,password);
}else{
return con;
}
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
/**
* 关闭连接
* @param con
* @param pstmt
* @param rs
*/
public static void closeAll(Connection con, PreparedStatement pstmt,ResultSet rs){
try {
if(rs != null && !rs.isClosed()){
rs.close();
}
if(pstmt != null && !pstmt.isClosed()){
pstmt.close();
}
if(con != null && !con.isClosed()){
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
·数据访问层的父类BaseDao.java
public class BaseDao {
private Connection conn = null;
public BaseDao(Connection conn){
this.conn = conn;
}
/**
* 增删改操作
* @param sql
* @param params
* @return (1)数据操作语句DML的行数(2)对于无返回内容的sql语句返回0
*/
public int executeUpdate(String sql, Object...params){ //Object...params 可变长的Object类型的数组
int result = 0;
PreparedStatement pstmt = null;//执行sql
try{
pstmt = conn.prepareStatement(sql);
if(params != null){
for(int i = 0; i < params.length; i++){
pstmt.setObject(i+1, params[i]);
//等价于pstmt.set类型(i+1, xxx);
}
}
result = pstmt.executeUpdate();//执行操作
}catch(SQLException e){
e.printStackTrace();
}finally{
JdbcUtils.closeAll(null, pstmt, null);
}
return result;
}
/**
* 查询操作,最后不关闭rs和pstmt
* @param sql
* @param params
* @return
*/
public ResultSet executeQuery(String sql, Object...params){
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = conn.prepareStatement(sql);
if(params != null){
for(int i = 0; i < params.length; i++){
pstmt.setObject(i+1, params[i]);
}
}
rs = pstmt.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
}