使用预准备语句将java数组值插入mysql

时间:2014-05-13 11:13:28

标签: mysql sql jsp java-ee servlets

嗨朋友在我的代码中我通过使用JSP页面中的request.getParameterValues()以数组的形式接收值,并且我将它传递给getter setter从那里传递到DAO但我不知道如何插入值通过使用预准备语句将每个arrary元素放入数据库中。我的疑问是如何为我的代码

的每次迭代插入备注,数量,startTime,endTime
  Servlet Code : 

  String[] remarks = request.getParameterValues("txtRemarks");
  String[] quantity = request.getParameterValues("txtQty");
  String[] startHrs = request.getParameterValues("txtStartTime");
  String[] endHrs = request.getParameterValues("txtEndtime");

  getter setter :

  public String[] getremarks() {  
  return getremarks;  
  }  
  public void setremarks(String[] newremarks) {  
  remarks = newremarks;  
  } 

 UserDAO :


         String query = "insert into table(remarks,quantity,startTime,endTime) values (?,?,?,?) "
         currentCon = ConnectionManager.getConnection();
         ps = currentCon.prepareStatement(query);
         rs = ps.executeQuery();

2 个答案:

答案 0 :(得分:0)

虽然我认为你应该更新你的应用程序设计,更具体地说是你从servlet获取数据的方式。 应该有一个名称为bean的bean MyBean用于保存您将存储到不具有独立属性数组的数据库的每个实体的数据:

class MyBean {
  String remark;
  String quantity;
  String startTime; //You said it is a String but maybe should be a timestamp
  String endTime;

  //Getters & Setters
}

现在回到你的代码,你应该注意到这是一个疯狂的猜测,因为我可以真正弄清楚你希望数据如何在你的表下:  *您是否希望在一列中存储备注数组?  *或那些备注代表每个单独的记录?

假设您要采用第二种方法,您的数据访问层服务可以如下所示:

public void insertMyEntity(String remark,
  String quantity,
  String startTime,
  String endTime) throws SQLException {

  Connection con = ConnectionManager.getConnection();
  PreparedStatement insertStatement = null;
  String insertString =
    "insert into YOUR_TABLE(remarks,quantity,startTime,endTime) values (?,?,?,?)";

  try {
    insertStatement = con.prepareStatement(insertString);
    insertStatement.setString(1, remark);
    insertStatement.setString(1, quantity);
    insertStatement.setString(1, startTime);// use setTimestamp() if it is a timestamp
    insertStatement.setString(1, endTime);
    insertStatement.executeUpdate();
  } catch (SQLException e ) {
      System.out.println(e.getMessage());
  } finally {
    if (insertStatement != null) {
      insertStatement.close();
    }
    if (con != null) {         
      con.close();
    }
  }
}

然后,当您从控制器(Servlet)中的客户端请求中检索数据时,可以使用循环迭代它们并插入数据库:

public class MySservlet extends HttpServlet
{
  protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException
  {
    ...
    String[] remarks = request.getParameterValues("txtRemarks");
    String[] quantity = request.getParameterValues("txtQty");
    String[] startHrs = request.getParameterValues("txtStartTime");
    String[] endHrs = request.getParameterValues("txtEndtime");

    for (int i = 0; i < remarks.length; i++)
    {
      insertMyEntity(remarks[i], quantity[i], startHrs[i], endHrs[i]);
    }
    response.sendRedirect("resourceuri");//replace the resource with servlet, jsp or html URL
  }
}

答案 1 :(得分:0)

tmarwen代码可以正常工作,您只需替换下面的try

try {
    insertStatement = con.prepareStatement(insertString);
    insertStatement.setString(1, remark);
    insertStatement.setString(1, quantity);
    insertStatement.setString(1, startTime);// use setTimestamp() if it is a timestamp
    insertStatement.setString(1, endTime);
    insertStatement.executeUpdate();
  } catch (SQLException e ) {
      System.out.println(e.getMessage());
  } finally {
    if (insertStatement != null) {
      insertStatement.close();
    } if (con != null) {         
      con.close();
    }
  }
response.sendRedirect("servlet/jsp");//Where ever you require to redirect