Jsp+Servlet在用连接池连接Oracle数据库时奇怪的异常

BernardXiong 发布于 2011/09/08 21:33
阅读 1K+
收藏 0

各位大虾,我是一名学徒,在做Jsp+Servlet+JDBC的时候,测试数据库连接池始终有问题。。。我的数据库连接池代码如下:

public class ConnectionPool {
 private static BasicDataSource dataSource = null;

 private ConnectionPool() {
 }

 public static void init() {
  if (dataSource != null) {
   try {
    dataSource.close();
   } catch (Exception e) {
    e.printStackTrace();
   }
   dataSource = null;
  }
  try {
   Properties p = new Properties();
   p.setProperty("driverClassName", "oracle.jdbc.driver.OracleDriver");
   p.setProperty("url", "jdbc:oracle:thin:@localhost:1521:XE");
   p.setProperty("username", "system");
   p.setProperty("password", "xiongyun");
   p.setProperty("maxActive", "30");
   p.setProperty("maxIdle", "10");
   p.setProperty("maxWait", "1000");
   p.setProperty("removeAbandoned", "false");
   p.setProperty("removeAbandonedTimeout", "120");
   p.setProperty("testOnBorrow", "true");
   p.setProperty("logAbandoned", "true");
   dataSource = (BasicDataSource) BasicDataSourceFactory
     .createDataSource(p);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 public static synchronized Connection getConnection() throws SQLException {
  if (dataSource == null) {
   init();
  }
  Connection conn = null;
  if (dataSource != null) {
   conn = dataSource.getConnection();
  }
  return conn;
 }

 public static void main(String[] args) throws SQLException {
  Connection connection = getConnection();
  System.out.println(connection);
 }
}
加入了如下架包:

ojdbc14.jar
commons-dbcp-1.4.jar
commons-dbcp-1.4-javadoc.jar
commons-dbcp-1.4-sources.jar
commons-pool-1.5.6.jar
commons-pool-1.5.6-javadoc.jar
commons-pool-1.5.6-sources.jar

当Tomcat6启动成功后,我就单独测试了这个类,打印连接结果。然后就报错。错误如下:

Exception in thread "main" java.lang.NullPointerException
 at java.lang.String.<init>(String.java:176)
 at oracle.sql.CharacterSet.AL32UTF8ToString(CharacterSet.java:1517)
 at oracle.jdbc.driver.DBConversion.CharBytesToString(DBConversion.java:589)
 at oracle.jdbc.driver.DBConversion.CharBytesToString(DBConversion.java:542)
 at oracle.jdbc.driver.T4CTTIoauthenticate.receiveOauth(T4CTTIoauthenticate.java:816)
 at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:362)
 at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:414)
 at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165)
 at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35)
 at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:801)
 at org.apache.commons.dbcp.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:38)
 at org.apache.commons.dbcp.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:582)
 at org.apache.commons.dbcp.BasicDataSource.validateConnectionFactory(BasicDataSource.java:1556)
 at org.apache.commons.dbcp.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:1545)
 at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1388)
 at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
 at com.xiong.oa.dao.ConnectionPool.getConnection(ConnectionPool.java:51)
 at com.xiong.oa.dao.ConnectionPool.main(ConnectionPool.java:57)

希望各位大虾能帮我解决下啊,困扰了很久了。。因为连接数据库不成功,一直没法做后面的练习。。。谢谢大家了。。。。

以下是问题补充:

@BernardXiong:我用的是MyEclipse8.5 (2011/09/08 21:40)
@BernardXiong:后来我换MyEclipse9.0也一样的错误。。觉得是数据库设置有问题,但不知道哪个设置有问题,求大侠们帮帮忙吧 (2011/09/11 20:29)
加载中
0
红薯
红薯
不熟悉 oracle 啊,帮顶了
0
BernardXiong
BernardXiong
哦,好吧。谢谢了。。
0
BernardXiong
BernardXiong

引用来自“红薯”的答案

不熟悉 oracle 啊,帮顶了
红薯大哥。。我自己解决了哈。。。的确是Oracle 10g JDBC Driver的一个Bug。。。11g的Driver修复了那个Bug...我直接换成11g的驱动就没有报错了。。。。
返回顶部
顶部