Hibernate connection MySQL

Source: Internet
Author: User

1 Downloads hibernate-3.6.0 final.zip to any directory, unzip to get hibernate directory

2 download Slf4j-1.7.13.zip to any directory and get slf4j-1.7.13 after decompression

3 Creating a student table in the test library

Msql-localhost-u root–p

Use test

CREATE TABLE student (no char, name varchar (), PrimaryKey (no));

4 Creating a Java Project named Hibernatedemo

5 Adding a Package

Add all packages in the Hibernate\jar

Add Slf4j-nop-1.7.13.jar in slf4j-1.7.13

Add a MySQL driver Mysql-connector-java-5.1.38-bin.jar

6 adding two configuration files and two classes

Hibernate.cfg.xml

Student.java

Student.hbm.xml

Test.java

(1) Add Hibernate.cfg.xml in the SRC directory under Add directory

[Java]View PlainCopy 
  1. <! DOCTYPE hibernate-configuration Public
  2. "-//hibernate/hibernate Configuration DTD 3.0//en"
  3. "HTTP://HIBERNATE.SOURCEFORGE.NET/HIBERNATE-CONFIGURATION-3.0.DTD" >
  4. <session-factory>
  5. < whether a real SQL statement is displayed when the program executes!--
  6. <property name="Show_sql" >true</property>
  7. <!--uses the corresponding "dialect" of SQL, here is the "dialect" of Oracle11--
  8. <property name="dialect" >org.hibernate.dialect.mysqlinnodbdialect
  9. </property>
  10. Driver--> of <!--connected Database
  11. <property name="Connection.driver_class" >
  12. Com.mysql.jdbc.Driver
  13. </property>
  14. <!--database connection url-->
  15. <property name="Connection.url" >
  16. Jdbc:mysql://localhost:3306/test
  17. </property>
  18. <!--user name--
  19. <property name="Connection.username" >root</property>
  20. <!--password--
  21. <property name="Connection.password" >123456</property>
  22. <mapping resource="Student.hbm.xml"/>
  23. </session-factory>

(2) Add Student.java to the COM.ABC package in the SRC directory

[Java]View PlainCopy 
  1. Package com.abc;
  2. Public class Student {
  3. private String NO;
  4. private String name;
  5. Public String Getno () {
  6. return NO;
  7. }
  8. public void Setno (String NO) {
  9. this.no = NO;
  10. }
  11. Public String GetName () {
  12. return name;
  13. }
  14. public void SetName (String name) {
  15. this.name = name;
  16. }
  17. }

(3) Add Student.hbm.xml in src directory

[Java]View PlainCopy 
  1. <?xml version="1.0" encoding="Utf-8"?>
  2. <! DOCTYPE hibernate-mapping
  3. Public "-//hibernate/hibernate Mapping DTD 3.0//en"
  4. "HTTP://HIBERNATE.SOURCEFORGE.NET/HIBERNATE-MAPPING-3.0.DTD" >
  5. package="COM.ABC" >
  6. <class Name="Student" table="Student" >
  7. <id name="NO" >
  8. <generator class="Assigned"/>
  9. </id>
  10. <property name="name" column="name" type="java.lang.String"/>
  11. </class>

(4) Test.java

[Java]View PlainCopy 
  1. Package com.abc;
  2. Import org.hibernate.*;
  3. Import org.hibernate.cfg.*;
  4. Public class Test {
  5. public static void Main (string[] args) {
  6. try {
  7. //Get a Sessionfactory object through the configuration
  8. Sessionfactory SF = new Configuration (). Configure (). Buildsessionfactory ();
  9. //Open a session
  10. Session session = Sf.opensession ();
  11. //Start a transaction
  12. Transaction tx = Session.begintransaction ();
  13. //Create a Student object
  14. Student stu = new Student ();
  15. //Save the Student object to the database using the Save () method of the session
  16. Stu.setno ("2016003");
  17. Stu.setname ("Zhang San");
  18. Session.save (Stu);
  19. //Commit a transaction
  20. Tx.commit ();
  21. //Close Session
  22. Session.close ();
  23. } catch (Exception e) {
  24. E.printstacktrace ();
  25. }
  26. }
  27. }

7 Verification

(1) Run the Test.java, the result is

Hibernate:insert into Student (name, NO) VALUES (?,?)

(2) querying data from MySQL

Hibernate connection MySQL

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.