Connect to MYSQL based on Hibernate for employee information query

Source: Internet
Author: User
Create a JAVA project, select "JAVA build path" in the project properties, and click "add external JAR" in the library tag"

Add the required JAR for Hibernate (also add the MYSQL driver JAR) as follows:


antlr.jar
cglib.jar
asm.jar
asm-attrs.jars
commons-collections.jar
commons-logging.jar
hibernate3.jar
jta.jar
dom4j.jar
log4j.jar

 
1. Create hibernate. cfg. xml as follows:

<? Xml version = '1. 0' encoding = 'utf-8'?>
<! DOCTYPE hibernate-configuration PUBLIC
"-// Hibernate/Hibernate Configuration DTD 3.0 // EN"
Http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd>

<! -- Generated by MyEclipse Hibernate Tools. -->
<Hibernate-configuration>

<Session-factory>
<Property name = "connection. username"> test </property>
<Property name = "connection. url">
Jdbc: mysql: // localhost: 3306/test
</Property>
<Property name = "dialect">
Org. hibernate. dialect. MySQLDialect
</Property>
<Property name = "myeclipse. connection. profile">
Mysqlprofile
</Property>
<Property name = "connection. password"> test </property>
<Property name = "connection. driver_class">
Org. gjt. mm. mysql. Driver
</Property>
<Property name = "max_fetch_depth"> 1 </property>
<Mapping resource = "query/model. hbm. xml"/>

</Session-factory>

</Hibernate-configuration>

2. Create the configuration file corresponding to the database table

<? Xml version = "1.0" encoding = "ISO-8859-1"?>
<! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate Mapping DTD 3.0 // EN"
Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>
<! --
Mapping file autogenerated by MyEclipse-Hibernate Tools
-->
<Hibernate-mapping package = "query">

<Class name = "query. Employee" table = "employee" catalog = "test">
<Id name = "id" type = "java. lang. Integer">
<Column name = "id"/>
<Generator class = "identity"/>
</Id>

<Property name = "name" type = "java. lang. String">
<Column name = "name"/>
</Property>

<Property name = "department" type = "java. lang. String">
<Column name = "department"/>
</Property>
<Property name = "salary" type = "java. lang. Double">
<Column name = "salary"/>
</Property>

</Class>
</Hibernate-mapping>
3. Create a class that encapsulates employee information

Package query;

Public class Employee {

Private int id;
Private String name;
Private String department;
Private double salary;

Public void setId (int id) {this. id = id ;}
Public void setName (String name) {this. name = name ;}
Public void setDepartment (String department) {this. department = department ;}
Public void setSalary (double salary) {this. salary = salary ;}

Public int getId () {return id ;}
Public String getName () {return name ;}
Public String getDepartment () {return department ;}
Public double getSalary () {return salary ;}

}
4. Create a SESSION management class

Package query;

Import org. hibernate. HibernateException;
Import org. hibernate. Session;
Import org. hibernate. SessionFactory;
Import org. hibernate. cfg. Configuration;

Import org. apache. commons. logging. Log;
Import org. apache. commons. logging. LogFactory;

Public class HibernateUtil {

Private static Log log = LogFactory. getLog (HibernateUtil. class );

Private static final SessionFactory sessionFactory;
Static {
Try {



SessionFactory = new Configuration (). configure (). buildSessionFactory ();


} Catch (Throwable ex ){
Ex. printStackTrace (); // TODO: handle exception
Throw new ExceptionInInitializerError (ex );
}

}

Public static final ThreadLocal session = new ThreadLocal ();

Public static Session currentSession () throws HibernateException {
Session s = (Session) session. get ();
If (s = null |! S. isOpen ()){
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 ();
}
}

5. Create an employee information query class:

Package query;
Import java. awt. Container;
Import java. awt. GridLayout;
Import java. awt. event. ActionEvent;
Import java. awt. event. ActionListener;
Import java. util. Iterator;
Import java. util. List;

Import javax. swing .*;

Import org. hibernate. HibernateException;
Import org. hibernate. Query;
Import org. hibernate. Session;
Import org. hibernate. Transaction;
Public class QueryFrame extends JFrame implements ActionListener {


/**
*
*/
Private static final long serialVersionUID = 1L;
JTextField jf = new JTextField (20 );
JButton find = new JButton ("query ");
JPanel panel_find = new JPanel ();
JPanel panel_content = new JPanel ();
Public List getEmployees (String empname) throws HibernateException {

Session session = HibernateUtil. currentSession ();
Transaction tx = session. beginTransaction ();
// String SQL = "from Emloyee where name like '%" + empname + "% '";

String SQL = "from Employee ";

Query query = session. createQuery (SQL );
List list = query. list ();
Tx. commit ();
HibernateUtil. closeSession ();
Return list;
}

Public QueryFrame (){
This. setTitle ("query system ");
Container c = this. getContentPane ();

Jf. addActionListener (this );
Find. addActionListener (this );
Panel_find.add (jf );
Panel_find.add (find );

C. add (panel_find, "North ");
C. add (panel_content, "Center ");

This. setSize (400,400 );
This. setLocation (200,100 );
This. setVisible (true );


}



Public void actionreceivmed (ActionEvent e ){
Panel_content.removeAll ();
String content = jf. getText ();
JLabel label1 = new JLabel ("employee ID" + "----" + "employee name" + "----" + "Employee Department" + "----" + "employee salary ");
JPanel panel = new JPanel ();
Panel. add (label1 );
Int I = 0;
For (Iterator it = this. getEmployees (content). iterator (); it. hasNext ();){
Employee emp = (Employee) it. next ();
JLabel label = new JLabel (emp. getId () + "----" + emp. getName () + "----" + emp. getDepartment () + "----" + emp. getSalary ());

Panel. add (label );
Panel_content.add (panel );
I ++;
}
Panel_content.setLayout (new GridLayout (I, 1 ));
Panel_content.validate ();
}







Public static void main (String [] args ){
New QueryFrame (); // TODO Auto-generated method stub

}

}

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.