我在使用带有Tomcat 8.5.9的Java 8编写Oracle 11.2数据库时遇到问题。实际上,以下代码适用于写入存储过程,但直接写入数据库时出错。
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup("jdbc/myPool");
conn = ds.getConnection();
// The following works fine:
cs = conn.prepareCall( "{call my_proc (?,?)}" );
cs.setString(1, var1);
cs.registerOutParameter(2, Types.VARCHAR);
cs.execute();
out_var2 = cs.getString(2);
// The following results in a ClassCastException error:
sql ="INSERT INTO MY_TABLE1 (my_value1, my_value2) VALUES (?,?)";
ps = (OraclePreparedStatement) conn.prepareStatement(sql);
// The following results in the same error, but is an example of using Oracle extensions for setXXX():
sql="INSERT INTO MY_TABLE2 (my_value3, my_value4) VALUES (?,?)";
ps = (OraclePreparedStatement) conn.prepareStatement(sql);
for (ii=0; ii<var100.length; ii++) {
ps.setBinaryFloat(3, my_value3[ii]);
ps.setBinaryDouble(4, my_value4[ii]);
ps.addBatch();
}
ps.executeBatch();
错误是:java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement cannot be cast to oracle.jdbc.OraclePreparedStatement
。
我最近从GlassFish切换到了Tomcat。之前为Glassfish工作的代码是:
OracleDataSource ods = ds.unwrap(OracleDataSource.class);
OracleConnection conn = (OracleConnection) ods.getConnection();
conn = ds.getConnection();
sql_a ="INSERT INTO MY_TABLE (my_value1, my_value2) VALUES (?,?)";
ps_a = (OraclePreparedStatement) conn.prepareStatement(sql_a);
但是它给出了Tomcat的错误java.lang.NullPointerException
。
我使用以下链接作为指南配置了我的tomcat文件:
https://tomcat.apache.org/tomcat-8.5-doc/jndi-datasource-examples-howto.html
特别是关于Oracle 8i, 9i, & 10g
的部分(上下文配置和web.xml)。
任何想法,当我直接写入数据库时,如何消除Tomcat错误,同时还允许上述代码在写入存储过程时继续工作?
答案 0 :(得分:2)
这就是我在Tomcat中的工作方式:
(a)在context.xml中定义资源并将其放在webapps // META-INF / context.xml
下<Context>
<Resource name="jdbc/orcldriver_dbcs" auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
username="hr"
password="hr"
url="jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(HOST=myhost)(PORT=1521)(PROTOCOL=tcp))(CONNECT_DATA=(SERVICE_NAME=myorcldbservicename)))"
/>
</Context>
(b)在servlet中,参考如下所示的资源。
ctx = new InitialContext();
Context envContext = (Context) ctx.lookup("java:/comp/env");
// Look up a data source
javax.sql.DataSource ds
= (javax.sql.DataSource) envContext.lookup ("jdbc/orcldriver_dbcs");
// With AutoCloseable, the connection is closed automatically.
try (OracleConnection connection = (ds.getConnection()).unwrap(oracle.jdbc.OracleConnection.class))
{
....
doSQLWork();
....
}
答案 1 :(得分:0)
这可能不是最好的答案,但通过反复试验,我发现我只需要在下面添加一行代码来解开与OracleConnection的连接,一切正常。
...
Connection tconn=null;
OracleConnection conn=null;
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup("jdbc/myPool");
tconn = ds.getConnection();
// the following line is needed to unwrap to OracleConnection
conn= tconn.unwrap(OracleConnection.class);
tconn.close();
...
我确信有一种替代(可能更好)的方法为OracleConnection配置Tomcat,但我不知道该怎么做。