gpt4 book ai didi

java - 如何使用 jdbc 连接从数据库中检索数据并将其显示在 jsp 文本字段中

转载 作者:可可西里 更新时间:2023-11-01 07:08:58 25 4
gpt4 key购买 nike

我正在从数据库中检索数据并将其显示在 JSP 的表格中,但我不知道如何在文本字段上显示它。

例如

  1. 当我搜索索引号时。
  2. 结果(姓名、地址、年龄)必须出现在我的 JSP
  3. 中的文本字段中

我的代码:

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();
}

最佳答案

确保您已在项目中包含 jdbc 驱动程序并“构建”它。然后:

  1. 建立数据库连接并检索查询结果。

  2. 返回查询结果并保存在ResultSet对象中。

  3. 遍历对象并显示查询结果。

下面的示例代码详细说明了这一点。

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;
}

我已尽力使这个示例尽可能详细。如果有任何疑问确实询问。

关于java - 如何使用 jdbc 连接从数据库中检索数据并将其显示在 jsp 文本字段中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22755795/

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