Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jul 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unable to connect database

    Hi,

    Here is my jsp page where i trying to display the data from table. Here form1 is the name of drop down list in my searchuser.jsp page. The content in the drop down list is a name of one column of the database. I am able to connect firstjsp page showing item available in drop down menu.But when I am running this page I am failing to connect it to database.
    Whether My marked query is right or wrong?
    Code:
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@ page language ="java" %>
    <%@ page import="java.sql.*, javax.sql.*, javax.naming.*,java.io.*,java.util.*" %>
    <!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>      
                  
              <% 
              
              String form=request.getParameter("form1"); 
              try {
              
              Connection connection = null;
              Statement st = null;
              ResultSet rs = null;
              Class.forName("com.mysql.jdbc.Driver");
              Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mynewdatabase","root", "root123");
               st=con.createStatement();
              
                   rs = st.executeQuery("select emailid ,password  from user where emailid='"+form+"'");
                         rs.next();
                         String EMAIL = rs.getString("emailid");
                         String PASSWORD = rs.getString("password");
                         rs.close();
                         %>
                              
                       <table border="1">
                            <%
                          while (rs.next()) {
                               %>
    
                     <tbody>
                      <tr><thead>
                      <tr>
                       <th><b>MODIFY USER</b></th>
                     </tr>
                       </thead>
                           <td>E-MAIL</td>
                         <td><input type="text" name="email" value=="<%=EMAIL%>" size="30" /> </td>
                        </tr>
                        <tr>
                           <td>PASSWORD</td>
                           <td><input type="text" name="password" value=="<%=PASSWORD%>" size="10" /></td>
                               </tr>
                             <tr>
                            <td><input type="submit" value="SUBMIT" name="submit" /></td>
                          <td><input type="reset" value="RESET" name="res" /></td>
                            </tr>
                            </tbody>
                            
                   <%   }    %>
                <%
                 
                        rs.close();
                       st.close();
                        connection.close();
                } catch (Exception ex) {
                   %>
                   <%
                    out.println("Unable to connect to database.");
                }
            %>
    
    </table>       
        </body>
    </html>
    Any suggestion is highly appreciated.
    Thanks
    Haresh

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    first, you could try printing the exception so that it tells you why exactly your program is unable to connect to the database..
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #3
    New to the CF scene
    Join Date
    Jul 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi shyam,
    The Exception stack is :Illegal operation on empty result set.Kindly help me as i am newbie in java
    Thanks and Regards
    Haresh

  • #4
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    Quote Originally Posted by guravharhsa View Post
    [code]
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@ page language ="java" %>
    <%@ page import="java.sql.*, javax.sql.*, javax.naming.*,java.io.*,java.util.*" %>
    <!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>

    <%

    String form=request.getParameter("form1");
    try {

    Connection connection = null;
    Statement st = null;
    ResultSet rs = null;
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mynewdatabase","root", "root123");
    st=con.createStatement();

    rs = st.executeQuery("select emailid ,password from user where emailid='"+form+"'");
    rs.next();
    String EMAIL = rs.getString("emailid");
    String PASSWORD = rs.getString("password");
    rs.close();
    %>

    <table border="1">
    <%
    while (rs.next()) {
    %>

    <tbody>
    <tr><thead>
    <tr>
    <th><b>MODIFY USER</b></th>
    </tr>
    </thead>
    <td>E-MAIL</td>
    <td><input type="text" name="email" value=="<%=EMAIL%>" size="30" /> </td>
    </tr>
    <tr>
    <td>PASSWORD</td>
    <td><input type="text" name="password" value=="<%=PASSWORD%>" size="10" /></td>
    </tr>
    <tr>
    <td><input type="submit" value="SUBMIT" name="submit" /></td>
    <td><input type="reset" value="RESET" name="res" /></td>
    </tr>
    </tbody>

    <% } %>
    <%

    rs.close();
    st.close();
    connection.close();
    } catch (Exception ex) {
    %>
    <%
    out.println("Unable to connect to database.");
    }
    %>

    </table>
    </body>
    </html>[/code
    well, what did u expect...u've closed the resultset and then trying to fetch more data from it :O
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •