5

我可以使用 JDBC 驱动程序和以下 Java 代码很好地连接到 Oracle 11.2 数据库:

  import java.sql.*;
  import javax.sql.DataSource;
  import javax.naming.Context;
  import javax.naming.InitialContext;
  import javax.naming.NamingException;
  import oracle.jdbc.OracleTypes;
  ...
  Connection conn=null;

  // connect to database
  Context context = new InitialContext();
  DataSource ds = (DataSource)context.lookup("jdbc/myPool");
  conn = ds.getConnection();

但现在我需要将选项设置SetFloatAndDoubleUseBinary为 true。请参阅此处的第 4-16 页

http://docs.oracle.com/cd/E14072_01/java.112/e10589.pdf

所以我尝试从这里遵循示例:

http://docs.oracle.com/cd/E11882_01/java.112/e16548/urls.htm

我将代码修改为:

  import java.sql.*;
  import java.util.Properties;
  import javax.sql.DataSource;
  import javax.naming.Context;
  import javax.naming.InitialContext;
  import javax.naming.NamingException;
  import oracle.jdbc.OracleTypes;
  ...
  Connection conn=null;

  // set connection properties
  Properties info = new java.util.Properties();
  info.put ("SetFloatAndDoubleUseBinary","true");

  // connect to database
  Context context = new InitialContext();
  DataSource ds = (DataSource)context.lookup("jdbc/myPool");
  conn = ds.getConnection(info);

我收到以下编译错误:

myClass.java:1145: cannot find symbol
symbol  : method getConnection(java.util.Properties)
location: interface javax.sql.DataSource
          conn = ds.getConnection(info);
                   ^

任何人都知道我可以如何正确设置SetFloatAndDoubleUseBinary这里?

更新 1

更改为:

  import java.sql.*;
  import java.util.Properties;
  import javax.sql.DataSource;
  import javax.naming.Context;
  import javax.naming.InitialContext;
  import javax.naming.NamingException;
  import oracle.jdbc.OracleTypes;
  import oracle.jdbc.*;       // NEW
  import oracle.jdbc.pool.*;  // NEW
  ...
  Connection conn=null;

  // set connection properties
  Properties info = new java.util.Properties();
  info.put ("SetFloatAndDoubleUseBinary","true");

  // connect to database
  Context context = new InitialContext();
  DataSource ds = (DataSource)context.lookup("jdbc/myPool");
  ((OracleDataSource)ds).setConnectionProperties(info); // NEW
  conn = ds.getConnection();                    // NEW

给出以下运行时错误:

stack trace: java.lang.ClassCastException: com.sun.gjc.spi.jdbc40.DataSource40 cannot be cast to oracle.jdbc.pool.OracleDataSource

4

3 回答 3

3

The DataSource is usually configured on the application server. For example, in Glassfish you could set this property in the admin console like this:

enter image description here

and then just call

ds.getConnection() from your client code.

Edit:

If you want to get access to the implementation class of javax.sql.DataSource, you should use DataSource#unwrap method rather than simple cast.

For example:

DataSource ds = (DataSource) ctx.lookup("jdbc/MyPool");
OracleDataSource oracleDS = ds.unwrap(OracleDataSource.class)  

Make sure, you have your jdbc driver jar on classpath.

But then your code will be non-portable, if you wish to switch to another database vendor in the future.

Edit 2:
Also, for Glassfish, refer to the Configuring Specific JDBC Connection Pool Features of Oracle Administration Guide.

于 2012-08-27T20:41:46.560 回答
2

这种形式的getConnection()特定于更通用的接口OracleDataSource并且不存在于更通用的DataSource接口中。

解决方法很简单,替换这一行:

DataSource ds = (DataSource)context.lookup("jdbc/myPool");

有了这个:

OracleDataSource ds = (OracleDataSource)context.lookup("jdbc/myPool");

当然,这意味着从现在开始,您的应用程序将仅适用于 Oracle 数据库。根据您的需要,这可能是也可能不是一个好主意。

于 2012-08-27T20:29:48.693 回答
0

除了亚历克斯的评论,我会尝试

 conn = ds.getConnection();
 conn.setClientInfo("SetFloatAndDoubleUseBinary","true");
于 2012-08-27T20:31:34.727 回答