I connected jsp with mysql. i m beg. level programmer in JSP. So, plz dont comment on my code writing style, bt plz help me to improve ma programming. I can execute query bt whn i try RESULTSET to retrive, getting error.
my code is below :

<%@page import = "java.sql.* "%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>

   <body>
<%!     Connection Db;
                ResultSet rs;
                Statement Dr;
                String query, pass, typ,pnt,user;

%>

 <%
            Class.forName ("com.mysql.jdbc.Driver");
                Db = DriverManager.getConnection("jdbc:mysql://localhost/bug_track", "root","root");
                query= "Select * from login" ;
                Dr = Db.createStatement();
                rs=Dr.executeQuery(query);


do {
 user = rs.getString(1);
 pass = rs.getString(2);
 typ = rs.getString(3);
 pnt = user + "   "  +pass+"  "+typ;
 %>

 <p> <%=pnt%> </p>
 <%
 }while(rs.next());
     %>


 </body>
</html>

Recommended Answers

All 3 Replies

It has been a while so I could be remembering this incorrectly.

I believe you need to do your rs.next() before your rs.getstring(). The execute query does not retrieve any data from the database, only prepares it for the first rs.next(). By having a do...while, your while is not executed until the end of the do loop.

Try changing your
do
{
}while rs.next()
to
while rs.next()
{
}

Let me know if that doesn't fix the problem.

I connected jsp with mysql. i m beg. level programmer in JSP. So, plz dont comment on my code writing style, bt plz help me to improve ma programming. I can execute query bt whn i try RESULTSET to retrive, getting error.
my code is below :

<%@page import = "java.sql.* "%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
   
   <body>
<%!     Connection Db;
                ResultSet rs;
                Statement Dr;
                String query, pass, typ,pnt,user;

%>

 <%
            Class.forName ("com.mysql.jdbc.Driver");
                Db = DriverManager.getConnection("jdbc:mysql://localhost/bug_track", "root","root");
                query= "Select * from login" ;
                Dr = Db.createStatement();
                rs=Dr.executeQuery(query);


do {

 pnt = user + "   "  +pass+"  "+typ;
 %>

 <p> <%=pnt%> </p>
 <%
 }while(rs.next());
     %>


 </body>
</html>

I copied ths code from "Complete Reference : J2EE". Actually, i don thnk thr is problem. but getting error in,

user = rs.getString(1);
 pass = rs.getString(2);
 typ = rs.getString(3);

Remember that a "do while" loop executes the logic inside the loop and then executes the while. In your case, you are trying to do an rs.getString(1) but have never done the rs.next to load the values.

Your code should look like this

<%
while(rs.next)
{
      user = rs.getString(1);
      pass = rs.getString(2);
      typ = rs.getString(3);
%>
pnt = user + " " +pass+" "+typ;
%>
<p> <%=pnt%> </p>
<%
}; <== Not sure if you need the ";".  I forget the syntax for the end of a while loop.
%>

If this still blows up, please repost your code and tell exactly what line is blowing up.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.