resin.conf文件中配置:其中databaseName,username,password,ip地址需要替换<resource-ref>
  <res-ref-name>jdbc/databaseName</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <init-param driver-name="oracle.jdbc.driver.OracleDriver"/>
  <init-param url="jdbc:oracle:thin:@127.0.0.1:1521:databaseName"/>
  <init-param user="username"/>
  <init-param password="password"/>
  <init-param max-connections="100"/>
  <init-param max-idle-time="30"/>
</resource-ref>
写一个db.java文件import java.sql.*;
import com.caucho.sql.*;public class db {  Connection c = null;
  DBPool db = null;
  public db() {
    db = new DBPool();
  }  public Connection getConnection() throws SQLException {
    if (c == null) {
       return c = db.getPool("databaseName").getConnection();
    }
    else {
       return c;
    }
  }  public void clsConnection() throws SQLException {     if(c != null) c.close();   //close Connection ;
     if(db != null) {
        /*if not setName(),the class will throw NullPointerException ,
        *this is a bug in com.caucho.sql.* package maybe;
        *System.out.println(db.toString());
        */
        db.setName("poolName");
        db.close();   //close DBPool;
        //System.out.println(db.toString()); not remove the db.setName();
        //this operation will print: [DBPool poolName]
     }
  }
}

解决方案 »

  1.   

    说明:然后可以用db来建立连接和关闭连接,需要把resin.jar文件放到classpath中
      

  2.   

    我用的是muysql,也能用这种方法吗??
      

  3.   

    修改配置文件就可以了,driver改成mysql就可以了,resin默认驱动就是mysql,只需改一下ip,username,password吧,哦,对了,把驱动的jar文件放到classpath中
      

  4.   

    可是显示错误:C:\AgeHouse\classes\company\CompanyList.java:46: non-static method getConnection()
    cannot be referenced from a static context
    conn = dbpolltest.getConnection();
                                             ^
    1 error
      

  5.   

    <resource-ref>
      <res-ref-name>jdbc/test</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <init-param driver-name="com.caucho.jdbc.mysql.Driver"/>
      <init-param url="jdbc:mysql_caucho://localhost:3306/test"/>
      <init-param user=""/>
      <init-param password=""/>
      <init-param max-connections="20"/>
      <init-param max-idle-time="30"/>
    </resource-ref>我没用过mysql,驱动应该在jdbc-mysql.jar中
      

  6.   

    db dbconn = new db();
    Connection c = db.getConnection();用这样的方式获取连接
      

  7.   

    db dbconn = new db();
    Connection c = dbconn.getConnection();上面写错了
      

  8.   

    呵呵,可以运行了~多谢,那如果建立连接后,一定要显示的关闭它吗?就是必须调用clsConnection() 方法吗??还是建立连接后,这个连接在任何程序里都可用???
      

  9.   

    最好显式关闭连接,否则数据库连接数很容易达到最大,导致out of memory ,所以可以用clsConnection()来关闭连接,另外Connection,Statement,ResultSet等需要也显式关闭此连接任何程序都可以用,不过前提是网络是畅通的