gpt4 book ai didi

java - 半多线程 JDBC 连接

转载 作者:可可西里 更新时间:2023-11-01 08:05:45 25 4
gpt4 key购买 nike

我有以下 Callable 实例,在此处抛出 SQLException:

public long[] call() throws Exception {
long[] stats = new long[6];
try {
executer.acquire();
PreparedStatement statement =
connection
.prepareStatement("SELECT `War`.`EndTime` FROM `WarSim`.`War` WHERE `War`.`WarName` = ?");
statement.setString(1, warName);
ResultSet res = statement.executeQuery(); //<--------------SQLEXCEPTION HERE
if (res.first()) {
Timestamp ts = res.getTimestamp("EndTime");
if (ts != null)
stats[0] = 1;
statement =
connection
.prepareStatement("SELECT COUNT(`ID`) FROM `Missile` WHERE `WarName` = ?");
statement.setString(1, warName);
res = statement.executeQuery();
stats[1] = res.getInt(1);
statement =
connection
.prepareStatement("SELECT COUNT(`ID`) FROM `Missile` WHERE `WarName` = ? AND `Intercepted` = '1'");
statement.setString(1, warName);
res = statement.executeQuery();
stats[2] = res.getInt(1);
stats[3] = stats[1] - stats[2];
statement =
connection
.prepareStatement("SELECT COUNT(`ID`) FROM `EnemyLauncher` WHERE `WarName` = ? AND `Intercepted` = '1'");
statement.setString(1, warName);
res = statement.executeQuery();
stats[4] = res.getInt(1);
statement =
connection
.prepareStatement("SELECT SUM(`Damage`) FROM `Missile` WHERE `WarName` = ? AND `Intercepted` = '0'");
statement.setString(1, warName);
res = statement.executeQuery();
stats[5] = res.getInt(1);
}
} catch (SQLException e) {
System.out.println(warName + " is problematic");
while (e != null) {
System.out.println("\tmsg: " + e.getMessage()+
"\n\tstate: " + e.getSQLState());
e = e.getNextException();
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
executer.release();
}
return stats;
}

executer 是我使用的单一许可、公平信号量。

当我调试代码时,一切正常(没有异常),但是当我“正常”运行程序时,我得到“结果集开始”SQLException 以及 SQLState S1000。

为什么我使用信号量获取互斥量进行查询却出现异常?

请帮忙:)

编辑:这里是堆栈跟踪。

java.sql.SQLException: Before start of result set

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1075)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:929)
at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:841)
at com.mysql.jdbc.ResultSetImpl.getInt(ResultSetImpl.java:2672)
at db.jdbc.GetWarStatsTask.call(GetWarStatsTask.java:37)
at db.jdbc.GetWarStatsTask.call(GetWarStatsTask.java:1)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

处理数据库连接的类:

public class JDBCConnection implements DBConnection {

private Connection connection;
private String dbUrl;
private Semaphore executer;
private ExecutorService es;
private static JDBCConnection instance;

public static JDBCConnection getInstance() {
if (instance == null) {
instance = new JDBCConnection();
}
return instance;
}

private JDBCConnection() {
dbUrl = "jdbc:mysql://---------/WarSim";
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection =
DriverManager.getConnection(dbUrl, "------", "-------");
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
while (e != null) {
System.out.println(e.getMessage());
e = e.getNextException();
}
}
es = Executors.newCachedThreadPool();
executer = new Semaphore(1, true);
}

public Future<long[]> getWarStats(String warName) {
return es.submit(new GetWarStatsTask(executer, connection, warName));
}

public void closeDB() {
try {
if (connection != null) {
connection.close();
}
} catch (Exception e) {
System.out.println("Could not close the current connection.");
e.printStackTrace();
}
}

最佳答案

基本上,您是将光标定位在第一行之前,然后请求数据。您需要将光标移动到第一行。

所以首先调用result.next();

关于java - 半多线程 JDBC 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26388465/

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