Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: jbbc problem

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default jbbc problem

    In my assignment i need to create html form for registering customers and those values need to be store in database.

    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 please help me
    Thank you
    Last edited by Json; May 24th, 2010 at 02:50 AM. Reason: Please use code tags.


  2. #2
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: jbbc problem

    Quote Originally Posted by enjoybullife View Post
    can any one please help me
    Maybe if you explained what the problem is?

    When posting code, please put &#91;CODE]...&#91;/CODE] tags around it so it is readable.

  3. #3
    Junior Member
    Join Date
    May 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: jbbc problem

    Hi dlrode,

    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
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: jbbc problem

    If you want your code read, post it in CODE tags as requested. If not, why post it?

    If you want help with error messages and exceptions, post the full text of the error message and the stack trace.