package cn.com.basedao
import java.lang.reflect.Field
import java.lang.reflect.InvocationTargetException
import java.lang.reflect.Method
import java.sql.Connection
import java.sql.Date
import java.sql.PreparedStatement
import java.sql.SQLException
import java.text.SimpleDateFormat
import cn.com.db.DButil
import cn.com.javabean.IsId
public class BaseDao<T> {
public void add(T t1) {
Class c = t1.getClass()
String Simplename = c.getSimpleName()
StringBuilder stb = new StringBuilder("insert into " + Simplename+ " ")
StringBuilder stbColName = new StringBuilder("(")
StringBuilder stbValue = new StringBuilder("values(")
Field[] f = c.getDeclaredFields()
try {
int i = f.length - 1
for (Field ff : f) {
String name = ff.getName()
Class type = ff.getType()
stbColName.append(name)
stbValue.append("?")
if (i > 0) {
stbColName.append(",")
stbValue.append(",")
}
i--
}
stbColName.append(")")
stbValue.append(")")
stb.append(stbColName + " " + stbValue)
Connection conn = DButil.getConn()
PreparedStatement pstm = null
pstm = conn.prepareStatement(new String(stb))
int result = 0
for (i = 0
Object value=null
String name = f[i].getName()
f[i].setAccessible(true)
Class type = f[i].getType()
if (type == String.class) {
if (f[i].get(t1) == null) {
pstm.setNull(i + 1,Types.VARCHAR)
} else {
pstm.setObject(i + 1, f[i].get(t1))
}
} else if (type == Date.class) {
if (f[i].get(t1) == null) {
pstm.setNull(i + 1,Types.DATE)
} else {
pstm.setObject(i + 1, f[i].get(t1))
}
}else{
if (f[i].get(t1) == null) {
pstm.setNull(i + 1,Types.INTEGER)
} else {
pstm.setObject(i + 1, f[i].get(t1))
}
}
}
result = pstm.executeUpdate()
System.out.println(result)
} catch (SecurityException e) {
e.printStackTrace()
} catch (SQLException e) {
e.printStackTrace()
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace()
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
public void update(T t1) {
Class c = t1.getClass()
String Simplename = c.getSimpleName()
StringBuilder stb = new StringBuilder("update " + Simplename + " set ")
Field[] f = c.getDeclaredFields()
int i = 0
try {
for (i = 1
String name = f[i].getName()
Class type = f[i].getType()
f[i].setAccessible(true)
System.out.println(name)
stb.append(name + "=? ")
if (i + 1 < f.length) {
stb.append(",")
}
}
stb.append(" where " + f[0].getName() + " =?")
System.out.println(stb)
Connection conn = DButil.getConn()
PreparedStatement pstm = null
pstm = conn.prepareStatement(new String(stb))
f[0].setAccessible(true)
pstm.setObject(f.length, f[0].get(t1))
int result = 0
for (i = 1
pstm.setObject(i, f[i].get(t1))
}
pstm.setObject(f.length, f[0].get(t1))
result = pstm.executeUpdate()
System.out.println(result)
} catch (SecurityException e) {
e.printStackTrace()
} catch (SQLException e) {
e.printStackTrace()
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace()
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
}
**查询和删除方法会继续更新**