Today I used the Java Database persistence-Business Hibernate framework. The following is an example of using hibernate to connect to the MySQL database.
The table creation structure is as follows:
Mysql> DESC test;
+ ---------- + -------------- + ------ + ----- + --------- + ---------------- +
| FIELD | type | null | key | default | extra |
+ ---------- + -------------- + ------ + ----- + --------- + ---------------- +
| ID | int (11) | no | pri | null | auto_increment |
| Username | varchar (100) | no | null |
+ ---------- + -------------- + ------ + ----- + --------- + ---------------- +
2 rows in SET (0.00 Sec)
Mysql>
Hibernate. cfg. xml
<? XML version = '1. 0' encoding = 'utf-8'?> <Br/> <! Doctype hibernate-configuration public "-// hibernate/hibernate configuration DTD 3.0 // en" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <br/> <pibernate-configuration> <br/> <session-factory> <br/> <property name = "show_ SQL"> true </property> <br/> <property name = "dialect"> Org. hibernate. dialect. mysqldialect </property> <br/> <property name = "connection. driver_class "> COM. mySQL. JDBC. driver </property> <br/> <property name = "connection. URL "> JDBC: mysql: // 192.168.25.152/test </property> <br/> <property name =" connection. username "> username </property> <br/> <property name =" connection. password "> password </property> <br/> <Mapping Resource =" user. HBM. XML "/> <br/> </session-factory> <br/> </pibernate-configuration>
Table ing user. HBM. xml
<? XML version = '1. 0' encoding = 'utf-8'?> <Br/> <! Doctype hibernate-mapping public "-// hibernate/hibernate mapping DTD 3.0 // en" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <br/> <pibernate-mapping> <br/> <class name = "com. testdb "table =" test "> <br/> <ID name =" ID "column =" ID "> <br/> <generator class =" increment "/> <br /> </ID> <br/> <property name = "username" column = "username"/> <br/> </class> <br/> </pibernate- mapping>
Table ing Class
Package com; </P> <p>/** <br/> * created by intellij idea. <br/> * User: Administrator <br/> * Date: 2006-2-6 <br/> * Time: 22:10:05 <br/> * to change this template use file | Settings | file templates. <br/> */<br/> public class testdb {<br/> private string username; <br/> private long ID; </P> <p> Public long GETID () {<br/> return ID; <br/>}</P> <p> Public void setid (long ID) {<br/> This. id = ID; <br/>}</P> <p> Public String GetUserName () {<br/> return username; <br/>}</P> <p> Public void setusername (string myname) {<br/> This. username = myname; <br/>}< br/>
Test class
Package com; </P> <p> Import Java. util. list; </P> <p> Import Org. hibernate. query; <br/> Import Org. hibernate. session; <br/> Import Org. hibernate. sessionfactory; <br/> Import Org. hibernate. transaction; <br/> Import Org. hibernate. cfg. configuration; </P> <p> public class test {</P> <p> // traversal <br/> Public static void all () <br/>{< br/> query q = session. createquery ("select C. ID, C. username from testdb as C "); </P> <p> list l = Q. list (); <br/> for (INT I = 0; I <L. size (); I ++) <br/>{< br/> // testdb user = (testdb) L. get (I); <br/> // system. out. println (user. getUserName (); </P> <p> object [] ROW = (object []) L. get (I); <br/> long id = (long) Row [0]; <br/> string name = (string) Row [1]; <br/> system. out. println (ID + "" + name); <br/>}</P> <p> // read <br/> Public static void load () <br/> {<br/> testdb OBJ = (testdb) session. load (testdb. class, new long (2); <br/> system. out. println (obj. getUserName (); <br/>}</P> <p> // update <br/> Public static void Update () <br/> {<br/> testdb OBJ = (testdb) session. load (testdb. class, new long (2); <br/> obj. setusername ("CG"); <br/>}</P> <p> // insert <br/> Public static void insert () <br/>{< br/> testdb user = new testdb (); <br/> User. setusername ("SB"); </P> <p> session. save (User); <br/>}</P> <p> static sessionfactory; <br/> static session; <br/> static transaction TX; </P> <p> Private Static void Init () <br/> {<br/> sessionfactory = new configuration (). configure (). buildsessionfactory (); <br/> session = sessionfactory. opensession (); <br/> Tx = session. begintransaction (); <br/>}</P> <p> Private Static void close () <br/>{< br/> Tx. commit (); <br/> session. close (); <br/> sessionfactory. close (); <br/>}</P> <p> Public static void main (string [] ARGs) <br/>{< br/> Init (); <br/> Update (); <br/> close (); <br/>}< br/>
File structure