各位同行大家好
现有一张Excel表,想通过JXL包读取数据后,再将结果集写入数据库,但在提交事务报了这个错,请各位给些意见,谢谢
------------------------------------------------------
com.microsoft.sqlserver.jdbc.SQLServerException: SQL Server 返回的响应不完整。连接已关闭。
at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:1667)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.terminate(SQLServerConnection.java:1654)
at com.microsoft.sqlserver.jdbc.TDSReader.readPacket(IOBuffer.java:4844)
at com.microsoft.sqlserver.jdbc.TDSCommand.startResponse(IOBuffer.java:6150)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatementBatch(SQLServerPreparedStatement.java:1284)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtBatchExecCmd.doExecute(SQLServerPreparedStatement.java:1209)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:180)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:155)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeBatch(SQLServerPreparedStatement.java:1173)
at ExcelReadPre.writeToDb(ExcelReadPre.java:118)
at ExcelReadPre.testRun(ExcelReadPre.java:29)
at ExcelReadPre.main(ExcelReadPre.java:15)
______________________________________________________________________________________________________________
代码如下
import jxl.Cell;
import jxl.Sheet;
import java.io.File;
import java.io.InputStream;
import java.sql.*;
import java.util.Vector;
import java.io.FileInputStream;
import jxl.Workbook;
public class ExcelReadPre {
public static void main(String[] args) {
ExcelReadPre cr = new ExcelReadPre();
long start = System.currentTimeMillis();// 记录开始时间
cr.testRun();// 开始执行
long end = System.currentTimeMillis();
System.out.println("============耗時:====" + ((end - start) / 1000.0));
}
public void testRun() {
String path = "J:\\Loader\\excel";
try {
File fileDir = new File(path);
File files[] = fileDir.listFiles();
for (int i = 0; i < files.length; i++) {
System.out.println("P第" + (i + 1) + "个文件:====="
+ files[i].getAbsolutePath());
this.writeToDb(files[i].getAbsolutePath());
}
if (this.con != null && !con.isClosed()) {
this.con.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 执行指定的Sql
*/
private String url = "jdbc:sqlserver://localhost:1433;database=test";
private Connection con = null;
PreparedStatement pst = null;
public void getCOnn() {
// System.out.println("=========="+sql);
try {
if (con == null || con.isClosed()) {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(url, "sa", "fuckguoqinglin");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 导入一指定文件数据 批处理SQL
*
* @param filePath
*/
public void writeToDb(String filePath) {
String sql = "insert into [dbo].[5lpriceticket] values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
try {
this.getCOnn();
// 关闭事务自动提交
con.setAutoCommit(true);
pst = con.prepareStatement(sql);
Vector<Cell[]> v = readExcel(filePath);
int i = 0;