欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

idea 4.5 + tomcat 4 IDEATomcatOracleHibernateJava 

程序员文章站 2022-07-13 14:36:21
...
0、拷贝tomcat5.0的common/lib/下的文件到tomcat4.1的common/lib/下
commons-dbcp-1.2.jar
commons-el.jar
commons-pool-1.2.jar

1、Hibernate 整到Module Libraries中去
cglib-full-2.0.1.jar
commons-collections-2.1.jar
commons-lang-1.0.1.jar
commons-logging-1.0.3.jar
dom4j-1.4.jar
ehcache-0.7.jar
hibern8ide.jar
hibernate-tools.jar
hibernate2.jar
jdom.jar
jta.jar
odmg-3.0.jar
oscache-2.0.jar

2、ojdbc14.jar 整到JDK的Classpath中去(Hibernate中的Clob字段)
(http://otn.oracle.com/software/tech/java/sqlj_jdbc/content.html)
(http://otn.oracle.com/software/tech/java/sqlj_jdbc/htdocs/jdbc9201.html)
并且要拷贝到tomcat4.1的common/lib/目录中

1、Blob和BLOB的问题

java.sql.Blob
oracle.sql.BLOB

这两个blob仅仅是大小写不同,但是差异很大,java.sql.Blob是一个接口,而oracle.sql.BLOB是一个实现java.sql.Blob的类,并且还有很多扩展的属性和方法,注意不要搞混了。


2、JDBC2.0和JDBC3.0的问题

classes12.zip实现了JDBC2.0(JDK1.3),而JDBC2.0对于Blob的操作只有读,没有写,所以classes12.zip只好自己扩展了一套对Blob进行写的API,我的例子就是用了这套API。

ojdbc14.jar实现了JDBC3.0(JDK1.4),JDBC3.0已经包括了对Blob写的操作,而ojdbc14.jar也实现了该API。

jconn2d.jar sybaseJDBCDriver

//3、zpt.jar 整到Module Libraries中去(Hibernate)
其中要包含javax/transaction/Synchronization
(取至weblogic.jar)
3、jta.jar 从oracle\ora90\jlib\下整到Module Libraries中去(Hibernate)
其中要包含javax/transaction/Synchronization

4、DataSource DataSource ds=(DataSource)new InitialContext().lookup("java:comp/env/jdbc/pmis");
配置tomcat4.1的conf/server.xml文件,加入如下:

         
<Context path="/pmis" docBase="pmis">
              <Resource name="jdbc/pmis" scope="Shareable" type="javax.sql.DataSource"/>
              <ResourceParams name="jdbc/pmis">
                  <parameter>
                      <name>factory</name>
                      <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
                      </parameter>
                  <!-- DBCP database connection settings -->
                  <parameter>
                      <name>url</name>
                      <value>jdbc:oracle:thin:@10.161.161.135:1521:oradb</value>
                  </parameter>
                  <parameter>
                      <name>driverClassName</name>
                      <value>oracle.jdbc.driver.OracleDriver</value>
                  </parameter>
                  <parameter>
                      <name>username</name>
                      <value>pmis</value>
                  </parameter>
                  <parameter>
                      <name>password</name>
                      <value>pmiswh</value>
                  </parameter>
                  <!-- DBCP connection pooling options -->
                  <parameter>
                      <name>maxWait</name>
                      <value>3000</value>
                  </parameter>
                  <parameter>
                      <name>maxIdle</name>
                      <value>100</value>
                  </parameter>
                  <parameter>
                      <name>maxActive</name>
                      <value>10</value>
                  </parameter>
              </ResourceParams>
          </Context>


5、Clob

           
public void add(Cat obj); throws Exception{
                Session sess=HibernateUtil.currentSession();;
                Transaction tx=sess.beginTransaction();;
        
                obj.setMes(Hibernate.createClob(" "););;
                sess.save(obj);;
                sess.flush();;
        
                sess.refresh(obj, LockMode.UPGRADE);;
                CLOB clob=(CLOB);obj.getMes();;
                Writer pw=clob.getCharacterOutputStream();;
                pw.write(obj.getMesString(););;
                pw.flush();;
                pw.close();;
                sess.flush();;
        
                tx.commit();;
                HibernateUtil.closeSession();;
            }
            public void add1(Cat obj); throws Exception{
                Context ctx=new InitialContext();;
                DataSource ds=(DataSource);ctx.lookup("java:comp/env/jdbc/pmis");;
                Connection conn=ds.getConnection();;
                conn.setAutoCommit(false);;
                Statement stmt=conn.createStatement();;
                stmt.executeUpdate("update cat set mes=empty_clob(); where id=2079");;
                ResultSet rs=stmt.executeQuery("select mes from cat where id=2079 for update");;
                if(rs.next(););{
                    //CLOB clob=((OracleResultSet);rs);.getCLOB("mes");;
                    CLOB clob=(CLOB);rs.getClob("mes");;
                    Writer pw=clob.getCharacterOutputStream();;
                    pw.write(obj.getMesString(););;
                    pw.flush();;
                    pw.close();;
                }
                rs.close();;
                stmt.close();;
                conn.commit();;
                conn.close();;
            }
            public List lists(); throws Exception{
                Session sess=HibernateUtil.currentSession();;
                Query query=sess.createQuery("from Cat");;
                List list=query.list();;
                HibernateUtil.closeSession();;
                for(int i=0; i<list.size();; i++);{
                    Cat obj=(Cat);list.get(i);;
                    obj.setMesString(obj.getMes();==null?null:obj.getMes();.getSubString(1, (int);obj.getMes();.length();););;
                }
                return list;
            }



package pmis.common.hibAccess;

import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;

public class HibernateUtil {

    private static final SessionFactory sessionFactory;

    static 
    {
        try 
        {
            sessionFactory = new Configuration();.configure();.buildSessionFactory();;
        } 
        catch (HibernateException ex); 
        {
            throw new RuntimeException("Exception building SessionFactory: " + ex.getMessage();, ex);;
        }
    }

    public static final ThreadLocal session = new ThreadLocal();;

    public static Session currentSession(); throws HibernateException 
    {
        Session s = (Session); session.get();;
        // Open a new Session, if this Thread has none yet
        //if (s == null); 
        {
            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();;
        }
    }
}



package test;

import pmis.common.hibAccess.HibernateUtil;

import javax.sql.DataSource;
import javax.naming.InitialContext;
import javax.naming.Context;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Connection;
import java.io.Writer;

import net.sf.hibernate.Session;
import ammeter.valueObject.TsBmRelation;
import oracle.sql.CLOB;

/**
 * Created by IntelliJ IDEA.
 * User: zpt
 * Date: 2005-1-26
 * Time: 9:03:54
 * To change this template use File | Settings | File Templates.
 */
public class test {
    public test(); throws Exception{
        t2();;
    }
    private void t1(); throws Exception{
        Context iniContext=new InitialContext();;
        Context envContext=(Context);iniContext.lookup("java:comp/env");;
        DataSource ds=(DataSource);envContext.lookup("jdbc/pmis/oracle");;
        Connection conn=ds.getConnection();;
        Statement stmt=conn.createStatement();;
        ResultSet rs=stmt.executeQuery("select name from Cat");;
        String s="";
        while(rs.next(););{
            s+=rs.getString("name");;
        }
        rs.close();;
        stmt.close();;
        conn.close();;
    }
    private void t2(); throws Exception{
        Session sess=HibernateUtil.currentSession();;
        TsBmRelation obj=(TsBmRelation);sess.get(TsBmRelation.class, new Long(1971););;
        HibernateUtil.closeSession();;
    }
    private void t3(); throws Exception{
        net.sf.hibernate.HibernateException
        Context iniContext=new InitialContext();;
        Context envContext=(Context);iniContext.lookup("java:comp/env");;
        DataSource ds=(DataSource);envContext.lookup("jdbc/pmis/oracle");;
        Connection conn=ds.getConnection();;
        conn.setAutoCommit(false);;
        Statement stmt=conn.createStatement();;
        stmt.executeUpdate("update cat set mes=empty_clob(); where id=2148");;
        ResultSet rs=stmt.executeQuery("select mes from cat where id=2148 for update");;
        if(rs.next(););{
            //CLOB clob=((OracleResultSet);rs);.getCLOB("mes");;
            CLOB clob=(CLOB);rs.getClob("mes");;
            Writer pw=clob.getCharacterOutputStream();;
            pw.write("asdfasdf");;
            pw.flush();;
            pw.close();;
        }
        rs.close();;
        stmt.close();;
        conn.commit();;
        conn.close();;
    }
    private void t4(); throws Exception{
        Session sess=HibernateUtil.currentSession();;

        HibernateUtil.closeSession();;
    }
}