Results 1 to 2 of 2
  1. #1
    sharanya is offline Member
    Join Date
    Feb 2011
    Posts
    10
    Rep Power
    0

    Default executing catch block by saying unable to connect to database

    I am trying to retrieve data from database in atable
    for that i created the jsp pages

    the code is as follows
    hi this my first page


    welcome_to_database.jsp

    <!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>display data from the table using jsp</title>

    </head>

    <body>

    <br><br><br><br><br><br><br>

    <TABLE align="center" style="background-color: #ffffcc;">

    <TR>

    <TD align="center"><h2>To display all the data from the table click here...</h2></TD>

    </TR>

    <TR>

    <TD align="center"><A HREF="ConnectJspToMysql.jsp">

    <font size="4" color="blue">show data from table</font></A></TD>

    </TR>

    </TABLE>

    </body>

    </html>


    and this is ConnectJspToMysql.jsp


    <!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>display data from the table using jsp</title>

    </head>

    <body>

    <h2 align="center">Data from the table 'emp' of database 'empLeave12'</h2>

    <%

    try {

    /* Create string of connection url within specified format with machine name, port number and database name. Here machine name id localhost and database name is student. */

    String connectionURL = "jdbc:mysql://localhost:3306/empLeave12";



    // declare a connection by using Connection interface

    Connection connection = null;



    // declare object of Statement interface that is used for executing sql statements.

    Statement statement = null;



    // declare a resultset that uses as a table for output data from tha table.

    ResultSet rs = null;



    //*Load JBBC driver "com.mysql.jdbc.Driver"

    Class.forName("com.mysql.jdbc.Driver").newInstance ();



    /* Create a connection by using getConnection() method that takes parameters of string type connection url, user name and password to connect to database. */

    connection = DriverManager.getConnection(connectionURL, "root", "notallowed");



    /* createStatement() is used for create statement object that is used for sending sql statements

    to the specified database. */

    statement = connection.createStatement();



    // sql query to retrieve values from the secified table.

    String QueryString = "SELECT * from emp";

    rs = statement.executeQuery(QueryString);

    %>

    <TABLE cellpadding="15" border="1" style="background-color: #ffffcc;" align="center">

    <%

    while (rs.next()) {

    %>

    <TR>

    <TD><%=rs.getInt(1)%></TD>

    <TD><%=rs.getString(2)%></TD>

    <TD><%=rs.getString(3)%></TD>

    <TD><%=rs.getString(4)%></TD>

    </TR>

    <% } %>

    <%

    // close all the connections.

    rs.close();

    statement.close();

    connection.close();

    } catch (Exception ex) {

    %>

    </font>

    <font size="+3" color="red"></b>

    <%

    out.println("Unable to connect to database.");

    }

    %>

    </TABLE><TABLE align="center">

    <TR>

    <TD><FORM ACTION="welcome_to_database_query.jsp" method="get" >

    <button type="submit"><-- back</button></TD>

    </TR>

    </TABLE>

    </font>

    </body>

    </html>


    when i run these in browser whatever there in the catch block is executing

    can any one give the reply

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    First you must you use special tag code /code.
    Second I should recommend you divide code on the MVC, i.e. you can take out code which retrieve data from database in special class and used it in jsp.
    Skype: petrarsentev
    http://TrackStudio.com

Similar Threads

  1. unable to connect remote database using jdbc
    By enggvijaysingh@gmail.com in forum Advanced Java
    Replies: 1
    Last Post: 12-16-2010, 01:16 PM
  2. Replies: 4
    Last Post: 04-28-2010, 07:13 PM
  3. Try/catch block
    By swati.jyoti in forum New To Java
    Replies: 5
    Last Post: 07-02-2009, 03:32 PM
  4. Question reg try/catch block
    By nn12 in forum New To Java
    Replies: 1
    Last Post: 09-16-2008, 06:56 PM
  5. try...catch block
    By javaplus in forum New To Java
    Replies: 3
    Last Post: 11-06-2007, 08:53 PM

Posting Permissions

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