用jsp更新星号的SQL数据库

问题描述 投票:-1回答:1

我使用jsp呈现从用户查询中检索到的一些数据。每次输入查询时,jdbc都会将一些上下文数据和查询本身保存在MySQL数据库中。我将为用户添加对每个结果进行评分的可能性,并创建了一个评分系统,该系统将以数字的形式表示评分,当用户单击带编号的星号时,该评分将插入数据库(例如** ** *** ****对应于2 5 4)。为此,我在jdbc中输入以下代码:

PreparedStatement preps = null;

            preps = dbMySqlConn.prepareStatement("" +
                    " UPDATE " + dbName + "." + tableNameOutput +
                    " SET" + " ratings = ?" +
                    " WHERE id = " + queryID , Statement.RETURN_GENERATED_KEYS);

            if (stars != null) {
                preps.setString(19, stars);
            }

            preps.execute();

并且在主要的jsp中,我通过以下方式可视化评级系统:

"    <form name=\"rating\"\">\n" +  

"    <input class=\"star star-5\" id=\"star" + sessionTime + i + "-5\" type=\"radio\" onClick=\"javascript:saveRating()\" name=\"star\"/>\n" +
"           <label class=\"star star-5\" for=\"star" + sessionTime + i + "-5\" title=\"Perfect match\"></label>\n" +
"    </input>" +

"    <input class=\"star star-4\" id=\"star" + sessionTime + i + "-4\" type=\"radio\" onClick=\"javascript:saveRating()\" name=\"star\"/>\n" +
"           <label class=\"star star-4\" for=\"star" + sessionTime + i + "-4\" title=\"Excellent match\"></label>\n" +
"    </input>" +

"    <input class=\"star star-3\" id=\"star" + sessionTime + i + "-3\" type=\"radio\" onClick=\"javascript:saveRating()\" name=\"star\"/>\n" +
"           <label class=\"star star-3\" for=\"star" + sessionTime + i + "-3\" title=\"Good match\"></label>\n" +
"    </input>" +

"    <input class=\"star star-2\" id=\"star" + sessionTime + i + "-2\" type=\"radio\" onClick=\"javascript:saveRating()\" name=\"star\"/>\n" +
"           <label class=\"star star-2\" for=\"star" + sessionTime + i + "-2\" title=\"Fair match\"></label>\n" +
"    </input>" +

"    <input class=\"star star-1\" id=\"star" + sessionTime + i + "-1\" type=\"radio\" onClick=\"javascript:saveRating()\" name=\"star\"/>\n" +
"           <label class=\"star star-1\" for=\"star" + sessionTime + i + "-1\" title=\"Wrong match\"></label>\n" +
"    </input>" +

"  </form>\n" +

调用javascript函数的地方是:

"   function saveRating(){\n" +

                    "if ($('.star.star-5')[0].checked){" +          
                        "alert(\"Rating 5 saved\");" +
                    "}" +

                    "if ($('.star.star-4')[0].checked){" +
                        "alert(\"Rating 4 saved\");" +
                    "}" +

                    "if ($('.star.star-3')[0].checked){" +
                        "alert(\"Rating 3 saved\");" +
                    "}" +

                    "if ($('.star.star-2')[0].checked){" +
                        "alert(\"Rating 2 saved\");" +
                    "}" +

                    "if ($('.star.star-1')[0].checked){" +
                        "alert(\"Rating 1 saved\");" +
                    "}" +

                    "else{" +
                    "}" +
                    "        }\n" +
                    "</script>" +

显示警报的效果很好,但是如果我将警报替换为要执行的操作,则jdbc插入:

"MySqlJDBC.rate(servletPath, \"1\");"
"MySqlJDBC.rate(servletPath, \"2\");"
"MySqlJDBC.rate(servletPath, \"3\");"
"MySqlJDBC.rate(servletPath, \"4\");"
"MySqlJDBC.rate(servletPath, \"5\");"

它只是不起作用,我问自己为什么警报确实起作用,为什么调用java方法不起作用。

javascript mysql ajax jsp servlets
1个回答
0
投票

用代码解决了jsp中的问题:

"if ($('.star.star-1')[0].checked){" +
                    "   $.ajax({" +
                    "       type: \"POST\"," +
                    "       url: \"" + servletPath + "/servletName\"," +                                      
                    "       data: { operation: \"operation1\", userRating:stars, resId: numberOfQuery }" +    
                    "   }).done(function() {" +
                    "       alert( \"Success\");" +                                         
                    "   });" +
                    "   }" +

并在调用SQL语句的servlet内创建一个doPost。

正确的流程是:jsp标记->调用ajax的jsp函数-> servlet doPost-> JDBC

© www.soinside.com 2019 - 2024. All rights reserved.