我想在用户从下拉菜单中选择时运行sql语句.
我的HTML:
我的JSP代码:
<% Dbase db = new Dbase(); Connection conn = db.connect("jdbc:mysql://localhost:3306/fyp", "root", ""); ResultSet rs; Statement stmt = conn.createStatement(); String keyword = request.getParameter("item"); //out.println(keyword); if(keyword == "buy"){ out.println("buy statement"); String query = "select numberofoccurence from stock WHERE name='buy';"; rs = stmt.executeQuery(query) ; while(rs.next()){ System.out.println(rs.getString(1)); } }else if(keyword == "sell"){ String query = "select numberofoccurence from stock WHERE name='sell';"; rs = stmt.executeQuery(query) ; while(rs.next()){ System.out.println(rs.getString(1)); } } %>
它完全没有错误.它只是无法进入if else语句.可能是什么原因 ?我是JSP的新手.
这已经回答了一百万次!总是在字符串中使用equals,所以替换if(keyword == "buy")...
为if("buy".equals(keyword))
.