Results 1 to 5 of 5
  1. #1
    enjoybullife is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default Problem with Jdbc

    Hi,
    I have assignment to create webdesign using html and servlets.

    When a new user registered i want those details stored in database using jdbc.
    Here is my code:
    import java.io.IOException;
    import java.io.PrintStream;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    public class registerservlet extends HttpServlet
    {
    Connection con = null;
    Statement stmt = null;
    PreparedStatement pstmt1 = null;
    PreparedStatement pstmt2 = null;


    ResultSet rs = null;
    String JDBCUrl = "jdbc:oracle:thin:@136.206.35.131:1521:SSD";
    String username = "ee_user";
    String password = "ee_pass";

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("<HTML><HEAD><TITLE>Database Servlet</TITLE></HEAD>");
    out.println("<BODY><H1>Database Values</H1>");

    // Now we add our database code!
    try {
    System.out.println("\nConnecting to the SSD Database......");
    Class.forName("oracle.jdbc.driver.OracleDriver");
    con = DriverManager.getConnection(JDBCUrl, username, password);
    }
    catch (Exception e) {
    out.println("An error has occurred during the connection phase! Did you upload your Oracle Drivers?");
    }
    try
    {
    out.println("Connection Successful..... creating statement....");

    String regnumber = request.getParameter("regnumber");
    String password = request.getParameter("password");
    String firstname = request.getParameter("firstname");
    String lastname = request.getParameter("lastname");

    String email = request.getParameter("email");
    String address = request.getParameter("address");


    PreparedStatement pstmt1 = con.prepareStatement("insert into pa_customers(regnumber,firstname,lastname,password ,email,address)" + "values(?,?,?,?,?,?)");
    pstmt1.clearParameters();
    pstmt1.setString(1, regnumber);

    pstmt1.setString(4, password);
    pstmt1.setString(2, firstname);
    pstmt1.setString(3, lastname);


    pstmt1.setString(5, email);

    pstmt1.setString(6, address);

    pstmt1.executeUpdate();}
    catch (Exception e) {
    out.println("Error occured while insert opration");

    }
    try{

    pstmt2 = con.prepareStatement("select * from pa_customers");
    rs = pstmt2.executeQuery("");

    while (rs.next())
    out.println("<BR>Name=" + rs.getString("FIRSTNAME") + " " + rs.getString("LASTNAME"));
    }
    catch (Exception e)
    {
    out.println("<BR>An error has occurred during the Statement/ResultSet phase. Please check the syntax and study the Exception details!");
    }
    finally
    {
    try {
    if (rs != null) rs.close();
    if (stmt != null) stmt.close();
    if (con != null) con.close();
    }
    catch (Exception ex)
    {
    out.println("<BR>An error occurred while closing down connection/statement");
    }
    }

    }
    public void doPost(HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException, IOException {
    doGet(request, response);
    }
    }

    can any one tell me wats wrong with the code

    Thank you,

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    It's not formatted for starters.
    Use code tags please.

    Next you have given your servlet state...that is you have a Connection, a Statement and a couple of PreparedStatement attributes. Servlets run in a multi-threaded environment, so all those things will be shared amongst any number of threads causing a great big headache. Your db stuff should be in a separate class...a different layer if you like.

    You also shouldn't be doing diaplay stuff in here, it should forward to a JSP to do all that.

    And you're eating your exceptions...you should ensure you do a printStackTrace() as a minimum.

    On the plus side you are at least closing your resources in a finally block, which is nice to see. However each bit should be in its own try/catch. If the closing the resultset throws an exception then you won't close the statements or connection with the way you have it currently.

    ...oh, and you haven't actually told us what problem you're having.

  3. #3
    enjoybullife is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default

    Hi tolls,

    Thx for the detail explanation.

    Sorry abt code thing this is my first time here.

    Problem is when i connect this servlet to the html registration form it is not storing the values to db.

    It is giving exceptions
    1.Error occured while insert opration.
    2.An error has occurred during the Statement/ResultSet phase. Please check the syntax and study the Exception details.

    Don't know what went wrong,

    Thank you

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    And this is why you do a ex.printStackTrace() from the exception, and not just print a message...because that tells you more about the exception.

    However, looking at this:
    rs = pstmt2.executeQuery("");

    You've created a PreparedStatement (in this case not actually necessary since you haven't got any parameters, a Statement would do), and then told it to execute a different query...and that query is blank.

  5. #5
    enjoybullife is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

Similar Threads

  1. jdbc problem!!! pls help
    By dilanka007 in forum New To Java
    Replies: 1
    Last Post: 04-14-2010, 02:16 PM
  2. jdbc problem!!! pls help
    By dilanka007 in forum New To Java
    Replies: 5
    Last Post: 01-16-2010, 06:07 PM
  3. problem with jdbc in jsp
    By nishant.4545 in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 08-29-2009, 02:31 PM
  4. Problem in JDBC
    By Preethi in forum New To Java
    Replies: 11
    Last Post: 01-08-2009, 06:18 PM
  5. JDBC problem
    By Swamipsn in forum New To Java
    Replies: 1
    Last Post: 08-13-2007, 07:05 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
  •