0

我正在尝试更新中的行mysql table,我正在使用html form数据插入。在 html 表单属性中,我正在value使用数据库中的现有数据。

编辑.jsp

<form action="Update.jsp">
                <%  Class.forName("com.mysql.jdbc.Driver").newInstance();
                    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
                    Statement st = con.createStatement();
                    ResultSet rs = st.executeQuery("select u.login,i.name,i.surname,i.age,i.tel,i.email,a.street,a.town,a.zip,ul.name,t.name from users u join info i on i.user_id=u.user_id join user_level ul on ul.ulevel=u.ulevel join teams t on t.team_id=u.team_id join adress a on a.info_id=i.info_id where u.login='" + session.getAttribute("uzivatel") + "'");
                    while (rs.next()) {
                %>

                <div class="well well-large">
                    <font size="2"><b>Welcome, </b></font><font color="RED"><i><%= session.getAttribute("uzivatel")%></i></font><br>
                    <b>Name:</b> <input type="text" name="name" value="<%= rs.getString(2)%>"><input type="text" name="surname" value="<%= rs.getString(3)%>"> <br>
                    <b>Age:</b> <input type="text" name="age" value="<%= rs.getString(4)%>"><br>
                    <b>Telephone:</b> <input type="text" name="tel" value="0<%= rs.getString(5)%>"><br>
                    <b>E-mail:</b> <input type="text" name="email" value="<%= rs.getString(6)%>"><br>
                    <b>Adress:</b> <input type="text" name="street" value="<%= rs.getString(7)%>"><input type="text" name="town" value="<%= rs.getString(8)%>"><input type="text" name="zip" value="<%= rs.getString(9)%>"><br>
                    <b>User level:</b> <%= rs.getString(10)%><br>
                    <b>Team:</b> <%= rs.getString(11)%><br>
                    <input type="submit" name="Submit" value="Update" />
                </div>
            </form>

更新.jsp

<%
                String name = request.getParameter("name");
                String surname = request.getParameter("surname");
                String age = request.getParameter("age");
                String telephone = request.getParameter("tel");
                String email = request.getParameter("email");
                String street = request.getParameter("street");
                String town = request.getParameter("town");
                String zip = request.getParameter("zip");

                try {
                    Connection conn = null;
                    Class.forName("com.mysql.jdbc.Driver").newInstance();
                    conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
                    Statement st1 = null;
                    st1 = conn.createStatement();
                    System.out.println(session.getAttribute("uzivatel"));
                    st1.executeUpdate("UPDATE users JOIN info ON users.user_id = info.user_id"
                            + " JOIN adress ON info.info_id = adress.info_id"
                            + "SET info.name = '"+name+"',info.surname = '"+surname+"',"
                            + "info.age = '"+age+"',info.tel = '"+telephone+"',info.email = '"+email+"',"
                            + "adress.street = '"+street+"',adress.town = '"+town+"',adress.zip = '"+zip+"',"
                            + "WHERE users.login ='" + session.getAttribute("uzivatel") + "'  ");
                    response.sendRedirect("AdministrationControlPanel.jsp");
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
            %>

当我按下提交按钮时,它会将我重定向到Update.jsp并且没有任何改变。

4

2 回答 2

1

您的查询存在问题,这就是您没有得到任何结果的原因。当您将字符串分成不同的行时,请适当地留出空间。system.out.println() 将在控制台上显示消息而不是在页面上

st1.executeUpdate("UPDATE users JOIN info ON users.user_id = info.user_id"
                        + " JOIN adress ON info.info_id = adress.info_id "
                        + "SET info.name = '"+name+"',info.surname = '"+surname+"', "
                        + "info.age = '"+age+"', info.tel = '"+telephone+"', info.email = '"+email+"',"
                        + "adress.street = '"+street+"',adress.town = '"+town+"',adress.zip = '"+zip+"', "
                        + "WHERE users.login ='" + session.getAttribute("uzivatel") + "'  ");
于 2013-01-31T08:01:48.323 回答
0

最好使用 jQuery 而不是调用页面 update.jsp。当您使用 jQuery 时,您可以使用参数将数据发送到 update.jsp 页面并从该页面获取响应并将其打印到您的页面中,即使您可以从 jQuery 中获取所有表数据

这很简单希望这会对你有所帮助.. 示例示例有以下 url

演示
http://www.roseindia.net/tutorial/jquery/jqDatabaseConnect.html

http://api.jquery.com/jQuery.post/

于 2013-01-31T08:10:45.120 回答