Showing posts with label Servlet. Show all posts
Showing posts with label Servlet. Show all posts

Saturday, 16 April 2016

Input data in webpage and store it in Database using SERVLET

In this program, we will input data from webpage and then store it into database using JDBC and Servlet.
For example: please see below screenshots:








Data successfully inserted in the database table.






Here first we will design web page (LoginScreen.html) where we insert the fields:

<form method="post" action="Login">
UserName: <input name="userName" type="text" required="required" placeholder="userName"/>
Enter Password: <input name="password" type="password" required="required" placeholder="passWord"/>
<!--  ReEnter Password: <input name="repass" type="password" required="required" placeholder="passWord"/>-->
Email Id: <input name="email" type="email" required="required" />
<input type="submit" name="submit"   />

</form>

Change the welcome file in WEB.xml , so when servlet start, it will pick LoginScreen.html file.

<welcome-file-list>
    <welcome-file>LoginScreen.html</welcome-file>
  </welcome-file-list>

And all the logic for inserting data in DB will come in doPOST method of HTTPServlet. We will override this method in our class. 

              String userName=request.getParameter("userName");
              String pass=request.getParameter("password");
              String toemail=request.getParameter("email");
              response.setContentType("text/html");
              PrintWriter writer=response.getWriter();
             
              try {
                     Class.forName("org.postgresql.Driver");
                     Connection c=DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres", "postgres", "oracle");
                     String ins_new_user="Insert into new_user(user_id,sys_creation_date,sys_update_date,user_status,user_name,passwd,email) values"
                                  + "(nextval('user_id_1sq'),current_date,null,'A','"+userName+"','"+pass+"','"+toemail+"');";
                     PreparedStatement pstmt=c.prepareStatement(ins_new_user);
                     pstmt.execute();
                     writer.println("Hi "+userName+ " Your data is created!");
                    
                    
              } catch (SQLException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
              } catch (ClassNotFoundException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
              }

How to fix No suitable driver found for jdbc:postgresql://localhost/dbname” Exception in SERVLET

The reason you got this error :
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/dbname
When running TOMCAT in eclipse , it won't pick the libraries set in CATALINA_HOME/lib. For correcting this exception, we need to add the JDBC jar in Tomcat plugin config.
Double click on tomcat server present in Server tab of eclipse and then open 'Open launch Configuration' and then add jars in CLASSPATH tab. Here we are adding postgresql's jar.
Please see screenshots: