[英]inserting java array values into mysql by using prepared statement
嗨,我的代碼中的朋友們,我通過使用 JSP 頁面中的 request.getParameterValues() 以數組的形式接收值,然后將它傳遞給 getter setter 從那里傳遞給 DAO,但我不知道如何插入每個數組的值元素通過使用准備好的語句進入數據庫。 我的疑問是如何為每次迭代插入備注、數量、開始時間、結束時間我的代碼是
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();
雖然我認為您應該更新您的應用程序設計,更具體地說是您從 servlet 獲取數據的方式。 應該有一個名為例如MyBean
的 bean 來保存您將存儲到數據庫中的每個實體的數據,而沒有單獨的屬性數組:
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
}
}
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.