我有这段代码:
public void saveGPSReading(GPSReader gpsRd){
String sql="INSERT INTO EventData(" +
"accountID," +
"deviceID," +
"timestamp,"+
"statusCode," +
"latitude," +
"longitude, " +
"gpsAge," +
"speedKPH, " +
"heading," +
"altitude," +
"address," +
"creationTime)" +
"values(" +
"'demo','teltonika001',?,61714,?,?,0,?,?,?,'--not implemented--',?)";
try{
if(!con.isClosed()){
stmt = con.prepareStatement(sql);
for(DeviceEvent de : gpsRd.getEvents()){
GPSElement gps=de.getGpsElement();
IOElementReader ioe= de.getIoElement();
stmt.setLong(1, gps.getTimestamp().getTime());
stmt.setDouble(2, gps.getLatitude());
stmt.setDouble(3, gps.getLongitude());
stmt.setLong(4, gps.getSpeed());
stmt.setLong(5, gps.getAngle());
stmt.setLong(6, gps.getAltitude());
stmt.setLong(7, (new Date()).getTime());
stmt.addBatch();
//String s = stmt.toString();
}
stmt.executeBatch();
}
}
catch(Exception err){
err.printStackTrace();
}
}
在这里调用:
public void readPacketData(){
if(info.isValid()==true){
for( int i =0; i< info.getRecords(); i++){
GPSElement gps= readGPSElement();
IOElementReader io= readIOElement();
DeviceEvent dEvt = new DeviceEvent(gps,io);
gpsReader.addEvent(dEvt);
}
}
dbMan.saveGPSReading(gpsReader);
这是我调用它的唯一地方,并在行
处获得该异常stmt.executeBatch();
java.sql.BatchUpdateException:语句后不允许任何操作 关闭。 在 com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:2024) 在 com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1449) 在DBManager.saveGPSReading(DBManager.java:64)
这里有什么帮助吗?
我在这里没有关闭任何东西。在我的代码中的任何一点都没有连接
:: EDIT ::
所以我按照你的提示,将代码更改为:
public void saveGPSReading(GPSReader gpsRd){
String sql="INSERT INTO EventData(" +
"accountID," +
"deviceID," +
"timestamp,"+
"statusCode," +
"latitude," +
"longitude, " +
"gpsAge," +
"speedKPH, " +
"heading," +
"altitude," +
"address," +
"creationTime)" +
"values(" +
"'demo','teltonika001',?,61714,?,?,0,?,?,?,'--not implemented--',?)";
try{
Connection con= createConnection();
con.setAutoCommit(false);
stmt = con.prepareStatement(sql);
for(DeviceEvent de : gpsRd.getEvents()){
GPSElement gps=de.getGpsElement();
IOElementReader ioe= de.getIoElement();
stmt.setLong(1, gps.getTimestamp().getTime());
stmt.setDouble(2, gps.getLatitude());
stmt.setDouble(3, gps.getLongitude());
stmt.setLong(4, gps.getSpeed());
stmt.setLong(5, gps.getAngle());
stmt.setLong(6, gps.getAltitude());
stmt.setLong(7, (new Date()).getTime());
stmt.addBatch();
}
int[] updateCounts = stmt.executeBatch();
checkUpdateCounts(updateCounts);
con.commit();
}
catch (BatchUpdateException e) {
int[] updateCounts = e.getUpdateCounts();
checkUpdateCounts(updateCounts);
try {
con.rollback();
}
catch (Exception e2) {
e.printStackTrace();
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
stmt.close();
con.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
使用新方法创建连接:
public Connection createConnection(){
Connection con= null;
if(con ==null){
try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
String c=Constants.DB.DB_URL+"?user="+Constants.DB.DB_USER+"&"+"password="+Constants.DB.DB_PASSWORD;
con=DriverManager.getConnection(c);
}
catch (SQLException e) {
e.printStackTrace();
}
}
return con;
}
同样的问题仍然存在
答案 0 :(得分:0)
anfy2002us已经在他的评论中发布了您的数据库连接可能尚未打开(因为isClosed仅在打开然后关闭时才为真)。
如果这不是问题(例如,如果您确实在输入readPacketData()
之前检查了连接是否已打开,请按照此示例执行此操作:
http://www.java2s.com/Code/Java/Database-SQL-JDBC/DemoPreparedStatementAddBatchMySQL.htm
我们应该在失败时显式处理批处理失败和回滚重试。