我在使用netbeans中的jsp页面查看mysql数据库中的数据时遇到问题。 这是我在jsp页面中的代码:
<%@page import="javax.swing.JOptionPane"%>
<%@page import="java.sql.DatabaseMetaData"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8" import="java.util.ArrayList" import="gettingItem.Item"%>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h2>Home Page</h2>
<form name="myform" onsubmit="return OnSubmitForm();" method="get">
<select name="select" size="12" style = "width: 150px">
<%
String url = "jdbc:mysql://localhost:3306/itemsdb ";
String user = "admin";
String password = "admin";
String Line;
Connection Con = null;
Statement Stmt = null;
ResultSet RS = null;
try
{
Class.forName("com.mysql.jdbc.driver");
Con = DriverManager.getConnection(url, user, password);
Stmt = Con.createStatement();
Stmt.executeQuery("SELECT ItemName FROM items");
RS = Stmt.getResultSet();
while(RS.next()){%>
<Option value="<%=RS.getString("ItemName")%>">
<%=RS.getString("ItemName")%>
</Option>
</select>
<input type="submit" value="Remove" onclick="document.pressed = this.value">
<input type="submit" value="Display" onclick="document.pressed = this.value">
</form>
<form action="Add_Item.jsp" method ="get" >
<input type="submit" value="Add">
</form>
<%
}
RS.close();
Stmt.close();
Con.close();
}
catch (Exception cnfe){
System.err.println("Exception: "+cnfe);
}
%>
</body>
</html>
我想在jsp页面的下拉列表中查看数据,但它似乎无法正常工作,我不知道我的代码有什么问题?
答案 0 :(得分:0)
它是MVC应用程序吗?我的意思是,最好在dao中加载数据,通过控制器将它们返回给视图。所以你可以调试什么错误。
我这样做了:
DAO
public List<String> getCiid(String eingabe)
{
String tmp = new String("SELECT inst_ciid FROM T_INSTANCE WHERE inst_ciid like '%"+eingabe.toUpperCase()+"%'");
Query query = this.getSession().createSQLQuery(tmp);
List liste = query.list();
return liste;
}
控制器:
@RequestMapping(value = "/getCiid", method = RequestMethod.GET)
public void getCiid(HttpServletResponse response,@RequestParam Map<String, String> params)
{
String jsonResponse = null;
List<String> liste = instanzDao.getCiid(params.get("term"));
jsonResponse = new Gson().toJson(liste);
response.setContentType("application/json");
try
{
response.getOutputStream().print(jsonResponse);
}
catch (IOException e)
{
e.printStackTrace();
}
}
视图:
$( "#ciid" ).autocomplete({
source: "getCiid",
minLength: 3,
select: function( event, ui ) {
// whatever you want
}
});
<td>
<label for="ciid"> CIID: </label>
<form:input path="instanceCiid" id="ciid" />
</td>