我正在从数据库中检索数据并将其显示在JSP
的表格中,但我不知道如何在文本字段中显示它。
e.g。
JSP
我的代码:
public class S2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "shoppingCart";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "";
Statement st;
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url + dbName, userName, password);
System.out.println("Connected!");
String pid = request.getParameter("pid");
ArrayList al = null;
ArrayList pid_list = new ArrayList();
String query = "select * from user where uid='" + pid + "' ";
System.out.println("query " + query);
st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
while (rs.next()) {
al = new ArrayList();
out.println(rs.getString(1));
out.println(rs.getString(2));
out.println(rs.getString(3));
out.println(rs.getString(4));
out.println(rs.getString(5));
al.add(rs.getString(1));
al.add(rs.getString(2));
al.add(rs.getString(3));
al.add(rs.getString(4));
al.add(rs.getString(5));
System.out.println("al :: " + al);
pid_list.add(al);
}
request.setAttribute("piList", pid_list);
RequestDispatcher view = request.getRequestDispatcher("/searchview.jsp");
view.forward(request, response);
conn.close();
System.out.println("Disconnected!");
} catch (Exception e) {
e.printStackTrace();
}
答案 0 :(得分:1)
确保您已在项目中包含jdbc驱动程序并且" build"它。然后:
建立数据库连接并检索查询结果。
返回查询结果并保存在ResultSet的对象中。
遍历对象并显示查询结果。
下面的示例代码详细说明了这一点。
String label = request.getParameter("label");
//retrieving a variable from a previous page
Connection dbc = null; //Make connection to the database
Class.forName("com.mysql.jdbc.Driver");
dbc = DriverManager.getConnection("jdbc:mysql://localhost:3306/works", "root", "root");
if (dbc != null)
{
System.out.println("Connection successful");
}
ResultSet rs = listresult.dbresult.func(dbc, label);
//The above function is mentioned in the end.
//It is defined in another package- listresult
while (rs.next())
{
%>
<form name="demo form" method="post">
<table>
<tr>
<td>
Label Name:
</td>
<td>
<input type="text" name="label"
value="<%=rs.getString("lname")%>">
</td>
</tr>
</table>
</form>
<% } %>
public static ResultSet func(Connection dbc, String x)
{
ResultSet rs = null;
String sql;
PreparedStatement pst;
try
{
sql = "select lname from demo where label like '" + x + "'";
pst = dbc.prepareStatement(sql);
rs = pst.executeQuery();
}
catch (Exception e)
{
e.printStackTrace();
String sqlMessage = e.getMessage();
}
return rs;
}
我试图让这个例子尽可能详细。如果有任何问题请询问。
答案 1 :(得分:0)
<input type="text" value=<%=rs.getString("table_coloumn_name")%>></input>
答案 2 :(得分:0)
将检索到的值添加到请求或会话对象,并使用scrip let和表达式在JSP页面上检索它。或者您可以尝试使用JSTL / EL,如下所示
在处理程序或servlet中,将检索到的值从数据库添加到请求对象
request.setAttribute(theValueFromDB, "value");
然后将页面转发或重定向到相应的JSP。 在JSP页面中
<input type="text" value="${ requestScope.value}">
答案 3 :(得分:0)
您使用的是哪个数据库?例如,MySQL和MS Access都有不同的方法来连接数据库。
如果您可以建立连接,请执行以下操作
如果您有任何问题,我可以为您提供修复代码。
答案 4 :(得分:0)
while(!rs1.next()&amp;&amp;!rs2.next()) {
%>
<td><input type="checkbox" name="pteam" value=<%=rs1.getString("ename")%>>emp_name: </td>
<%x++;}}st.close();con.close();%>