package test;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.HashMap;
import java.util.Map;
import model.Student;
public class Session {
String tableName = "student";
Map<String, String> cfs = new HashMap<String, String>() ;
String[] methodNames;
public Session(){
cfs.put("id", "id");
cfs.put("name", "name");
cfs.put("age","age");
methodNames =new String[cfs.size()];
}
public void save(Student s) throws Exception{
String sql = createSQL();
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/hibernate","root","root");
PreparedStatement ps = conn.prepareStatement(sql);
for(String mName : methodNames){
Method m = s.getClass().getMethod(mName);
System.out.println(m.getName()+"|"+m.getReturnType());
}
for(int i =0;i<methodNames.length;i++){
Method m = s.getClass().getMethod(methodNames[i]);
Class r= m.getReturnType();
if(r.getName().equals("java.lang.String")){
String returnValue = (String) m.invoke(s);
ps.setString(i+1, returnValue);
}
if(r.getName().equals("java.lang.Integer")){
Integer returnValue = (Integer) m.invoke(s);
ps.setInt(i+1, returnValue);
}
}
ps.executeUpdate();
ps.close();
conn.close();
}
private String createSQL() {
String str1 ="";
int index=0;
for(String s : cfs.keySet()){
String v= cfs.get(s);
v=Character.toUpperCase(v.charAt(0))+v.substring(1);
methodNames[index] = "get" +v;
str1+=s+",";
index++;
}
str1 = str1.substring(0,str1.length()-1);
System.out.println(str1);
String str2="";
for(int i = 0 ;i<cfs.size();i++){
str2+="?,";
}
str2=str2.substring(0,str2.length()-1);
System.out.println(str2);
String sql = "insert into "+tableName+"("+str1+")"+" values ("+str2+")";
System.out.println(sql);
return sql;
}
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.HashMap;
import java.util.Map;
import model.Student;
public class Session {
String tableName = "student";
Map<String, String> cfs = new HashMap<String, String>() ;
String[] methodNames;
public Session(){
cfs.put("id", "id");
cfs.put("name", "name");
cfs.put("age","age");
methodNames =new String[cfs.size()];
}
public void save(Student s) throws Exception{
String sql = createSQL();
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/hibernate","root","root");
PreparedStatement ps = conn.prepareStatement(sql);
for(String mName : methodNames){
Method m = s.getClass().getMethod(mName);
System.out.println(m.getName()+"|"+m.getReturnType());
}
for(int i =0;i<methodNames.length;i++){
Method m = s.getClass().getMethod(methodNames[i]);
Class r= m.getReturnType();
if(r.getName().equals("java.lang.String")){
String returnValue = (String) m.invoke(s);
ps.setString(i+1, returnValue);
}
if(r.getName().equals("java.lang.Integer")){
Integer returnValue = (Integer) m.invoke(s);
ps.setInt(i+1, returnValue);
}
}
ps.executeUpdate();
ps.close();
conn.close();
}
private String createSQL() {
String str1 ="";
int index=0;
for(String s : cfs.keySet()){
String v= cfs.get(s);
v=Character.toUpperCase(v.charAt(0))+v.substring(1);
methodNames[index] = "get" +v;
str1+=s+",";
index++;
}
str1 = str1.substring(0,str1.length()-1);
System.out.println(str1);
String str2="";
for(int i = 0 ;i<cfs.size();i++){
str2+="?,";
}
str2=str2.substring(0,str2.length()-1);
System.out.println(str2);
String sql = "insert into "+tableName+"("+str1+")"+" values ("+str2+")";
System.out.println(sql);
return sql;
}
}
测试类:
package test;
import model.Student;
public class StudentTest {
public static void main(String[] args) throws Exception{
Student s = new Student();
s.setId(8);
s.setName("s2");
s.setAge(9);
Session session = new Session();
session.save(s);
}
}