0

i have a page in jsp where i am dynamically displaying records from the database in a table, with dynamic rows being generated. With every row, there is a radio button so that the user can select one row.

The code for page1.jsp is:

<form action="page2.jsp" method="post">

<table>
<%
 try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb,        *.accdb)};DBQ=C:/Users/hp/Documents/Sample1.accdb";

Connection con = DriverManager.getConnection(database, "", ""); 
Statement stmnt = con.createStatement();

String source=request.getParameter("from");
String dest=request.getParameter("to")
ResultSet resultset = stmnt.executeQuery("select * from Trains123 where From='" +source+    "' and To='" +dest+ "' ");

     while(resultset.next())
     {          
         %>        

<tr>

<td><% out.println(resultset.getString("From")); %></td>
<td><% out.println(resultset.getString("To")); %></td>
<td><% out.println(resultset.getString("TrainName")); %></td>        


<td><input type="radio" name="TName" value="<%=    resultset.getString("TrainName")%>">book</td>
</tr>
</table>
</form>

and the code on page2.jsp is:

<% out.println(request.getParameter("Tname")); %>

There is no problem with the record set, the values are getting printed. but the radio button is not being displayed.Also, If there are 2 records in the recordset, only the first one gets printed without the radio button. I debugged the file, the control stops at the radio button line.

I have no idea, where the error is and what is the problem with giving dynamic value to the radio button. All i want to do is to find out the row selected by the user.

Seema
  • 107
  • 2
  • 5
  • 13

1 Answers1

0

AFAIR JDBC specification does not give guarantee, that you can call getXxx on a result set with the same column name/number twice. Most of JDBC drivers do support it, but some might not I am not sure if ODBC is not probably this exception. So first try to get values of all columns in a row to extra variables like this:

String from = resultset.getString("From");
String to = resultset.getString("To");
String trainName = resultset.getString("TrainName");

and use it in this way:

<tr><td><%=form%></td>
    <td><%=to%></td>
    <td><%=trainName%></td>
    <td><input type="radio" name="TName" value="<%=trainName%>"/> book</td>
</tr>

Also please do escape these values, as if trainName would contain '"' character, you may break HTML code.

But much more important is the way you wrote your code. First of all you should separate view layer (JSP) from the code. So you should create a class, that reads list of trains and simply put it into request, more or less like this:

public class RailLink {
    public String from;
    public String to;
    public String trainName;
}

public class Trains {

    private Connection getConnection() {
        //- This code actually should be put somewhere else.
//          Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//          String db = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:/Users/hp/Documents/Sample1.accdb";
//          Connection cn = DriverManager.getConnection(db, "", ""); 
            return cn;
    }

    public static List<RailLink> findLinks(String from, String to) {
        assert from != null && to != null;
        List<RailLinks> links = new LinkedList<>();
        try (
            Connection cn = getConnection();
            PreparedStatement ps = cn.prepareStatement("SELECT * FROM Trains123 WHERE \"From\" = ? AND \"To\" = ?");
        ) {
            ps.setString(1, from);
            ps.setString(2, to);
            try (ResultSet rs = ps.executeQuery()) {
                while (rs.next()) {
                    RailLink link = new RailLink();
                    link.from = rs.getString("From");
                    link.to = rs.getString("To");
                    link.trainName = rs.getString("TrainName");
                    links.add(link);
                }
            }
        }
        return links;
    }
}

Now write a servlet which handles your form:

public class FindLinks extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse respone) {
        List<RailLink> links = Trains.findLinks(request.getParameter("from"), request.getParameter("to"));
        request.setAttribute("rail-links", links);
        request.getRequestDispatcher("page1.jsp").forward(request, response);
    }
}

And finally modify your page1.jsp as:

<%@page ...%>
...
<%  List<RailLink> links = request.getAttribute("rail-links");
    if (links != null) {%>
        <table><%
        for (RailLink l : links) {%>
            <tr><td><%=l.from%></td>
                <td><%=l.to%></td>
                <td><%=l.trainName%></td>
                <td><input type="radio" name="TName" value="<%=l.trainNam%"/> book</td>
        <%}%>
        </table><%
    }
    else {%>
        <p>No connections found.</p><%
    }%>

Have a good luck with it.

Cromax
  • 1,822
  • 1
  • 23
  • 35
  • I've tried this method as well <%=form%> <%=to%> <%=trainName%> book but it doesn't work. wherever i try to enter server side code, it doesn't work. – Seema Feb 09 '14 at 18:56
  • I don't get any error. I just get an empty . No error Im very new to java, i've just known jsp, sorry i don't know about servlets and all. is there any way to solve it using just jsp? – Seema Feb 10 '14 at 13:18
  • It's hard to track it down this way... OK, on what server do run your JSPs? Tomcat? Jetty? Something else? – Cromax Feb 10 '14 at 13:19
  • Im using GlassFish server. – Seema Feb 11 '14 at 15:42
  • Can you get logs? Read here http://stackoverflow.com/questions/13835913/location-of-glassfish-server-logs, how to find them on GlassFish. – Cromax Feb 11 '14 at 17:42