[java]代码库
package com.ruide.db;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import com.ruide.mapper.IMapper;
public class DBManager {
private Connection getConn() throws SQLException {
return DBHelper.cpds.getConnection();
}
public int executeUpdate(String sql,Object[] params){
Connection conn=null;
PreparedStatement pst=null;
try {
conn=getConn();
pst=conn.prepareStatement(sql);
if(params!=null){
for(int i=0;i<params.length;i++){
pst.setObject(i+1, params[i]);
}
}
int i=pst.executeUpdate();
return i;
} catch (SQLException e) {
e.printStackTrace();
}
return -1;
}
public List executeQuery(String sql,IMapper mapper,Object params[]){
Connection conn=null;
PreparedStatement pst=null;
ResultSet rst=null;
try {
conn =getConn();
pst =conn.prepareStatement(sql);
if(params!=null){
for(int i=0;i<params.length;i++){
pst.setObject(i+1, params[i]);
}
}
rst=pst.executeQuery();
return mapper.map(rst);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
[源代码打包下载]