简体   繁体   English

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

[英]how to Retrieve data from database and display it in a jsp text fields using jdbc connection

I am retrieving data from database and displaying it in table in a JSP but I do not have any idea about how to display it on text fields. 我正在从数据库中检索数据并在JSP中的表中显示它,但我不知道如何在文本字段中显示它。

eg 例如

  1. when I search a index number. 当我搜索索引号时。
  2. the the result (name , address, age) must come to the textfeilds which are in my JSP 结果(名称,地址,年龄)必须来自我的JSP的textfeild

My code: 我的代码:

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

Make sure you have included the jdbc driver in your project and "build" it. 确保已在项目中包含jdbc驱动程序并“构建”它。 Then: 然后:

  1. Make the database connection and retrieve the query result. 建立数据库连接并检索查询结果。

  2. Return the query result and save in an object of ResultSet. 返回查询结果并保存在ResultSet的对象中。

  3. Traverse through the object and display the query results. 遍历对象并显示查询结果。

The example code below demonstrates this in detail. 下面的示例代码详细说明了这一点。

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

I have tried to make this example as detailed as possible. 我试图尽可能详细地说明这个例子。 If any queries do ask. 如果有任何疑问请询问。

<input type="text" value=<%=rs.getString("table_coloumn_name")%>></input>

Add the retrieved value to a request or session object and retrieve it on the JSP page using scrip lets and expressions. 将检索到的值添加到请求或会话对象,并使用scrip let和表达式在JSP页面上检索它。 or you can try using JSTL/EL too like below 或者您可以尝试使用JSTL / EL,如下所示

In the handler or servlet add the retrieved value from database to request object 在处理程序或servlet中,将检索到的值从数据库添加到请求对象

request.setAttribute(theValueFromDB, "value");

then forward or redirect the page to the corresponding JSP. 然后将页面转发或重定向到相应的JSP。 In the JSP page 在JSP页面中

<input type="text" value="${ requestScope.value}">

Which database you are using? 您使用的是哪个数据库? For example, MySQL and MS Access each has a different method to connect to the database. 例如,MySQL和MS Access每个都有不同的方法来连接数据库。

If you can make a connection, then do the following 如果可以建立连接,请执行以下操作

  • make an object of class that is querying to database 创建一个查询数据库的类对象
  • call the function that will query database 调用将查询数据库的函数
  • return the query result and save in an object of mySqlQuery 返回查询结果并保存在mySqlQuery的对象中
  • now retrieve the rows and display each column in fields you want. 现在检索行并显示所需字段中的每一列。

If you have any problems, I can provide you code to fix it. 如果您有任何问题,我可以提供代码来修复它。

while(!rs1.next() && !rs2.next()) { while(!rs1.next()&&!rs2.next()){

            %>
            <td><input type="checkbox" name="pteam" value=<%=rs1.getString("ename")%>>emp_name:  </td>
            <%x++;}}st.close();con.close();%>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM