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

问题描述 投票:0回答:2

嗨朋友在我的代码中我通过使用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();
mysql sql jsp java-ee servlets
2个回答
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
  }
}

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
© www.soinside.com 2019 - 2024. All rights reserved.