JBuilder + Hibernate + Tomcat Quick Start

xiaoxiao2021-03-06  70

JBuilder Hibernate Tomcat Quick Start

Gaoke Hua

About the author: Gao Jinghua, Nanjing Aviation College computing a master's degree in mathematics, more than ten years of corporate informationization work experience. Current research interest, J2EE enterprise application, ERP software research and development, data warehouse system research and development. This article gives a general step in developing the Hibernate Tomcat web application with JBuilder.

1. Download Hibernate-2.1.x.ziphttp: //prdownloads.sourceforge.net/hibernate/? Sort_by = Date & Sort = DESC

2. Installation, extract the zip file to jbuilder_home / thirdparty

3. In the new Hibernate library in JBuilder 2005, select Menu Tools-Configure-Libraries, click the New button in the pop-up dialog box, enter the name of the Hibernate library: Hibernate, click the Add button to add the file hibernate2.jar in the hibernate directory. In the Hibernate library, the corresponding * .jar in the hibernate / lib directory is added to the Hibernate library as needed, which generally requires these categories: DOM4J, CGLIB, COMMONS Collections, Commons Logging, ODMG4, EHCACHE

4. New project files, select the menu file-new project, name to myProject

Set the properties of the project file, select the menu Project-Project Properties, select Tomcat as the server, join the Hibernate library to Path / Required Libraries.

5. New web module, select the menu file-new, named QuickStart for the web module

6. Use the servlet wizard to create a servlet file, select the menu file-new, select the web-standard servlet in the pop-up dialog, name servlet1, in the fifth step of the wizard, select Create a Runtime Configuration

7. Run servlet1

8. Add the file server8080.xml in the QuickStart / Tomcat / Conf directory to the project file, modify the content of Server8080.xml

factory

org.apache.commons.dbcp.basicDataSourceFactory

URL

jdbc: hsqldb: hsql: // localhost

JDBC: Microsoft: SQLServer: // NT04: 1433; DatabaseName = Test

driverclassname

Org.hsqldb.jdbcdriver

com.microsoft.jdbc.sqlserver.sqlserverdriver

Username

sa

Password

maxwait

3000

maxidle

100

maxactive

10

9. Set Hibernate, create new files in the QuickStart / src directory, hibernate.cfg.xml as follows:

PUBLIC "- // Hibernate / Hibernate Configuration DTD // EN"

"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

Java: Comp / Env / JDBC / QuickStart

false

Net.sf.hibernate.Dialect.postgreSqldiaAlaforct

10. Newly built a lasting class

Package net.sf.hibernate.examples.quickstart;

PUBLIC Class Cat {

Private string id;

PRIVATE STRING NAME;

Private char sex;

PRIVATE FLOAT Weight;

Public cat () {

}

Public string getId () {

Return ID;

}

Private void setid (string id) {

THIS.ID = ID;

}

Public string getname () {

Return Name;

}

Public void setname (String name) {

THIS.NAME = Name;

}

Public char getsex () {

Return SEX;

}

Public void setsex (char sex) {

THIS.SEX = SEX;

}

Public float getWeight () {

Return weight;

}

Public void setWeight (Float Weight) {

THIS.WEIGHT = Weight;

}

}

11. Create a new image in the QuickStart / SRC directory Cat.hbm.xml as follows:

Public "- // hibernate / hibernate mapping dtd // en"

"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

Generated by hibernate with the uuid pattern. ->

12. Prepare the database

13. Create a new class HibernateUtil

Import net.sf.hibernate. *;

Import net.sf.hibs. *;

Public class hibernateutil {

Private static log log = logfactory.getlog (HibernateUtil.class);

PRIVATE STATIC FINAL SESSIONFACTORY SESSIONFACTORY;

STATIC {

Try {

// Create the sessionFactory

SESSIONFACTORY = New Configuration (). CONFIGURE (). BuildSessionFactory ();

} catch (throwable ex) {

Log.Error ("Initial SessionFactory Creation Failed.", EX);

Throw New ExceptioninInitializerError (ex);

}

}

PUBLIC Static Final Threadlocal session = new threadlocal ();

PUBLIC STATIC session currentsession () throws hibernateException {session s = (session) session.get ();

// Open a new session, if this thread HAS NONE YET

IF (s == NULL) {

S = sessionFactory.openSession ();

Session.set (s);

}

Return S;

}

Public static void closesession () THROWS HibernateException {

Session s = (session) session.get ();

session.set (null);

IF (s! = null)

s.close ();

}

}

14. Modify servlet1.java

Package net.sf.hibernate.examples.quickstart;

Import javax.servlet. *;

Import javax.servlet.http. *;

Import java.io. *;

Import java.util. *;

Import net.sf.hibernate.transaction;

Import net.sf.hibernate.hibernateException;

Import net.sf.hibernate.Session;

Import net.sf.hibernate.query;

Public class servlet1 extends httpservlet {

Private static final string content_type = "text / html; charSet = BIG5";

Private hibernateutil HibernateUtil = NULL;

// Initialize Global Variables

Public void init () throws servletexception {

}

// Process the http get request

Public void doget (httpservletRequest Request, HttpservletResponse Response) THROWS

ServletException, IOException {

Response.setContentType (Content_Type);

PrintWriter out = response.getwriter ();

HibernateUtil = new hibernateutil ();

Try {

Session session = hibernateutil.currents;

Transaction tx = session.begintransaction ();

Cat princess = new cat ();

Princess.setname ("Princess");

Princess.setSex ('f');

Princess.setWeight (7.4f);

Session.save (Princess);

TX.comMit ();

Query Query = session.createQuery

"SELECT C from Cat as C where C.sex =: SEX");

Query.setCharacter ("SEX", 'F');

Out.println ("");

Out.println (" servlet1 </ title> </ head>"); out.println ("<body bgcolor = /" # ffffff / ">");</p> <p>Iteerator it = query.Itemate (); it.hasnext ();) {</p> <p>Cat Cat = (CAT) IT.Next ();</p> <p>Out.println ("<p> female cat:" Cat.GetName () "</ p>");</p> <p>}</p> <p>Out.println ("</ Body>");</p> <p>Out.println ("</ html>");</p> <p>Out.close ();</p> <p>TX.comMit ();</p> <p>Hibernateutil.closesis ();</p> <p>} catch (hibernateException e) {</p> <p>E.PrintStackTrace ();</p> <p>}</p> <p>}</p> <p>// Process the http post request</p> <p>Public Void Dopost (httpservletRequest Request, HttpservletResponse Response) THROWS</p> <p>ServletException, IOException {</p> <p>DOGET (Request, Response);</p> <p>}</p> <p>// Process the http put request</p> <p>Public void doput (httpservletRequest Request, httpservletresponse response) throws</p> <p>ServletException, IOException {</p> <p>}</p> <p>// Clean Up Resources</p> <p>Public void destroy () {</p> <p>}</p> <p>}</p> <p>15. Run Servlet1 again, an error.</p> <p>16. Set the properties of the project file, select the menu Project-Project Properties, select Build-Resource-XML in the pop-up dialog box, select the selection button Copy. This step is to copy the XML file in the src directory to the classs directory when rebuilding the project file.</p> <p>17. Run Servlet1 again, you will see the display results after using Hibernate.</p> <p>"Hibernate.cfg.xml not found".</p> <p>When testing with JUnit, you need to add as follows:</p> <p><Property Name = "Connection.driver_class"> Org.hsqldb.jdbcdriver </ Property></p> <p><Property Name = "Connection.URL"> JDBC: HSQLDB: HSQL: // localhost </ property></p> <p><Property Name = "UserName"> SA </ property></p> <p><proty name = "password"> </ property></p> <p><Property Name = "Connection.Provider_class> Net.sf.hibernate.Connection.dbcpConnectionProvider </ property></p> <p><Property Name = "Connection.pool_size"> 50 </ property> <property name = "dbcp.maxactive> 100 </ print"</p> <p><Property Name = "dbcp.whenexhaustedAction> 1 </ property></p> <p><Property Name = "dbcp.maxwait"> 120000 </ property></p> <p><Property Name = "dbcp.maxidle"> 10 </ property></p> <p>Reference resource</p> <p>http://www.hibs/reference/en/html/quickstart.html</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-87716.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="87716" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.045</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'ExpB8_2FqnmMQMlLhP6rk1sxSx83buznrb0r2wnEZGMnsZrAZxvty3uyn1PsoJ3zqNAY53_2BpBtzhQkrZZ3QSI5aQ_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>