java Preparestatement 批量增加时,怎么定位错误?

请教我CC 2013-09-13 09:08:45
java Preparestatement 批量增加时,怎么定位错误?

每一千条执行一次executeBatch()

假设第1234条出错,回滚。怎么知道是哪条数据出了错??
...全文
186 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
请教我CC 2013-09-13
  • 打赏
  • 举报
回复
引用 6 楼 wangchi110 的回复:
[quote=引用 4 楼 breaking236 的回复:] [quote=引用 3 楼 wangchi110 的回复:] [quote=引用 1 楼 breaking236 的回复:] log用来干嘛的
求大神详细讲解。[/quote] 你们做项目不用logger么[/quote] 我新手,不怎么会。[/quote] 只晓得logger.info("");logger.debug("");logger.error("");之类的。
请教我CC 2013-09-13
  • 打赏
  • 举报
回复
引用 4 楼 breaking236 的回复:
[quote=引用 3 楼 wangchi110 的回复:] [quote=引用 1 楼 breaking236 的回复:] log用来干嘛的
求大神详细讲解。[/quote] 你们做项目不用logger么[/quote] 我新手,不怎么会。
请教我CC 2013-09-13
  • 打赏
  • 举报
回复

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");
	}
}

breaking236 2013-09-13
  • 打赏
  • 举报
回复
引用 3 楼 wangchi110 的回复:
[quote=引用 1 楼 breaking236 的回复:] log用来干嘛的
求大神详细讲解。[/quote] 你们做项目不用logger么
请教我CC 2013-09-13
  • 打赏
  • 举报
回复
引用 1 楼 breaking236 的回复:
log用来干嘛的
求大神详细讲解。
tony4geek 2013-09-13
  • 打赏
  • 举报
回复
你异常里面可以把错误那条打印出来的
breaking236 2013-09-13
  • 打赏
  • 举报
回复
log用来干嘛的

51,409

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧