package com.dao;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.entity.Book;
public class BaseDao {
private static PreparedStatement ps;
private static Connection conn;
private static String sql;
private static ResultSet rs;
static{
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConn(){
conn=null;
try {
conn=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "scott", "accp");
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void closeAll(Connection conn,PreparedStatement ps,ResultSet rs){
try {
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(conn!=null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
//查询全部
public static List findAll(Class c){
List list=new ArrayList();
Field[] fArr=c.getDeclaredFields();
AccessibleObject.setAccessible(fArr, true);
sql="select * from "+c.getSimpleName()+" order by id desc";
conn=getConn();
try {
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()){
Object obj=c.newInstance();
for (int i= 0; i < fArr.length; i++) {
Field f=fArr[i];
Object value=rs.getObject(f.getName());
String type=f.getType().getSimpleName();
if(value instanceof BigDecimal){
if("int".equals(type)){
value=((BigDecimal)value).intValue();
}else if("double".equals(type)){
value=((BigDecimal)value).doubleValue();
}
}
f.set(obj, value);
}
list.add(obj);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
closeAll(conn, ps, rs);
}
return list;
}
//添加
public static int add(Object obj){
int count=0;
Class c=obj.getClass();
StringBuffer sb=new StringBuffer("insert into "+c.getSimpleName()+" values(seq_users.nextval");
Field[] fArr=c.getDeclaredFields();
AccessibleObject.setAccessible(fArr, true);
for (int i = 1; i < fArr.length; i++) {
sb.append(",?");
}
sb.append(")");
sql=sb.toString();
conn=getConn();
try {
ps=conn.prepareStatement(sql);
for (int i = 1; i < fArr.length; i++) {
Field f=fArr[i];
Object o=f.get(obj);
ps.setObject(i, o);
}
count=ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally{
closeAll(conn, ps, null);
}
return count;
}
//删除
public static int del(Object obj){
int count=0;
//delete 表明 where id=?
Class c=obj.getClass();
try {
Field[] fArr=c.getDeclaredFields();
AccessibleObject.setAccessible(fArr, true);
String id=fArr[0].getName();
StringBuffer sb=new StringBuffer("delete "+c.getSimpleName()+" where "+id+"=?");
sql=sb.toString();
conn=getConn();
ps=conn.prepareStatement(sql);
ps.setObject(1, fArr[0].get(obj));
count=ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally{
closeAll(conn, ps, null);
}
return count;
}
//根据ID查找
public static Object QueryById(Object obj){
Class c=obj.getClass();
Object obj1=null;
Field fArr[]=c.getDeclaredFields();
AccessibleObject.setAccessible(fArr, true);
StringBuffer sb=new StringBuffer("select * from "+c.getSimpleName()+" where "+fArr[0].getName()+"=?");
sql=sb.toString();
conn=getConn();
try {
ps=conn.prepareStatement(sql);
ps.setObject(1, fArr[0].get(obj));
rs=ps.executeQuery();
if(rs.next()){
obj1=c.newInstance();
for (int i = 0; i < fArr.length; i++) {
Field f=fArr[i];
Object value=rs.getObject(f.getName());
String type=f.getType().getSimpleName();
if(value instanceof BigDecimal){
if("int".equals(type)){
value=((BigDecimal)value).intValue();
}else if("double".equals(type)){
value=((BigDecimal)value).doubleValue();
}
}
f.set(obj1, value);
}
}
} catch (Exception e) {
e.printStackTrace();
}finally{
closeAll(conn, ps, rs);
}
return obj1;
}
//修改
public static int update(Object obj){
int count=0;
Class c=obj.getClass();
StringBuffer sb=new StringBuffer("update "+c.getSimpleName()+" set ");
Field[] fArr=c.getDeclaredFields();
AccessibleObject.setAccessible(fArr, true);
for (int i = 1; i < fArr.length; i++) {
Field f=fArr[i];
sb.append(f.getName()+"=?");
if(i sb.append(",");
}
}
sb.append(" where "+fArr[0].getName()+"=?");
sql=sb.toString();
conn=getConn();
try {
ps=conn.prepareStatement(sql);
for (int i = 1; i < fArr.length; i++) {
Field f=fArr[i];
ps.setObject(i, f.get(obj));
}
ps.setObject(fArr.length, fArr[0].get(obj));
count=ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally{
closeAll(conn, ps, null);
}
return count;
}
public static void main(String[] args) {
}
}