Using hibernate to connect SQL Server 2000

Source: Internet
Author: User
Tags define connect sql net reflection return string version
Server
The following code is tested through the JDK 5.0, Hibernate 2.1, SQL Server SP3.









The first use of hibernate as a persistent layer, the feeling is still more complex to use, especially debugging is very inconvenient. Although the Hibernate based on reflection mechanism is very flexible, it obviously creates a barrier to the tracking code and gives too little information about the exception. Personally feel that there is a lot of room for improvement, such as Java new annotation syntax, whether it can be used to define ORM, and replace the form of hbm.xml.

All right, get to the point.

First, you must configure the database, as an example of the operations in the database Yufan.

CREATE TABLE CUSTOMER (CID INTEGER NOT null PRIMARY KEY, USERNAME VARCHAR (a) not NULL, PASSWORD VARCHAR (12));







Then there is a data object that must provide a read-write property method for each of its fields, and Hibernate will retrieve it with reflection.

Customer.java

public class Customer {







private int id;







Private String username;







private String password;







public int getId () {







return ID;







}







Public String GetPassword () {







return password;







}







Public String GetUserName () {







return username;







}







public void setId (int id) {







This.id = ID;







}







public void SetPassword (String password) {







This.password = password;







}







public void Setusername (String username) {







This.username = Username;







}







}









And then there's the hibernate mapping Customer.hbm.xml









<?xml version= "1.0" encoding= "Utf-8"?>







<! DOCTYPE hibernate-mapping Public







"-//hibernate/hibernate Mapping dtd//en"







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














<class name= "Customer" table= "Customer" proxy= "Customer" >







<id name= "id" column= "CID" >







<generator class= "Increment"/>







</id>







<property name= "username" column= "username"/>







<property name= "password" column= "password"/>







</class>
















Classes and mappings are combined to define ORM.









The following are hibernate's configuration files, including database connections, mapping file references, and so on. File name must be Hibernate.cfg.xml









<?xml version= "1.0" encoding= "Utf-8"?>







<! DOCTYPE hibernate-configuration







Public "-//hibernate/hibernate Configuration dtd//en"







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






















<session-factory name= "Java:/hibernate/hibernatefactory" >







<property name= "Show_sql" >true</property>







<property name= "Connection.driver_class" >







Com.jnetdirect.jsql.JSQLDriver







</property>







<property name= "Connection.url" >







Jdbc:jsqlconnect://localhost:1433;database=yufan;







</property>







<property name= "Connection.username" >







Sa







</property>







<property name= "Connection.password" >







Yufan







</property>







<property name= "dialect" >







Net.sf.hibernate.dialect.SQLServerDialect







</property>







<mapping resource= "Customer.hbm.xml"/>







</session-factory>
















Important property includes Connection.driver_class, which specifies the JDBC database driver. Connection.url the URL of the database. I'm using Jsqldriver, which is more powerful than MS's JDBC driver. However, it is important to note that when you specify a database, you must use the database= or databasename= syntax, and the databaseName error. The driver for MS does not have this problem. I was in a daze when I first met the problem ... Finally found is so ...









In this file, it is not possible to use XML annotations, which is a hibernate flaw.

And finally, Test.java.

Import net.sf.hibernate.*;







Import net.sf.hibernate.cfg.*;















public class Test {















public static void Main (string[] args) {







try {







Sessionfactory SF = new Configuration (). Configure (). Buildsessionfactory ();







Session session = Sf.opensession ();







Transaction tx = Session.begintransaction ();







for (int i = 0; i < i++) {







Customer customer = new Customer ();







Customer.setusername ("customer" + i);







Customer.setpassword ("Customer");







Session.save (customer);







}















Tx.commit ();







Session.close ();







}







catch (Hibernateexception e) {







E.printstacktrace ();







}







}







}















OK, add a reference to the Hibernate library file in the project, compile and execute, and you'll find the newly added record in the database.








The code section is taken from the simplest hibernate introduction by WATANO_CC, based on SQL Server changes.



Related Article

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.