我可以使用 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