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
Use the Connection Properties dialog box to control various settings for connections to external data sources, and to use, reuse, or switch connection files. This dialog box is often titled Query Properties when Power Query has been used to import the external data source.
In MS SQL Server, the Database Properties dialog has the "View Connection Properties" link over on the left. Clicking that brings the "Connection Properties" dialog with properties of the current connection, such as Authentication Method, Network Protocol, Computer Name, etc...
The getConnection() method of DriverManager class is used to establish connection with the database.
A database connection URL is a string that your DBMS JDBC driver uses to connect to a database. It can contain information such as where to search for the database, the name of the database to connect to, and configuration properties. The exact syntax of a database connection URL is specified by your DBMS.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With