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.
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();
}
Make sure you have included the jdbc driver in your project and "build" it. Then:
Make the database connection and retrieve the query result.
Return the query result and save in an object of ResultSet.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With