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

62,254

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

  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.

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.

Share:
62,254
Gayan Priyanakara
Author by

Gayan Priyanakara

Software Engineer @ Origins software &amp; web solutions · Colombo, Sri Lanka Application developer @ Sarasavi Publishers. Colombo, Sri Lanka

Updated on September 12, 2020

Comments

  • Gayan Priyanakara
    Gayan Priyanakara almost 4 years

    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.

    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

    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();
        }
    
  • Gayan Priyanakara
    Gayan Priyanakara about 10 years
    i have done it but i am confused a little if i send to you an you correct it for me
  • Gayan Priyanakara
    Gayan Priyanakara about 10 years
    am using Mysql and i did it still bit of confuse can you fix it for me if i send it to you
  • Raza Quaidian
    Raza Quaidian about 10 years
    Yes Gayan i'll try my best to do it for you, plz send me your model class(class connecting to db)