Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Tolls

Thread: My code was not executed properly.It will jumping to exception handling.my code is

  1. #1
    vinay4051 is offline Member
    Join Date
    Aug 2011
    Posts
    3
    Rep Power
    0

    Default My code was not executed properly.It will jumping to exception handling.my code is

    package com.jsp_exp.servlet;

    import java.io.IOException;
    import java.io.*;
    import java.sql.*;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;




    /**
    * Servlet implementation class F1
    */
    public class F1 extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
    * Default constructor.
    */
    public F1() {
    // TODO Auto-generated constructor stub
    }

    /**
    /* * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub


    String loginUser = "root";
    String loginPasswd = "fortune";

    String loginUrl = "jdbc:mysql://localhost:3306/db1";

    response.setContentType("text/html"); // Response mime type

    // Output stream to STDOUT
    PrintWriter out = response.getWriter();

    out.println("<HTML><HEAD><TITLE> JSP Sample</TITLE></HEAD>");
    out.println("<BODY><H1> JSP Sample</H1>");

    // Load the mm.MySQL driver
    try
    {
    Class.forName("com.mysql.jdbc.Driver");
    Connection dbcon = DriverManager.getConnection(loginUrl, loginUser, loginPasswd);
    // Declare our statement
    Statement statement = dbcon.createStatement();

    String query = "SELECT fname, lname, age, email";
    query += "FROM mydata ";

    // Perform the query
    ResultSet rs = statement.executeQuery(query);

    out.println("<TABLE border>");

    // Iterate through each row of rs
    while (rs.next())
    {
    String m_fname = rs.getString("fname");
    String m_lname = rs.getString("lname");
    String m_age = rs.getString("age");
    string m_email=rs.getstring("email")
    out.println("<tr>" +
    "<td>" + m_fname + "</td>" +
    "<td>" + m_lname + "</td>" +
    "<td>" + m_age + "</td>" +
    "<td>" + m_email + "</td>" +
    "</tr>");
    }

    out.println("</TABLE>");

    rs.close();
    statement.close();
    dbcon.close();
    }
    catch (SQLException ex) {
    while (ex != null) {

    System.out.println ("SQL Exception: " + ex.getMessage ());
    ex = ex.getNextException ();
    } // end while
    } // end catch SQLException

    catch(java.lang.Exception ex)
    {
    out.println("<HTML>" +
    "<HEAD><TITLE>" +
    "Bedrock: Error" +
    "</TITLE></HEAD>\n<BODY>" +
    "<P>SQL error in doGet: " +
    ex.getMessage() + "</P></BODY></HTML>");
    return;
    }
    out.close();
    }


    public void doPost(HttpServletRequest inRequest,
    HttpServletResponse outResponse) throws ServletException,
    IOException {
    doGet(inRequest, outResponse);
    }
    }


    I AM GETTING OUTPUT AS:

    JSP Sample

    SQL error in doGet: com.mysql.jdbc.Driver

    PLZ HELP ME

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,997
    Rep Power
    19

    Default

    Please use code tags when posting code so it retains its formatting.

    Also, when handling exceptions use printStackTrace() so you know what exactly is causing the problem.
    In this case I'm going to hazard a guess that it's this line:
    Java Code:
    Class.forName("com.mysql.jdbc.Driver");
    throwing a ClassNotFoundException, probably because you haven't got the MySQL driver in your WEB-INF/lib directory.

  3. #3
    vinay4051 is offline Member
    Join Date
    Aug 2011
    Posts
    3
    Rep Power
    0

    Default

    can u plz send me sample code?????? i am not sure about my is correct. i am using linux ubuntu.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,997
    Rep Power
    19

    Default

    It's got nothing to do with code (and we don't supply code here).
    It's to do with you supplying us with the full exception (plus stack trace) and then acting on our advice.
    I made a guess at the problem based on your incomplete information...so, have you done what I suggested?
    Fubarable likes this.

Similar Threads

  1. Replies: 3
    Last Post: 05-02-2011, 08:02 PM
  2. Replies: 1
    Last Post: 12-27-2010, 11:09 PM
  3. Properly formatted code
    By Bgreen7887 in forum New To Java
    Replies: 1
    Last Post: 11-22-2010, 04:28 PM
  4. How to stop thread from being jumping off the code without executing it.....
    By chiragkini in forum Threads and Synchronization
    Replies: 6
    Last Post: 01-22-2009, 03:38 AM
  5. My code is not working properly ..modify it
    By Shyam Singh in forum New To Java
    Replies: 14
    Last Post: 07-16-2008, 05:48 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
  •