我的代码实现批量插入,虽然可以插入,我觉得写的不怎么好,有没有高手帮忙改下,或者有更好的方法,本人新手只想到这个方法,而且返回的count应该是有问题的 public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name=request.getParameter("name");
//String[] vallists=request.getParameterValues("vallist");
name="testnettype";
String[] vallists= {"1,2","2,3","3,4"};
String pcount=vallists[1];
String[] szcount=pcount.split(",");
int counts=szcount.length;
Dao dao=new Dao();
int count=0;
PrintWriter out = null;
try {
response.setContentType("text/plain; charset=UTF-8");
out=response.getWriter();
count = dao.Batch(vallists, name,counts);
out.println(count);
} catch (Exception e) {
StringWriter sw=new StringWriter();
e.printStackTrace(new PrintWriter(sw));
System.out.println(sw.toString());
out.println(0);
}
out.flush();
out.close();
}public int Batch(String[] list,String tablename,int counts){
Connection conn = null;
PreparedStatement pst = null;
int count=0;
String values="";
try {
conn = ConnectionSource.getConnection();
for (int i = 0; i < counts; i++) {
values+="?,";
}
String value=values.substring(0, values.length()-1);
String sql="insert into "+tablename +" values("+value+")";
pst = conn.prepareStatement(sql);
conn.setAutoCommit(false);
for (int i = 0; i < list.length; i++) {
String pcount=list[i];
String[] szcount=pcount.split(",");
for (int j = 0; j <szcount.length; j++) {
pst.setObject(j+1, szcount[j]);
}
pst.addBatch();
count++;//用于记录插入多少,但是如果回滚了这个还是在的!!!!
}
pst.executeBatch();
conn.commit();
} catch (SQLException e) {
if(conn != null){
try {
conn.rollback();
conn.commit();
} catch (SQLException e1) {
loggerError.error(e1);
e1.printStackTrace();
}
}
loggerError.error(e);
e.printStackTrace();
} finally {
ConnectionSource.releaseSource(null, pst, conn);
}
return count;
}
throws ServletException, IOException {
String name=request.getParameter("name");
//String[] vallists=request.getParameterValues("vallist");
name="testnettype";
String[] vallists= {"1,2","2,3","3,4"};
String pcount=vallists[1];
String[] szcount=pcount.split(",");
int counts=szcount.length;
Dao dao=new Dao();
int count=0;
PrintWriter out = null;
try {
response.setContentType("text/plain; charset=UTF-8");
out=response.getWriter();
count = dao.Batch(vallists, name,counts);
out.println(count);
} catch (Exception e) {
StringWriter sw=new StringWriter();
e.printStackTrace(new PrintWriter(sw));
System.out.println(sw.toString());
out.println(0);
}
out.flush();
out.close();
}public int Batch(String[] list,String tablename,int counts){
Connection conn = null;
PreparedStatement pst = null;
int count=0;
String values="";
try {
conn = ConnectionSource.getConnection();
for (int i = 0; i < counts; i++) {
values+="?,";
}
String value=values.substring(0, values.length()-1);
String sql="insert into "+tablename +" values("+value+")";
pst = conn.prepareStatement(sql);
conn.setAutoCommit(false);
for (int i = 0; i < list.length; i++) {
String pcount=list[i];
String[] szcount=pcount.split(",");
for (int j = 0; j <szcount.length; j++) {
pst.setObject(j+1, szcount[j]);
}
pst.addBatch();
count++;//用于记录插入多少,但是如果回滚了这个还是在的!!!!
}
pst.executeBatch();
conn.commit();
} catch (SQLException e) {
if(conn != null){
try {
conn.rollback();
conn.commit();
} catch (SQLException e1) {
loggerError.error(e1);
e1.printStackTrace();
}
}
loggerError.error(e);
e.printStackTrace();
} finally {
ConnectionSource.releaseSource(null, pst, conn);
}
return count;
}
解决方案 »
- java 数据源的问题
- 如何根据存储过程的名字,用java程序来判断该存储过程是否返回结果集*((数据库为sqlserver 2008
- 求大虾介绍一下东软东软基础软件事业部(大连)! 谢谢了
- htmi:errors无法显示信息
- 在Struts的ACTION组件中究竟能不能声明实例变量???
- tomcat5.0环境变量配置
- Tomcat安装问题
- 请问为什么会报这种错?Illegal character at end of document, <.
- to:buick555 && wjmmml 请看我这个问题如何解决
- 新手初学,关于绘图代码有一处未明,请教
- 请教 关于Spring security 获取在线人数的问题
- 怎样用java获取URL返回状态码
conn.rollback();
conn.commit();
} catch (SQLException e1) {
loggerError.error(e1);
e1.printStackTrace();
}
==> try {
conn.rollback();
} catch (SQLException e1) {
loggerError.error(e1);
e1.printStackTrace();
}