Student.java文件代码如下:
public class Student {
public int id;
public String name;
public int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Session.java文件代码如下:
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
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 ClassNotFoundException, SQLException,
SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
String sql=createSQL();
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost/hibernate","root","123456");
PreparedStatement ps=conn.prepareStatement(sql);
//获取方法返回值
for(int i=0;i<methodNames.length;i++){
//获取得到方法的对象
Method method=s.getClass().getMethod(methodNames[i]);
//获取方法的返回值
Class r=method.getReturnType();
//根据返回值传入ps的参数
if(r.getName().equals("java.lang.String")){
String returnValue=(String)method.invoke(s);
//向ps里面传入参数
ps.setString(i+1, returnValue);
}
if(r.getName().equals("int")){
Integer returnValue=(Integer)method.invoke(s);
ps.setInt(i+1, returnValue);
}
System.out.println(method.getName()+"|"+r);
}
ps.executeUpdate();
ps.close();
conn.close();
}
public String createSQL(){
//存取具体参数变量
String keyStr="";
int index=0;
for(String s:cfs.keySet()){
//获取key中对应的值
String v=cfs.get(s);
//把获取到keySet值首字母转成大写
v=Character.toUpperCase(v.charAt(0))+ v.substring(1);
//给get属性的名称首字母大写
methodNames[index]="get"+v;
keyStr +=s+",";
index++;
}
System.out.println("keyStr="+keyStr);
//不要最后一个逗号
keyStr=keyStr.substring(0, keyStr.length()-1);
System.out.println("keyStr="+keyStr);
//存取一共有几个参数,用问号替换参数变量
String keyStr2="";
for(int i=0;i<cfs.size();i++){
keyStr2+="?,";
}
System.out.println("keyStr2="+keyStr2);
keyStr2=keyStr2.substring(0,keyStr2.length()-1);
System.out.println("keyStr2="+keyStr2);
//组装成一句完整的sql语句
String sql="insert into "+tableName+"("+keyStr+")"+"values("+keyStr2+")";
System.out.println("sql="+sql);
return sql;
}
}
TestHibernate.java代码如下:
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
public class TestHibernate {
/**
* @param args
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws NoSuchMethodException
* @throws SQLException
* @throws ClassNotFoundException
* @throws IllegalArgumentException
* @throws SecurityException
*/
public static void main(String[] args) throws SecurityException,
IllegalArgumentException,
ClassNotFoundException,
SQLException,
NoSuchMethodException,
IllegalAccessException,
InvocationTargetException {
Student s=new Student();
s.setId(1);
s.setName("tfq");
s.setAge(22);
Session session=new Session();
session.save(s);
}
}
以上代码可模拟Hibernate实现插入一条记录.