我使用jtds 1.3.1连接到MSSQL Server 2008 SP3。我在3个单独的线程中创建3个PreparedStatements并向它们添加查询。当我执行PreparedStatements时,我得到了这个例外:
java.sql.BatchUpdateException: I/O Error: Connection reset by peer: socket write error
at net.sourceforge.jtds.jdbc.JtdsStatement.executeBatch(JtdsStatement.java:1091)
at de.mvn.gotdb_mvn.Main.main(Main.java:181)
请帮我解决问题。这是我的代码:
public static void main(String[] args) throws SQLException
{
final Importer importer = new Importer();
importer.openConnection();
final Connection connection = importer.getCon();
importer.createNewTables();
StatementPreparer test_falldata = new StatementPreparer(connection.prepareStatement(q1)) {
@Override
public void doRun()
{
try
{
PreparedStatement falldata_stmt = getStmt();
// |
String[] line = this.getLine();
falldata_stmt.setInt(1, Integer.parseInt(line[0].trim()));
//FallNr
falldata_stmt.setString(2, line[1]);
//DRG
falldata_stmt.setString(3, line[18]);
//MDC
falldata_stmt.setString(4, line[19]);
//PCCL
falldata_stmt.setString(5, line[20]);
falldata_stmt.addBatch();
} catch (Exception e)
{
e.printStackTrace();
}
}
};
StatementPreparer test_diagnosen = new StatementPreparer(connection.prepareStatement(q2)) {
@Override
public void doRun()
{
try
{
PreparedStatement diagnosen_stmt = getStmt();
String[] line = this.getLine();
String[] diagnose_only = line[13].split("~");
for (int i = 0; i < diagnose_only.length; i++)
{
int index_of_roof = diagnose_only[i].indexOf("^");
//FallNr
diagnosen_stmt.setString(1, line[1]);
//Code
diagnosen_stmt.setString(2, diagnose_only[i].substring(0, index_of_roof));
//Used int
diagnosen_stmt.setInt(3, Integer.parseInt(diagnose_only[i].substring(index_of_roof + 1, index_of_roof + 2)));
//HDX/NDX int
if (i == 1)
{
diagnosen_stmt.setInt(4, 1);
} else
{
diagnosen_stmt.setInt(4, 0);
}
diagnosen_stmt.addBatch();
}
} catch (Exception e)
{
e.printStackTrace();
}
}
};
StatementPreparer test_proceduren = new StatementPreparer(connection.prepareStatement(q3)) {
@Override
public void doRun()
{
try
{
PreparedStatement proceduren_stmt = getStmt();
String[] line = this.getLine();
String[] procedure_only = line[14].split("~");
for (String single_proce : procedure_only)
{
if (!single_proce.isEmpty())
{
int index_of_amp = single_proce.indexOf("&");
//fallnr
proceduren_stmt.setString(1, line[1]);
//code
proceduren_stmt.setString(2, single_proce.substring(0, index_of_amp));
//lokalisation
if (single_proce.lastIndexOf("&") == index_of_amp + 1)
{
proceduren_stmt.setString(4, "");
//Datum
proceduren_stmt.setDate(3, getProcedureDate(single_proce, proce_no_lok));
} else
{
String lokalisation = single_proce.substring(index_of_amp + 1, single_proce.lastIndexOf("&"));
proceduren_stmt.setString(4, lokalisation);
//Datum
proceduren_stmt.setDate(3, getProcedureDate(single_proce, proce_with_lok));
}
//used
proceduren_stmt.setInt(5, Integer.parseInt(single_proce.substring(single_proce.indexOf("^") + 1, single_proce.indexOf("^") + 2)));
proceduren_stmt.addBatch();
}
}
} catch (Exception e)
{
e.printStackTrace();
}
}
};
PreparerListener listener = new PreparerListener();
test_falldata.addListener(listener);
test_diagnosen.addListener(listener);
test_proceduren.addListener(listener);
try
{
for (String nextline = importer.getNextLine(); nextline != null; nextline = importer.getNextLine())
{
test_falldata.setLine(nextline);
test_diagnosen.setLine(nextline);
test_proceduren.setLine(nextline);
test_falldata.resumeThread();
test_diagnosen.resumeThread();
test_proceduren.resumeThread();
}
if(!connection.isClosed())
{
test_falldata.getStmt().executeBatch();
if(connection.isClosed())
{
System.out.print("CLOSED");
System.exit(-10);
}
test_diagnosen.getStmt().executeBatch();// Exception
test_proceduren.getStmt().executeBatch();
}else
{
System.out.println("Connection has Closed");
}
} catch (Exception e)
{
e.printStackTrace();
}
}
OpenConnection方法:
public void openConnection(String p_propertys)
{
try{
Class.forName("net.sourceforge.jtds.jdbc.Driver");
String url="jdbc:jtds:sqlserver://LB236/checkpointDB;loginTimeout=30;socketTimeout=1800";
String user ="jadmin";
String pw="jadmin123";
con = DriverManager.getConnection(url,user,pw);
}catch(Exception e){e.printStacktrace();}
}