gpt4 book ai didi

html - jsp页面没有从mysql获取数据

转载 作者:行者123 更新时间:2023-11-29 23:20:05 26 4
gpt4 key购买 nike

我在使用 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页面的下拉列表中查看数据,但它似乎不起作用,我不知道我的代码有什么问题?

最佳答案

它是 MVC 应用程序吗?我的意思是,最好将数据加载到 dao 中,然后通过 Controller 将它们返回到 View 。这样你就可以调试出什么问题了。
我是这样做的: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;
}

Controller :

@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>

关于html - jsp页面没有从mysql获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27399183/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com