Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

mysql

jsp

jdbc

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();
    }
like image 966
Gayan Priyanakara Avatar asked Nov 11 '22 10:11

Gayan Priyanakara


1 Answers

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.

like image 60
Pransh Tiwari Avatar answered Nov 14 '22 23:11

Pransh Tiwari