Monday, May 11, 2015

How to transfer ResultSet object over network in java.

As we know the ResultSet object can not be transfer over the network. So for transferring the ResultSet object we need to set this object in Custom object and send this custom object over the network.

Step 1- Create UserTest class

Step 2- Retrieve the ResultSet object and set it into custom object (UserTest)



1
2
3
4
5
6
7
8
9
PreparedStatement ps1 = con.prepareStatement("select * from usertest");
ResultSet rs = ps1.executeQuery();
List<UserTest> userList = new ArrayList<UserTest>();
while (rs.next()) {
    UserTest userTest = new UserTest();
    userTest.setUserid(rs.getString("userid"));
    userTest.setPhotopath(rs.getString("photpath"));
    userList.add(userTest);
}

Step 3- Add this custom object into List

1
userList.add(userTest);

Step 4- Add this List into Session

1
session.setAttribute("userTestList", userList);

Step 5- Retrieve this List object at different page extract the data and display it.

1
2
3
List<UserTest> userList = (List<UserTest>) session.getAttribute("userTestList");
<td><%=userTest.getUserid()%></td>
<td><%=userTest.getPhotopath()%></td>

Example-

UserTest.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
package com. atozjavatutorials;
public class UserTest {
    private String userid;
    private String photopath;
    public String getUserid() {
        return userid;
    }
    public void setUserid(String userid) {
        this.userid = userid;
    }
    public String getPhotopath() {
        return photopath;
    }
    public void setPhotopath(String photopath) {
        this.photopath = photopath;
    }
}

ResultSetTransfer.jsp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="java.util.List"%>
<%@page import=" com. atozjavatutorials.UserTest"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8", "root", "root");
    PreparedStatement ps1 = con.prepareStatement("select * from usertest");
    ResultSet rs = ps1.executeQuery();
    List<UserTest> userList = new ArrayList<UserTest>();
    while (rs.next()) {
        UserTest userTest = new UserTest();
        userTest.setUserid(rs.getString("userid"));
        userTest.setPhotopath(rs.getString("photpath"));
        userList.add(userTest);
    }
    session.setAttribute("userTestList", userList);
%>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>ResultSet Object Transfer </title>
    </head>
    <body>
        <h1>ResultSet Object Transfer </h1>
        <h3><a href="GetResultSetObject.jsp">Click here to transfer the ResultSet Object from this page to next page >></a></h3>
    </body>
</html>

GetResultSetObject.jsp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<%@page import=" com. atozjavatutorials.UserTest"%>
<%@page import="java.util.List"%>
<% List<UserTest> userList = (List<UserTest>) session.getAttribute("userTestList"); %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>ResultSet Object Transfer </title>
    </head>
    <body>
        <h1>Extract ResultSet Object Here</h1>
        <table style="border-collapse: collapse; border: 1px solid gray;">
            <tr>
                <th>User Id</th><th>Photo Path</th>
            </tr>
            <%
                for (UserTest userTest : userList) {
            %>
            <tr>
                <td><%=userTest.getUserid()%></td>
                <td><%=userTest.getPhotopath()%></td>
            </tr>
            <%
                }
            %>
        </table>
    </body>
</html>

 

No comments :

Post a Comment