少年阿賓

          那些青春的歲月

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks

          package com.abin.lee.bean;

          /**
           * Userbean entity. @author MyEclipse Persistence Tools
           */

          public class UserBean implements java.io.Serializable {

           // Fields

           private String id;
           private String username;
           private String password;

           // Constructors

           /** default constructor */
           public UserBean() {
           }

           /** minimal constructor */
           public UserBean(String id) {
            this.id = id;
           }

           /** full constructor */
           public UserBean(String id, String username, String password) {
            this.id = id;
            this.username = username;
            this.password = password;
           }

           // Property accessors

           public String getId() {
            return this.id;
           }

           public void setId(String id) {
            this.id = id;
           }

           public String getUsername() {
            return this.username;
           }

           public void setUsername(String username) {
            this.username = username;
           }

           public String getPassword() {
            return this.password;
           }

           public void setPassword(String password) {
            this.password = password;
           }

          }





          <?xml version="1.0" encoding="utf-8"?>
          <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "<!--
              Mapping file autogenerated by MyEclipse Persistence Tools
          -->
          <hibernate-mapping>
              <class name="com.abin.lee.bean.UserBean" table="USERBEAN" schema="ABING">
                  <id name="id" type="string">
                      <column name="ID" length="80" />
                      <generator class="assigned" />
                  </id>
                  <property name="username" type="string">
                      <column name="USERNAME" length="80" />
                  </property>
                  <property name="password" type="string">
                      <column name="PASSWORD" length="80" />
                  </property>
              </class>
          </hibernate-mapping>






          package com.abin.lee.dao;

          import java.sql.SQLException;

          import org.hibernate.SQLQuery;
          import org.hibernate.Session;
          import org.hibernate.SessionFactory;
          import org.hibernate.Transaction;
          import org.hibernate.cfg.Configuration;

          public class UserDao {
           private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
           private static SessionFactory sessionFactory;
           static {
            sessionFactory=new Configuration().configure().buildSessionFactory();
           }
           public static Session GetSession(){
            Session session=(Session)threadLocal.get();
            if(null == session|| !session.isOpen()){
             if(null==sessionFactory){
              sessionFactory=new Configuration().configure().buildSessionFactory();
             }
             session=(sessionFactory!=null)?sessionFactory.openSession():null;
             threadLocal.set(session);
            }
            return session;
           }
           
           public static int insert(String id,String usr,String pwd) throws SQLException{
            UserDao UserDao=new UserDao();
            Session session=UserDao.GetSession();
            Transaction tx=session.beginTransaction();
            SQLQuery sqlQuery = session.createSQLQuery("{call insertObj(?,?,?)}");
            sqlQuery.setParameter(0, id);
            sqlQuery.setParameter(1, usr);
            sqlQuery.setParameter(2, pwd);
            int result=sqlQuery.executeUpdate();
            System.out.println("result="+result);
            return result;
           }
           
          }






          package com.abin.lee.test;

          import java.sql.SQLException;

          import junit.framework.TestCase;

          import com.abin.lee.dao.UserDao;

          public class UserTest extends TestCase{
           public void test() throws SQLException{
            UserDao user=new UserDao();
            int success=user.insert("abin","abing","bing");
            System.out.println("success="+success);
           }
          }








          <?xml version='1.0' encoding='UTF-8'?>
          <!DOCTYPE hibernate-configuration PUBLIC
                    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                    "

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

           <session-factory>
            <property name="dialect">
             org.hibernate.dialect.OracleDialect
            </property>
            <property name="connection.url">
             jdbc:oracle:thin:@localhost:1521:xe
            </property>
            <property name="connection.username">abing</property>
            <property name="connection.password">abing</property>
            <property name="connection.driver_class">
             oracle.jdbc.driver.OracleDriver
            </property>
            <property name="myeclipse.connection.profile">
             OracleConnection
            </property>
            <mapping resource="com/abin/lee/bean/UserBean.hbm.xml" />
             
              </session-factory>

          </hibernate-configuration>








          主站蜘蛛池模板: 麦盖提县| 广元市| 建宁县| 南充市| 丹巴县| 肥东县| 右玉县| 保靖县| 格尔木市| 兖州市| 阜新市| 新野县| 宁陕县| 张家港市| 防城港市| 同仁县| 肥乡县| 朝阳区| 鄂托克前旗| 朔州市| 衡水市| 苏尼特左旗| 临泉县| 克山县| 博客| 高台县| 青海省| 纳雍县| 江北区| 渑池县| 佳木斯市| 裕民县| 海南省| 新巴尔虎右旗| 汾阳市| 荣昌县| 城步| 微山县| 贡嘎县| 遵化市| 淳安县|