JDBC底层源代码

来源:互联网 发布:java 从启动程序 内存 编辑:程序博客网 时间:2024/05/17 23:43
package com.lovo.dao.impl;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;


public class BaseDao {

protected Connection con;

protected PreparedStatement ps;

protected ResultSet rs;

public void setConnection(){
try {
//加载驱动
Class.forName("org.gjt.mm.mysql.Driver");
this.con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/mysql?characterEncoding=utf-8","root","lovo");
} catch (Exception e) {
e.printStackTrace();
}
}

public void closeConnection(){
try{
if(rs != null){
rs.close();
}
if(ps != null){
ps.close();
}
if(con != null){
con.close();
}

}catch(Exception e){
e.printStackTrace();
}
}


public void updateData(String sql,Object[] valueObjs) throws Exception{
this.setConnection();
try{
//执行sql语句
this.ps = this.con.prepareStatement(sql);
//填充占位符
for(int i = 0;i
this.ps.setObject(i+1, valueObjs[i]);
}
//更新数据库
this.ps.executeUpdate();
}finally{
this.closeConnection();
}
}
//练习第二遍
public void DUADATA(String sql,Object[] value) throws Exception{
//简历链接
this.setConnection();
try{
this.ps=this.con.prepareStatement(sql);
for (int i = 0; i < value.length; i++) {
this.ps.setObject(i+1,value[i]);
}
this.ps.executeUpdate();
}finally{
this.closeConnection();
}
}
public List FINDDATA(String sql,Object[] value,Class Bean)throws Exception{
//声明list集合对象
List list=new ArrayList();
//获取链接
this.setConnection();
try{
this.ps=this.con.prepareStatement(sql);
for (int i = 0; i < value.length; i++) {
this.ps.setObject(i+1,value[i]);
}
//执行查询数据库更新
this.rs=this.ps.executeQuery();
ResultSetMetaData rm=rs.getMetaData();
//获得结果集的列数
int Count=rm.getColumnCount();

while(rs.next()){//遍历结果集
//获得类模板对象
Object obj=Bean.newInstance();
for (int i = 1; i <= Count; i++) {
//得到查询的结果集的列表值
String key=rm.getColumnName(i);
//根据结果的键得到对应的值
Object values=rs.getObject(key);
//根据属性得到对应的对象值
Field f=Bean.getDeclaredField(key);
//去掉私有修饰符的限制
f.setAccessible(true);
//给对象赋值
f.set(obj, values);
}
list.add(obj);
}
}finally{
this.closeConnection();
}
return list;
}
//查询
public List find(String sql,Object[] valueObjs,Class beanClass) throws Exception{
List list = new ArrayList();
this.setConnection();
try{
this.ps = this.con.prepareStatement(sql);
if(valueObjs != null){
for(int i = 0;i
this.ps.setObject(i+1, valueObjs[i]);

}
}
this.rs = this.ps.executeQuery();

ResultSetMetaData rm = rs.getMetaData();
//得到结果集的列数
int colummCount = rm.getColumnCount();
while(rs.next()){
Object obj = beanClass.newInstance();
for(int i=1;i<=colummCount;i++){
//得到查询列的列名
String columnName = rm.getColumnName(i);
//得到指定列结果集的值
Object value = rs.getObject(columnName);
//得到指定属性名的属性对象
Field f = beanClass.getDeclaredField(columnName);
f.setAccessible(true);
//给obj对象的属性填充结果集中的值
f.set(obj, value);
}
list.add(obj);
}
}finally{
this.closeConnection();
}
return list;
}
}
0 0
原创粉丝点击