我想通过一个查询将多个记录插入到mysql数据库中,出现错误; java.lang.ArrayIndexOutOfBoundsException:1
一直试图在Mysql数据库中插入多个记录。从动态创建的jsp输入字段中动态捕获值。
package ComDairy;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//@WebServlet("/pricing")
public class pricing extends HttpServlet {
private static final long serialVersionUID = 1L;
public pricing() {
super();
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
response.setContentType("text/html;charset=UTF-8");
try {
String projid = null;
String projname = null;
String projprice = null;
String[] itemid ={ request.getParameter("itemid")};
String[] Itemname ={ request.getParameter("itemname")};
String[] Itemprice ={ request.getParameter("itemprice")};
MyDB db = new MyDB();
Connection con = db.getCon();
//Statement stmt = con.createStatement();
PreparedStatement st = null;
//String sql = "insert into pricelist(Itemid,Itemname, Itemprice)" + " values('" + projid + "','" + projname + "','" + projprice + "')";
//String sql = "update pricelist set Itemid = ?,Itemname = ? ,Itemprice = ?";
String sql = "insert into pricelist(Itemid,Itemname,Itemprice)" + " values(?,?,?)";
con.setAutoCommit(false);
st = con.prepareStatement(sql);
for (int i = 0; i < 5; i++) {
projid = itemid[i] + " " + 1;
projname = Itemname[i] + " " + 1;
projprice = Itemprice[i] + " " + 1;
st.setString(1, projid);
st.setString(2, projname);
st.setString(3, projprice);
st.addBatch();
}
int counts[] = st.executeBatch();
con.commit();
} catch (SQLException ex) {
Logger.getLogger(pricing.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
processRequest(request, response);
} catch (SQLException ex) {
Logger.getLogger(pricing.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
processRequest(request, response);
} catch (SQLException ex) {
Logger.getLogger(pricing.class.getName()).log(Level.SEVERE, null, ex);
}
}
}package ComDairy;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//@WebServlet("/pricing")
public class pricing extends HttpServlet {
private static final long serialVersionUID = 1L;
public pricing() {
super();
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
response.setContentType("text/html;charset=UTF-8");
try {
String projid = null;
String projname = null;
String projprice = null;
String[] itemid ={ request.getParameter("itemid")};
String[] Itemname ={ request.getParameter("itemname")};
String[] Itemprice ={ request.getParameter("itemprice")};
MyDB db = new MyDB();
Connection con = db.getCon();
//Statement stmt = con.createStatement();
PreparedStatement st = null;
//String sql = "insert into pricelist(Itemid,Itemname, Itemprice)" + " values('" + projid + "','" + projname + "','" + projprice + "')";
//String sql = "update pricelist set Itemid = ?,Itemname = ? ,Itemprice = ?";
String sql = "insert into pricelist(Itemid,Itemname,Itemprice)" + " values(?,?,?)";
con.setAutoCommit(false);
st = con.prepareStatement(sql);
for (int i = 0; i < 5; i++) {
projid = itemid[i] + " " + 1;
projname = Itemname[i] + " " + 1;
projprice = Itemprice[i] + " " + 1;
st.setString(1, projid);
st.setString(2, projname);
st.setString(3, projprice);
st.addBatch();
}
int counts[] = st.executeBatch();
con.commit();
} catch (SQLException ex) {
Logger.getLogger(pricing.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
processRequest(request, response);
} catch (SQLException ex) {
Logger.getLogger(pricing.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
processRequest(request, response);
} catch (SQLException ex) {
Logger.getLogger(pricing.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
我希望通过单击按钮将记录保存到数据库中