Insert a record into the database using Hibernate Simulation

Source: Internet
Author: User

The code of the Student. java file is as follows:

 

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;
}

}


The Session. java file code is as follows:


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 );
// Obtain the method Return Value
For (int I = 0; I <methodNames. length; I ++ ){
// Obtain the object of the Method
Method method = s. getClass (). getMethod (methodNames [I]);
// Obtain the return value of the Method
Class r = method. getReturnType ();
// Input ps Parameters Based on the returned values
If (r. getName (). equals ("java. lang. String ")){
String returnValue = (String) method. invoke (s );
// Input parameters to 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.exe cuteUpdate ();
Ps. close ();
Conn. close ();

}
 
Public String createSQL (){
// Access specific parameter variables
String keyStr = "";
Int index = 0;
For (String s: cfs. keySet ()){
// Obtain the value corresponding to the key
String v = cfs. get (s );
// Convert the first letter of the obtained keySet value to uppercase
V = Character. toUpperCase (v. charAt (0) + v. substring (1 );
// Capital the first letter of the get attribute name
MethodNames [index] = "get" + v;
KeyStr + = s + ",";
Index ++;
}
System. out. println ("keyStr =" + keyStr );
// Do not use the last comma
KeyStr = keyStr. substring (0, keyStr. length ()-1 );
System. out. println ("keyStr =" + keyStr );
// There are several parameters to access. Replace the parameter variables with question marks.
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 );
// Assemble a complete SQL statement
String SQL = "insert into" + tableName + "(" + keyStr + ")" + "values (" + keyStr2 + ")";
System. out. println ("SQL =" + SQL );
Return SQL;
}
}

 

 

The TestHibernate. java code is as follows:


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 );

}
}


The above code can simulate Hibernate to insert a record.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.