hibernate 模拟器

域模型:Student
package com.eagle.hibernate.model;

public class Student {
private int id;
private String name;
private 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;
}

}
测试类:StudentTest

import com.eagle.hibernate.model.Student;


public class StudentTest {
public static void main(String args[]){
Student s = new Student();
s.setId(8);
s.setName("student2");
s.setAge(18);

Session session  = new Session();
session.save(s);
}
}
实现功能类:Session
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import com.eagle.hibernate.model.Student;


public class Session {

String tableName = "_Student";//数据库表名
Map<String,String> cfs = new HashMap<String,String>();
String[] methodNames;

public Session(){
cfs.put("_id","id");//key表示数据库字段,id表示student属性
cfs.put("_name", "name");
cfs.put("_age", "age");
methodNames = new String[cfs.size()];//存储get方法的名字
}



public void save(Student s) {

String sql = createSQL();
Connection conn = null;
PreparedStatement psmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost/hibernate?user=root&password=root";
conn = DriverManager.getConnection(url);
psmt = conn.prepareStatement(sql);
for(int i=0;i<methodNames.length;i++){
Method m = s.getClass().getMethod(methodNames[i]);//获得student的get方法
Class r = m.getReturnType();//取得返回值类型的类
System.out.println(m.getName()+"|"+r.getName());

if(r.getName().equals("java.lang.String")){
String returnValue = (String)m.invoke(s);//m.invoke()表示执行m方法,此方法返回值存入returnValue
psmt.setString(i+1, returnValue);
}
if(r.getName().equals("int")){
Integer returnValue = (Integer)m.invoke(s);
psmt.setInt(i+1, returnValue);
}
}


psmt.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} finally{
try {
psmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}

}

}



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;//取得student属性的get方法名
str1 += s+",";
index++;
}
str1 = str1.substring(0,str1.length()-1);

String str2="";//sql语句未知参数
for(int i=0;i<cfs.size();i++){
str2+="?,";
}
str2=str2.substring(0,str2.length()-1);
String sql = "insert into "+ tableName + " ( "+ str1 +" ) "+" values ( "+str2+" )";
System.out.println(sql);
return sql;
}

}

你可能感兴趣的:(java,sql,Hibernate,mysql,jdbc)