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