2025-05-23

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實現插入一條記錄.

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *