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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22755795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:43:20  来源:igfitidea点击:

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

javamysqljspjdbc

提问by Gayan Priyanakara

I am retrieving data from database and displaying it in table in a JSPbut I do not have any idea about how to display it on text fields.

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

e.g.

例如

  1. when I search a index number.
  2. the the result (name , address, age) must come to the textfeilds which are in my JSP
  1. 当我搜索索引号时。
  2. 结果(姓名、地址、年龄)必须出现在我的文本字段中 JSP

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

采纳答案by Pransh Tiwari

Make sure you have included the jdbc driver in your project and "build" it. Then:

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

  1. Make the database connection and retrieve the query result.

  2. Return the query result and save in an object of ResultSet.

  3. Traverse through the object and display the query results.

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

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

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

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.

我试图使这个例子尽可能详细。如果有任何疑问,请询问。

回答by Don Chakkappan

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

回答by bachman

Add the retrieved value to a request or session object and retrieve it on the JSP page using scrip lets and expressions. or you can try using JSTL/EL too like below

将检索到的值添加到请求或会话对象,并使用脚本让和表达式在 JSP 页面上检索它。或者您也可以尝试使用 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. In the JSP page

然后将页面转发或重定向到相应的 JSP。在 JSP 页面中

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

回答by Raza Quaidian

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
  • now retrieve the rows and display each column in fields you want.
  • 创建一个查询数据库的类对象
  • 调用将查询数据库的函数
  • 返回查询结果并保存在一个mySqlQuery对象中
  • 现在检索行并在您想要的字段中显示每一列。

If you have any problems, I can provide you code to fix it.

如果您有任何问题,我可以为您提供代码来解决它。

回答by user7982920

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