java Preparestatement 批量增加时,怎么定位错误?
java Preparestatement 批量增加时,怎么定位错误?每一千条执行一次executeBatch()
假设第1234条出错,回滚。怎么知道是哪条数据出了错?? java jdbc --------------------编程问答-------------------- log用来干嘛的 --------------------编程问答-------------------- 你异常里面可以把错误那条打印出来的 --------------------编程问答--------------------
求大神详细讲解。 --------------------编程问答--------------------
你们做项目不用logger么 --------------------编程问答--------------------
--------------------编程问答--------------------
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Logger;
public class Input implements InputI {
public static String DriverName = null;
public static String Url = null;
public static String Name = null;
public static String Pass = null;
public static Logger logger=Logger.getLogger(Input.class.getName()) ;
public void setDate(String table,Object[] ss,int num,String file,int rownum) {
// System.out.println(Input.class.getClass().getResource("/"));
Connection conn = null;
PreparedStatement state = null;
File directory = new File(Input.class.getClass().getResource("/")
.getPath()
+ "\\file.text");
int m = 1;
int tt=0;
int[] v=null;
try {
FileReader fi = new FileReader(directory);
InputStreamReader in = fi;
BufferedReader reader = new BufferedReader(in);
String s = null;
DriverName = reader.readLine();
Url = reader.readLine();
Name = reader.readLine();
Pass = reader.readLine();
conn = Input.getConn(DriverName, Url, Name, Pass);
conn.setAutoCommit(false);
String sql = null;
File f=new File(file);
Object[][] ob=ExcelOperate.getData(f, rownum);
System.out.println("共有"+ob.length+"组数据");
sql=Input.getSql(table, ss, num);
System.out.println(sql);
state = conn.prepareStatement(sql);
//每1000条处理一次
int n = ob.length / 1000 + 1;
while (m <= n) {
for (int i = 0; i < 1000 &&( m-1) * 1000 + i <ob.length; i++) {
for (int j = 0; j < ob[((m - 1) * 1000+ i)].length; j++) {
state.setObject(j + 1, ob[(m - 1) * 1000 + i][j]);
}
state.addBatch();
tt++;
}
v=state.executeBatch();
m++;
}
conn.commit();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println(tt);
System.out.println("第"+(m-1)*1000+"至"+m*1000+"条数据之间出现异常,回滚......");
try {
conn.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
}
}
//获取sql语句
public static String getSql(String table, Object[] s, int num) {
String sql = "insert into " + table;
if (s != null && s.length != 0) {
sql = sql + " (";
for (int i = 0; i < s.length; i++) {
sql = sql + s[i] + ",";
}
sql = sql.substring(0, sql.length() - 1) + ")";
}
sql = sql + " values (";
for (int i = 0; i < num; i++) {
sql = sql + "?,";
}
sql = sql.substring(0, sql.length() - 1) + ")";
return sql;
}
//获取连接
public static Connection getConn(String drivername, String url,
String user, String password) {
Connection con = null;
try {
Class.forName(drivername);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
new Input().setDate("fileload", new Object[]{"wdno","filenum","date"}, 3, "e:\\0910.xls", 1);
long endTime = System.currentTimeMillis();
System.out.println("程序运行时间: " + (endTime - startTime) + "ms");
}
}
log用来干嘛的
求大神详细讲解。
你们做项目不用logger么
我新手,不怎么会。 --------------------编程问答--------------------
log用来干嘛的
求大神详细讲解。
你们做项目不用logger么
我新手,不怎么会。
只晓得logger.info("");logger.debug("");logger.error("");之类的。
补充:Java , Java相关