gpt4 book ai didi

oracle11g - 将 java.sql.Connection 转换为 oracle.jdbc.OracleConnection 会导致编译错误

转载 作者:行者123 更新时间:2023-12-02 03:31:32 28 4
gpt4 key购买 nike

我想投java.sql.Connectionoracle.jdbc.OracleConnection为了在 ARRAY 上绑定(bind)数据我的查询。

当我在 scala 2.10、bonecp 0.8.0 和 slick 2.0.0 上尝试以下操作时:

import com.jolbox.bonecp.ConnectionHandle
import oracle.jdbc.OracleConnection

def failsWithCompilationError() = {
Database.forDataSource(ds).withDynTransaction {
val connection = dynamicSession.conn.asInstanceOf[ConnectionHandle].getInternalConnection
println(connection.unwrap(classOf[OracleConnection]))
// When uncommenting following two lines a compilation error "error while loading AQMessage, class file '.../ojdbc6.jar(oracle/jdbc/aq/AQMessage.class)' is broken" will occur
// val oracleConnection: OracleConnection = connection.unwrap(classOf[OracleConnection])
// println(oracleConnection)
}
}

并取消注释分配给 val 的两行类型 OracleConnectionprintln编译失败
[error] error while loading AQMessage, class file '.../ojdbc6.jar(oracle/jdbc/aq/AQMessage.class)' is broken会发生。

我已经验证了 ojdbc6.jar 不应通过从 Oracle 下载较新版本而损坏。

最佳答案

似乎问题出在Scala编译器上。

一旦我嵌入了依赖于 oracle.jdbc.OracleConnection 的功能到一个普通的旧 Java 类中,将其构建到一个单独的 .jar 中并与我的 Scala 代码链接,事情开始滚动。

这是我如何让它工作的:

OracleArray.java

package my.application.oracle.collections;

import oracle.jdbc.OracleConnection;
import oracle.jdbc.OraclePreparedStatement;
import oracle.sql.ARRAY;
import scala.Long;
import scala.Tuple2;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/*
Wraps usage of Oracle ARRAYs since casting java.sql.Connection to oracle.jdbc.Connection does not compile on Scala.
*/
public class OracleArray {
public static List<Tuple2<Long, Long>> fetchAssetsByIds(List ids, Connection connection) throws SQLException {
OracleConnection oracleConnection = (OracleConnection) connection;
ARRAY oracleArray = oracleConnection.createARRAY("MY_ARRAY_SQL_TYPE", ids.toArray());
String sql = "SELECT a.id, a.value" +
"FROM ASSET a " +
"WHERE a.id IN (SELECT COLUMN_VALUE FROM TABLE(?))";
PreparedStatement statement = oracleConnection.prepareStatement(sql);
try {
OraclePreparedStatement oraclePreparedStatement = (OraclePreparedStatement) statement;
oraclePreparedStatement.setArray(1, oracleArray);
ResultSet resultSet = oraclePreparedStatement.executeQuery();
try {
ArrayList<Tuple2<Long, Long>> resultTuples = new ArrayList<>();
while (resultSet.next()) {
long id = resultSet.getLong(1);
long value = resultSet.getLong(2);
resultTuples.add(new Tuple2(id, value));
}
return resultTuples;
} finally {
resultSet.close();
}
} finally {
statement.close();
}
}
}

数据用户.scala
package my.application

import my.application.oracle.collections.OracleArray

import scala.slick.driver.JdbcDriver.backend.Database
import Database.dynamicSession
import com.jolbox.bonecp.ConnectionHandle

import java.sql.Connection
import collection.JavaConversions._

/*
Uses BoneCP and Slick to connect to database and relays java.sql.Connection to
OracleArray in order to run operations that use Oracle ARRAYs
*/
object DataUser {
def doSomethingWithAssets(ids: Seq[Long]): Unit = {
Database.forDataSource(ds).withDynTransaction {
val connection: Connection = dynamicSession.conn.asInstanceOf[ConnectionHandle].getInternalConnection
val assets: Seq[(Long, Long)] = OracleArray.fetchAssetsByIds(ids, connection)
println(assets)
}
}
}

关于oracle11g - 将 java.sql.Connection 转换为 oracle.jdbc.OracleConnection 会导致编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26403098/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com