954,574 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Unable to connect database From jsp page

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?

<%@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("<strong>form1</strong>"); 
          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 and Regards
haresh

guravharsha
Light Poster
42 posts since Jun 2008
Reputation Points: 8
Solved Threads: 0
 

As Peter_budo had put it in one of his earlier posts, "Its hard to read errors from a crystal ball".

But it seems like you have no intention of learning anything from here, you just come here for quick fixes to your problems. Practically every one has already told you not to do database connectivity inside a JSP, but you refuse to budge and continue to persist in your naive ways.

stephen84s
Nearly a Posting Virtuoso
1,443 posts since Jul 2007
Reputation Points: 668
Solved Threads: 154
 

Really very difficult to find error from such a big JSP programming code. I will advise you to contact any Java or JSP programmer. Hope you will find the error.

ruthallison
Newbie Poster
4 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 

Do not use scriptlets in your jsp (NEVER EVER) use expression Language and JSTL instead. JSP are not ment for logic they are only meant for displaying static HTML; If you want to do any processing then use a Servlet.

Strip out all your JDBC connection code and put it inside a Servlet and override the init method from HttpServlet. Put all your connection parameters in a init-param tag in your Deployment Descriptor (web.xml).

Please don't ever write code like this again

ejosiah
Junior Poster
188 posts since Feb 2008
Reputation Points: 72
Solved Threads: 23
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You