Hibernate database operations

Source: Internet
Author: User
// Import the imported hibernate package
Import net. SF. hibernate. cfg. configuration;
Import net. SF. hibernate. hibernateexception;
Import net. SF. hibernate. sessionfactory;
Import net. SF. hibernate. Session;
Import net. SF. hibernate. transaction;
Import java. util. List;
Import com. hygj. Bean. users;
Public class hibernateutil {
// Defines the private variable CFG to capture the hibernate. cfg. xml preparation file.
Private net. SF. hibernate. cfg. Configuration CFG = NULL;
// Define the variable SF to manage connections
Private net. SF. hibernate. sessionfactory Sf = NULL;
// Define the session for specific operations
Private net. SF. hibernate. Session session = NULL;
// Manage multiple operations of a user and manage things
Private net. SF. hibernate. Transaction TR = NULL;

Public hibernateutil (){
Try {
CFG = new net. SF. hibernate. cfg. configuration (). Configure ();
Sf = cfg. buildsessionfactory ();
}
Catch (hibernateexception ex ){
}
}

// Query all
Public void chaxun (){
System. Out. println ("---- chaxun ----");
// Generate a response
Try {
This. Sessions = This. SFF. opensession ();
Session = SF. opensession ();
// Start the transaction
This. TRS = This. Sessions. begintransaction ();
Tr = session. begintransaction ();
// Generate a query
Net. SF. hibernate. query = session. createquery ("from users ");
// Obtain the result set
List list = query. List ();
Java. util. iterator it = List. iterator ();
While (it. hasnext ()){
Users user = (users) it. Next ();
System. Out. println ("ID:" + User. GETID ());
System. Out. println ("name:" + User. GetUserName ());
System. Out. println ("Password:" + User. GetPassword ());
System. Out. println ("--------");
}
// The execution is complete and the transaction is committed
Tr. Commit ();
}
Catch (hibernateexception ex ){
}
Finally {
Try {
Session. Close ();
}
Catch (hibernateexception ex1 ){
}
}
}

// Query by serial number
Public void chaxun (int id ){
Try {
Session = SF. opensession ();
Tr = session. begintransaction ();
// Generate a query, which needs to be queried by ID
Net. SF. hibernate. query = session. createquery ("from users where id =? ");

// Net. SF. hibernate. query que = session. createquery ("from users where id =: ID ");

// Assign values to parameters before querying
Query. setinteger (0, ID );
// Que. setinteger ("ID", ID );
// List = que. List ();
List list = query. List ();
// Perform null object verification
If (list. Size ()> 0 ){
Users user = (users) list. Get (0 );
System. Out. println ("ID:" + User. GETID ());
System. Out. println ("name:" + User. GetUserName ());
System. Out. println ("Password:" + User. GetPassword ());
System. Out. println ("--------");

}
Else {
System. Out. println ("no record ");
}
Tr. Commit ();
}
Catch (hibernateexception ex ){
}
Finally {
Try {
Session. Close ();
}
Catch (hibernateexception ex1 ){
}
}

}

// Fuzzy search
Public void chaxun (string name ){
Try {
Session = SF. opensession ();
Tr = session. begintransaction ();
// Generate a query, which needs to be queried by ID
Net. SF. hibernate. query = session. createquery ("from users where username like? ");

// Net. SF. hibernate. query que = session. createquery ("from users where id =: ID ");

// Assign values to parameters before querying
String STR = "%" + name + "% ";
Query. setstring (0, STR );
// Que. setinteger ("ID", ID );
// List = que. List ();
List list = query. List ();
// Perform null object verification
If (list. Size ()> 0 ){
For (INT I = 0; I <list. Size (); I ++ ){
Users user = (users) list. Get (I );
System. Out. println ("ID:" + User. GETID ());
System. Out. println ("name:" + User. GetUserName ());
System. Out. println ("Password:" + User. GetPassword ());
System. Out. println ("--------");
}

}
Else {
System. Out. println ("no record ");
}
Tr. Commit ();
}
Catch (hibernateexception ex ){
}
Finally {
Try {
Session. Close ();
}
Catch (hibernateexception ex1 ){
}
}

}

// Add data
Public void add (string name, string PWD ){
Try {
Session = SF. opensession ();
Tr = session. begintransaction ();
// Instance the new Bean class
Users user = new users ();
// Assign values to Bean
User. setusername (name );
User. setpassword (PWD );
// Save and submit
Session. Save (User );
Tr. Commit ();
}
Catch (hibernateexception ex ){
}
Finally {
Try {
Session. Close ();
}
Catch (hibernateexception ex1 ){
}
}

}

// Modify data
Public void Update (int id, string PWD ){
Try {
Session = SF. opensession ();
Tr = session. begintransaction ();
// Locate the data first
Net. SF. hibernate. query = session. createquery ("from users where id =? ");

Query. setinteger (0, ID );
List list = query. List ();
If (list. Size ()> 0 ){
Users user = (users) list. Get (0 );
// Password
User. setpassword (PWD );
// Save it back
Session. Update (User );
Session. Flush ();
Tr. Commit ();
System. Out. println ("update OK ");
}
Else {
System. Out. println ("no record can be updated ");
}
}
Catch (hibernateexception ex ){
}
Finally {
Try {
Session. Close ();
}
Catch (hibernateexception ex1 ){
}
}

}

// Delete data
Public void Delete (int id ){
Try {
Session = SF. opensession ();
Tr = session. begintransaction ();
// Locate the data first
Net. SF. hibernate. query = session. createquery ("from users where id =? ");

Query. setinteger (0, ID );
List list = query. List ();
If (list. Size ()> 0 ){
Users user = (users) list. Get (0 );
// Delete data
Session. Delete (User );
Session. Flush ();
Tr. Commit ();
System. Out. println ("delete OK ");
}
Else {
System. Out. println ("no record can be deleted ");
}
}
Catch (hibernateexception ex ){
}
Finally {
Try {
Session. Close ();
}
Catch (hibernateexception ex1 ){
}
}

}

Public static void main (string [] ARGs ){
Hibernateutil hibernateutil1 = new hibernateutil ();
// Hibernateutil1.add ("Subin", "123456 ");
// Hibernateutil1.chaxun (3 );
// Hibernateutil1.update (3, "9876543 ");
// Hibernateutil1.chaxun (3 );
Hibernateutil1.delete (4 );
}

}

The connection is completed in the configuration file 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>
<Hibernate-configuration>
<Session-factory>
<Property name = "dialect">
Net. SF. hibernate. dialect. sqlserverdialect
</Property>
<Property name = "show_ SQL"> true </property>
<Property name = "connection. driver_class">
Com. Microsoft. JDBC. sqlserver. sqlserverdriver
</Property>
<Property name = "connection. url">
JDBC: Microsoft: sqlserver: // localhost: 1433; databasename = pubs;
</Property>
<Property name = "connection. username">
SA
</Property>
<Property name = "connection. Password">

</Property>

<Mapping Resource = "com/hygj/bean/users. HBM. xml"/>
</Session-factory>
</Hibernate-configuration>

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.