Tuesday, September 8, 2015

How to create java servlet application to connect MySQL database and retrieve information (Section 2)

In this section we continue our work...

Part III: Create Servlet Application to insert data into MySQl database.

Create New Web application
Fileànew ProjectàSelect Catogary(Java Web) and Project(Web Application) Click Next

Give Project Name and click Next.

Select Apache Server and Click Finish (No need to go to Frameworks).

Then you can see your project in left side panel. Expand it go to Web Pages and click index.html file.

Inside <body> tag write code shows in below.

< a href=”insertrecord.html”>Insert record</a>
Using this command we create link to insertrecord.html file & displayrecord.jsp file with in index.html.

Right Click Sample InsertàNewàHTML create new HTML called insertrecord.html
Right Click Sample InsertàNewàJsp create new Jsp called displayrecord.jsp(Make sure you enter correct html and jsp file name)

Then Run the application and you able to see this page in your Browser.

Click Insert Record Link and you can see this.

Then you need to important thing in project.

Right Click Sample Insert-->New-->Java Class create new Java Class called DatabaseConnection.java
Inside that write this code.

Specially you need to give same jdbc URL (I discussed it in part II) inside getConnection() method.

Then goto insertrecord.html and write thic code inside <body> tag.

You can see I create two text fields and name them as username and password. So when I insert data to that fields that data assign to username and password variables.

Make sure you write action=”InsertData” and method=”Post”. I tell important of it later.
After run insertrecord.html you can see this.

But when you fill it and click submit you will see http Error.

Let’s fix that error.

For that we need Java servlets.

Create Java Servlet.

Right Click Sample Insert-->New-->Servlet create new Servlet called InsertData (This Name should be same as action name in <form> tag insertrecord.html. because when we click submit button that data should pass using POST method into this servlet. POST method is secured than GET method. Because when we send data using POST method that data doesn’t display in URL) and Click Next.
Then click (web.xml) check box and Click Finish.

Inside that servlet generally have three methods called processRequest, doGet, doPost, getServletInfo

We Usually write our codes in processRequest( )method. So write this code inside it.

First we get submit our input and post it them into InsertData servlet. Then we need to expand them and same them into variables.
So using request.getParameter( ) we can get those details into String variables(those variables should be same as database table attributes).

Then write Sql query to insert data into MySQL database and execute it.

Then We Finish.

Save and run application. Enter details and click submit button.

Finally go to databases àExecute command and you can see successfully our details goto MySQL database.

No comments:

Post a Comment