I can connect fine to an Oracle 11.2 database using JDBC driver and the following Java code:
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
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
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?
UPDATE 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
The DataSource
is usually configured on the application server. For example, in Glassfish
you could set this property in the admin console like this:
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.
This form of getConnection()
is specific to OracleDataSource
and doesn't exist in the more generic DataSource
interface.
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.
In addition to Alex's comment, I'd try
conn = ds.getConnection();
conn.setClientInfo("SetFloatAndDoubleUseBinary","true");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.