Simple manual Hibernate program Example _jsp programming

Source: Internet
Author: User
Tags commit object object rollback sessions

This article describes a simple manual Hibernate program example. Share to everyone for your reference. Specifically as follows:

Today I learned the next hibernate, wrote a small manual program, summed up,

First create the database table:

Copy Code code as follows:
CREATE TABLE Increment_testr (ID bigint not NULL, name char (), primary key (ID));

Eclipse, new project.

Create a new database table map, which is done manually:

Incrementtester.java

public class Incrementtester {
  private Long ID;
  private String name;
  Public Incrementtester () {} public
  Incrementtester (String name) {
    this.name = name;
  }
  Public Long GetId () {return
    this.id;
  }
  private void SetId (Long id) {
    this.id = ID;
  }
  Public String GetName () {return
    this.name;
  }
  public void SetName (String name) {
    this.name = name;
  }
}

mapping XML files corresponding to writing

IncrementTester.hbm.xml

Public "-//hibernate/hibernate Mapping DTD 3.0//en"
"Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
Private

Class Bussinessservice for implementing specific functions

Import java.lang.reflect.*;
Import org.hibernate.*;
Import org.hibernate.cfg.*;
Import java.io.*;
Import java.sql.*;
Import java.util.*;
  public class Bussinessservice {public static sessionfactory sessionfactory;
      static{try{Configuration config = new Configuration (). Configure ();
    Sessionfactory = Config.buildsessionfactory ();
    }catch (Exception e) {e.printstacktrace ();
    } public void Findallobjects (String className) {Sessions session = Sessionfactory.opensession ();
    Transaction tx = NULL;
      try{tx = Session.begintransaction ();
      List objects = Session.createquery ("from" +classname). List ();
        for (Iterator it = Objects.iterator (); It.hasnext ();) {Long id = new Long (0);
        Incrementtester xx = (incrementtester) it.next ();
        Id=xx.getid ();
      System.out.println ("ID of" +classname+ ":" +id+ "Name:" +xx.getname ());
    } tx.commit ();
    }catch (Exception e) {e.printstacktrace ();
   }finally{   Session.close ();
    } public void Saveobject (Object object) {Session session = Sessionfactory.opensession ();
    Transaction tx = NULL;
      try{tx = Session.begintransaction ();
      Session.save (object);
    Tx.commit ();
      }catch (Exception e) {e.printstacktrace ();
      if (TX!= null) {tx.rollback ();
    }}finally{Session.close ();
    } public void Deleteallobject (String className) {Sessions session = Sessionfactory.opensession ();
    Transaction tx = NULL;
      try{tx = Session.begintransaction ();
      Query query=session.createquery ("delete from" +classname);
      Query.executeupdate ();
    Tx.commit ();
      }catch (Exception e) {e.printstacktrace ();
      if (tx!=null) {tx.rollback ();
    }}finally{Session.close ();

 }
  }
}

To implement the main function:

public class Test {
  /**
   * @param args */public
  static void Main (string[] args) {
    //TODO Auto-generat Ed method stub
    String name= "Incrementtester";
    Bussinessservice AA = new Bussinessservice ();
    Aa.deleteallobject (name);
    Object O1 = null;
    try {
      O1 = class.forname (name). newinstance ();
      ((Incrementtester) O1). SetName ("Caijie");
      Aa.saveobject (O1);
      O1 = Class.forName (name). newinstance ();
      ((Incrementtester) O1). SetName ("Gufeng");
      Aa.saveobject (O1);
    } catch (Exception e) {
      e.printstacktrace ();
    }
    Aa.findallobjects (name);
  }

Hibernate configuration file: Hibernate.cfg.xml

Public "-//hibernate/hibernate Configuration dtd//en"
"Http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
Org.hibernate.dialect.MySQLDialect
Com.mysql.jdbc.Driver
Jdbc:mysql://localhost:3306/test
Root
Root
True

Add the corresponding library, run successfully after the result:

Hibernate:delete from Increment_testr
hibernate:select Max (IDs) from Increment_testr to
Hibernate:insert into Increment_testr (NAME, ID) VALUES (?,?)
Hibernate:insert into Increment_testr (NAME, ID) VALUES (?,?)
Hibernate:select incrementt0_.id as id0_, incrementt0_.name as name0_ from Increment_testr incrementt0_
ID of Increme Nttester:1 Name:caijie
ID of Incrementtester:2 Name:gufeng

I hope this article will help you with the JSP program design.

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.