第一个元素为主键,为修改索引
将修改后的对象传入
------------test----------------
package fozzz;
public class myTest {
public static void main(String[] args) {
// User user = new User();
//
// user.setId(1);
//
// user.setName("name");
//
// user.setPassword("pwd");
//
// Update update=new Update(user);
Student student = new Student(1, "王宝强", "马蓉", "经纪人", "爱绿");
Update update = new Update(student);
boolean ok = update.isOk();
System.out.println(ok);
}
}
----------------连接工厂类---------------------
package fozzz;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class MyConnection {
public Connection getConnection() {
Connection connection = null;
try {
connection = DriverManager.getConnection("jdbc:mysql://192.168.1.101/fozzz", "root", "root");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return connection;
}
}
-----------------连接数据库---------------------
package fozzz;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Update {
private boolean ok = false;
public Update(Object object) {
myUpdate myUpdate = new myUpdate(object);
Connection connection = new MyConnection().getConnection();
String sql = myUpdate.getSql();
int length = myUpdate.getLength();
Object[] resultArray = myUpdate.getResultArray();
try {
PreparedStatement prepareStatement = connection.prepareStatement(sql);
prepareStatement.setObject(length, resultArray[0]);
for(int i=1;i<length;i++){
prepareStatement.setObject(i, resultArray[i]);
}
int executeUpdate = prepareStatement.executeUpdate();
if (executeUpdate > 0) {
ok = true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public boolean isOk() {
return ok;
}
}
-------------反射,得到sql语句,get方法结果------------------------
package fozzz;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
public class myUpdate {
private Class<? extends Object> myClass;
private Object object;
private String[] memberArray;// 类变量
private Object[] resultArray;// get结果
private int length;// 变量个数
private String className;
public myUpdate(Object object) {
this.myClass = object.getClass();
this.object = object;
try {
myInit();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void myInit() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException,
NoSuchMethodException, SecurityException {
// 成员变量
Field[] declaredFields = myClass.getDeclaredFields();
length = declaredFields.length;
memberArray = new String[length];
resultArray = new Object[length];
for (int i = 0; i < length; i++) {
String name = declaredFields[i].getName();
memberArray[i] = name;
// 得到get方法的结果
resultArray[i] = myClass.getMethod("get" + (char) (name.charAt(0) - 32) + name.substring(1)).invoke(object);
}
String[] split = myClass.getName().split("\\.");
className = split[split.length - 1];
}
// 拼接sql语句
public String getSql() {
String sql = "update " + className + " set " + memberArray[1];
for (int i = 2; i < length; i++) {
sql += "=? ," + memberArray[i];
}
sql += "=? where " + memberArray[0] + "=?";
return sql;
}
public Object[] getResultArray() {
return resultArray;
}
public int getLength() {
return length;
}
}