oracle 如何设置连接属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12148976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I set a connection property?
提问by ggkmath
I can connect fine to an Oracle 11.2 database using JDBC driver and the following Java code:
我可以使用 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();
But now I need to set the option SetFloatAndDoubleUseBinary
to true. See page 4-16 here
但现在我需要将该选项设置SetFloatAndDoubleUseBinary
为 true。请参阅此处的第 4-16 页
http://docs.oracle.com/cd/E14072_01/java.112/e10589.pdf
http://docs.oracle.com/cd/E14072_01/java.112/e10589.pdf
So I try to follow examples from here:
因此,我尝试遵循此处的示例:
http://docs.oracle.com/cd/E11882_01/java.112/e16548/urls.htm
http://docs.oracle.com/cd/E11882_01/java.112/e16548/urls.htm
and I modify the code as:
我将代码修改为:
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);
and I'm getting the following compile error:
我收到以下编译错误:
myClass.java:1145: cannot find symbol
symbol : method getConnection(java.util.Properties)
location: interface javax.sql.DataSource
conn = ds.getConnection(info);
^
Anyone know how I can properly set SetFloatAndDoubleUseBinary
here?
有谁知道我如何SetFloatAndDoubleUseBinary
在这里正确设置?
UPDATE 1
更新 1
Changing to:
更改为:
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
gives the following run time error:
给出以下运行时错误:
stack trace: java.lang.ClassCastException: com.sun.gjc.spi.jdbc40.DataSource40 cannot be cast to oracle.jdbc.pool.OracleDataSource
stack trace: java.lang.ClassCastException: com.sun.gjc.spi.jdbc40.DataSource40 cannot be cast to oracle.jdbc.pool.OracleDataSource
回答by jFrenetic
The DataSource
is usually configured on the application server. For example, in Glassfish
you could set this property in the admin console like this:
在DataSource
通常应用服务器上配置。例如,Glassfish
您可以在管理控制台中设置此属性,如下所示:
and then just call
然后就打电话
ds.getConnection()
from your client code.
ds.getConnection()
从您的客户端代码。
Edit:
编辑:
If you want to get access to the implementation class of javax.sql.DataSource
, you should use DataSource#unwrapmethod rather than simple cast.
如果您想访问 的实现类javax.sql.DataSource
,您应该使用DataSource#unwrap方法而不是简单的强制转换。
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.
确保您在类路径上有 jdbc 驱动程序 jar。
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 Featuresof Oracle Administration Guide.
编辑 2:
另外,Glassfish
请参阅Oracle 管理指南的配置特定 JDBC 连接池功能。
回答by biziclop
This form of getConnection()
is specific to OracleDataSource
and doesn't exist in the more generic DataSource
interface.
这种形式getConnection()
特定OracleDataSource
于更通用的DataSource
接口,并且不存在于更通用的接口中。
The solution is simple, replace this line:
解决方法很简单,替换这一行:
DataSource ds = (DataSource)context.lookup("jdbc/myPool");
with this:
有了这个:
OracleDataSource ds = (OracleDataSource)context.lookup("jdbc/myPool");
Of course this means that from now on your application will only work with Oracle databases. Depending on what you need this may or may not be a good idea.
当然,这意味着从现在开始您的应用程序将只适用于 Oracle 数据库。根据您的需要,这可能是也可能不是一个好主意。
回答by leonbloy
In addition to Alex's comment, I'd try
除了亚历克斯的评论,我会尝试
conn = ds.getConnection();
conn.setClientInfo("SetFloatAndDoubleUseBinary","true");