jdbc封装

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Diligent_Programmer/article/details/78912993

封装主要为了方便后续直接使用,不同的用户不同的数据库,通过修改配置文件就能使用同一段代码


核心代码


一、主要的方法

1.导包

2.加载驱动(在整个程序运行只用加载一次)

Static {
Class.forName("ReaderPro.getInstance().getProperty("driverClassName")");
}

3.建立连接的方法(不希望外部直接修改)

private Connection getConnection(){
Connection con=null;
con=DriverManager("ReaderPro.getInstance().getProperty("url"),
ReaderPro.getInstance().getProperty("user"),
ReaderPro.getInstance().getProperty("password"));
return con;//***********
}

4.给预编译语句赋值的方法

private void setParams(PreparedStatement pstmt,Object ... params){
if( params!=null && params.length>0 ){   // params.length>0 ********
pstmt.getObject(i+1,params[i]);//注意类型和参数
}
}

5.关闭资源的方法

private void closeAll(ResultSet rs,PreparedStatement pstmt,Connection con){
if(rs!=null){rs.close();}
if(pstmt!=null){pstmt.close();}
if(con!=null){con.close();}
}

二、更新数据库操作方法update()返回影响数据库结果的行数

public int update(){

Connection con=null;
PreparedStatement pstmt=null;
int result=0;
try{
con=this.getConnection();//建立连接
pstmt=con.prepareStatement(sql);//创建预编译语句块
this.getParams(pstmt,params);//给预编译语句赋值
result=pstmt.executeUpdate();//执行预编译语句获取结果或结果集
}catch{}finally{
closeAll(null,pstmt,con);
}
return result;
}

测试更新数据

@Test
public void Test(){
DBHepler db=new DBHepler();
String sql="inset into dept values(?,?,?)";//**********用占位符
System.out.println(db.update(sql,50,'技术部','2020'));
}

三、查询数据库中所有数据的方法finds()

public List<Map<String,Object>> finds(String sql,Object ... params){

Connection con=null;
PreparedStatement pstmt=null;
ResultSet rs=null;

List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();//***************
Map<String,Object>=null;//每个Map中存放一行数据

try{
con=this.getConnection();//建立连接
pstmt=con.prepareStatement(sql);//创建预编译语句块
this.getParams(pstmt,params);//给预编译语句赋值
result=pstmt.executeUpdate();//执行预编译语句获取结果或结果集
//*****处理结果
ResultSetMetaData rsmd=rs.getMetaData();//获取列元数据
int colCount=rsmd.getColumnCount();//获取列数
String[] colNames=new String[colCount];//通过索引获取列名
for(int i=0;i<colCount;i++){
colNames[i]=rsmd.getColumnName(i+1).toLowerCase();//***********记得转换成小写
}
while(rs.next){//每次循环就是一行数据
map=new HashMap<String,Object>;//*******???????
for(String colname:colNames){
map.put(colName,rs.getObject(colName));//************
}
list.add(map);//******
}

}catch{}finally{
closeAll(rs,pstmt,con);
}
}
return list;
}

测试查询所有行

@Test
public void Test(){
DBHepler db=new DBHepler();
String sql="select *from dept where dname=?;//**********用占位符

List<Map<String,Object>> list=db.finds(sql);//***********接收结果集
for(Map<String,Object> map:list){
System.out.println(map.get(dname)));
}
}

四、在这java工程下面再建一个配置文件db.properties

driverClassName=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
user=scott
password=a

五、单例化(新建一个类,实现单例化,确保在整个过程中只存在一个实例化对象)


public class ReaderPro extends Properties {
private static ReaderPro instence=new ReaderPro();//饿汉式

/**
* 构造函数私有化
*/
private ReaderPro(){
InputStream is=null;
is=this.getClass().getClassLoader().getResourceAsStream("db.properties");
try {
this.load(is);
} catch (IOException e) {
e.printStackTrace();
}finally{
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

public static ReaderPro getInstance(){
return instence;
}
}

猜你喜欢

转载自blog.csdn.net/Diligent_Programmer/article/details/78912993
今日推荐