Mysql> Select * from users;
-- ------- ---------- -------------- | ID | Name | Password | CreateTime | ---- ------- ---------- -------------- | 1 | Bitan | Bbbbbbbb | 20050202035511 || 2 | Helen | HHHHHHHHHH | 20050202035526 || 3 | Susan | sssssssss | 20050202035541 | ---- ------- ------------ ---------------- 3 ROWS IN SET (0.00 SEC)
K: / Tomcat/conf/server.xml
......
Context>
Host>
Engine>
Service>
Server>
K: /TEST26/Web-inf/web.xml
XML Version = "1.0" encoding = "UTF-8"?>
XML Version = "1.0" Encoding = "UTF-8"?>
K: / Test26/web-inf/classes/Users.hbm.xml
XML Version = "1.0" encoding = "utf-8"?>
// Similar Javabean
Package beans;
public class Users {private int id; private String name; private String password; private String createTime; public void setId (int id) {this.id = id;} public int getId () {return this.id;} public void setName (String name) {this.name = name;} public String getName () {return this.name;} public void setPassword (String password) {this.password = password;} public String getPassword () {return this.password; } Public void setcreatetime (string createtime) {this.createtime = cretetime;} public string getcreatetime () {return this.createtime;}}
K: /TEST26/src/hibaateutil.java
// Set hibernate
Package utils; import net.sf.hib.Hibernate.cfg. *;
public class HibernateUtil {private static final ThreadLocal sessions = new ThreadLocal (); private static final SessionFactory factory;.. static {try {factory = new Configuration () configure () buildSessionFactory ();} catch (HibernateException he) {he.printStackTrace (); (Exception E) {E.PrintStackTrace ();}} PUBLIC STACSSION CURRENTSESSION () THROWS HibernateException {session s = (session) sessions.get (); if (s == null) {s = factory .openSession (); sessions.set (s);} return s;} public static vid closesession () throws hibernateException {session s = (session) sessions.get (); sessions.set (null); if (s! = NULL) {s.close ();}}}
K: /TEST26/HIBER.JSP
<% @ Page ContentType = "Text / HTML; Charset = GBK"%> <% @ page import = "Net.sf.hibernate. *, net.sf.hibernate.cfg. *, beans.users, utils.hibernateutil, Java.util.ITerator "%> <% try {net.sf.hibernate.Session h_session = utils.hibernateutil.currentSession ();
// Insert a line record transaction tx = h_session.begintransaction (); users users = new users (); users.setname ("jodan"); users.setpassword ("jjjjjjjjjjjj); h_session.save (users); tx.commit (); Hibernateutil.closesession ();
// Query all records h_session = hibernateutil.currentSession (); transaction tx2 = h_session.begginTransAction (); query query = h_session.createQuery ("SELECT U from beans.users as u"); // Hibernate unique SQL syntax for ( Iterator it = query.Iterate (); it.hasnext (); {useris = () it.next (); int id = localusers.getId (); string name = localusers.getname (); string password = LocalUsers.getPassword (); string createtime = localusers.getcreatetime (); out.println ("id =" ID "name =" Name "CreateTime =" password "CreateTime =" CreateTime "
");} Tx2.commit ();} catch (hibernateException he) {system.out.println (" ********************* HE .getMessage ()); He.PrintStackTrace ();}%>
result:
Http: // localhost: 2000 / test26 / hiber.jsp
id = 1 name = bitan password = bbbbbbbbb createTime = 20050202035511id = 2 name = helen password = hhhhhhhhh createTime = 20050202035526id = 3 name = susan password = ssssssssss createTime = 20050202035541id = 4 name = Jodan password = jjjjjjjjjjj createTime = 20050202045004