Click here to Skip to main content
15,917,859 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I hava Jsp code as:
jsp
<%@page import="org.apache.tomcat.util.net.SSLUtil"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

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

<html>
    <head>
        <title>Connection with mysql database</title>
    </head>
    <body>
        <h1>Connection status </h1>
        <%
            try {
                String connectionURL = "jdbc:mysql://dbno2.cfghe6zvdgnoq7r.ap-southeast-2.rds.amazonaws.com:3306/mydb?user=aghil&password=password1";
                Connection connection = null;
                Statement stmt = null;
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                connection = DriverManager.getConnection(connectionURL);
                if (!connection.isClosed()) {
                    stmt = connection.createStatement();
                }
                out.println("Successfully connected to MySQL server using TCP/IP...\n");
                String QueryString = "select username from mydb.userdetails WHERE username ='anil' and password='password'";
                ResultSet rs;
                String sre = null;
                try {
                    PreparedStatement statement = connection.prepareStatement(QueryString);
                    rs = statement.executeQuery();
                    sre = rs.getString(1);
                    out.println("\n" + sre);
                } catch (Exception e) {
                    out.println("\n" + e.toString());
                }
                connection.close();
            } catch (Exception ex) {
                out.println("Unable to connect to database." + ex);
            }
        %>
    </body>
</html>



and its output in browser is as follows :

<html><head>
<title>Connection with mysql database</title>
</head>
<body>

Connection status


Successfully connected to MySQL server using TCP/

java.sql.SQLException: Before start of result set></html>

why this? what to do?
Posted
Updated 15-Feb-13 22:55pm
v2

1 solution

PreparedStatement statement = connection.prepareStatement(QueryString);
rs = statement.executeQuery();
if(rs.next())
sre = rs.getString(1);
out.println("\n" + sre);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900