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.


31 comments:

  1. I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.

    Best Java Training Institute Chennai
    Java Training Institute Bangalore

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
  3. BCA Colleges in Noida

    CIIT Noida provides Sofracle Specialized B Tech colleges in Noida based on current industry standards that helps students to secure placements in their dream jobs at MNCs. CIIT provides Best B.Tech Training in Noida. It is one of the most trusted B.Tech course training institutes in Noida offering hands on practical knowledge and complete job assistance with basic as well as advanced B.Tech classes. CIITN is the best B.Tech college in Noida, greater noida, ghaziabad, delhi, gurgaon regoin .

    At CIIT’s well-equipped Sofracle Specialized M Tech colleges in Noida aspirants learn the skills for designing, analysis, manufacturing, research, sales, management, consulting and many more. At CIIT B.Tech student will do practical on real time projects along with the job placement and training. CIIT Sofracle Specialized M.Tech Classes in Noida has been designed as per latest IT industry trends and keeping in mind the advanced B.Tech course content and syllabus based on the professional requirement of the student; helping them to get placement in Multinational companies (MNCs) and achieve their career goals.

    MCA colleges in Noida we have high tech infrastructure and lab facilities and the options of choosing multiple job oriented courses after 12th at Noida Location. CIIT in Noida prepares thousands of engineers at reasonable B.Tech course fees keeping in mind training and B.Tech course duration and subjects requirement of each attendee.

    Engineering College in Noida"

    ReplyDelete
  4. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
    DevOps online Training|DevOps Training in USA
    Devops Training in Chennai

    Devops Training in Bangalore

    ReplyDelete
  5. Awesome..You have clearly explained …Its very useful for me to know about new things..Keep on blogging..
    python training in rajajinagar
    Python training in btm
    Python training in usa
    Python training in marathahalli

    ReplyDelete
  6. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.
    java training in annanagar | java training in chennai


    java training in marathahalli | java training in btm layout

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. Thanks for posting this info. I just want to let you know that I just check out your site and I find it very interesting and informative. I can't wait to read lots of your posts
    Data Science training in rajaji nagar | Data Science with Python training in chenni
    Data Science training in electronic city | Data Science training in USA
    Data science training in pune | Data science training in kalyan nagar

    ReplyDelete
  9. The young boys ended up stimulated to read through them and now have unquestionably been having fun with these things.
    best safety training in chennai

    ReplyDelete
  10. Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.

    AWS Training in Bangalore | Amazon Web Services Training in bangalore , india

    AWS Training in pune | Amazon Web Services Training in Pune, india

    ReplyDelete
  11. your blog information's are really creative and It contains full of new innovative ideas.thank you for sharing with us.please update more data.
    AWS Training in Saidapet
    AWS Training in Ambattur
    AWS Certification Training in Bangalore
    AWS Certification Training

    ReplyDelete
  12. My spouse and I love your blog and find almost all of your post’s to be just what I’m looking for
    iosh course in chennai

    ReplyDelete
  13. I would like to appreciate you for making it very simple and easy
    Regards,
    PHP Training in Chennai | PHP Course in Chennai

    ReplyDelete
  14. Really very happy to say that your post is very interesting. I never stop myself to say something about it. You did a great job. Keep it up.
    We have an excellent IT courses training institute in Hyderabad. We are offering a number of courses that are very trendy in the IT industry. For further information, please once go through our site.AWS Training In Hyderabad

    ReplyDelete
  15. "Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more.

    Digital Marketing Training Course in Chennai | Digital Marketing Training Course in Anna Nagar | Digital Marketing Training Course in OMR | Digital Marketing Training Course in Porur | Digital Marketing Training Course in Tambaram | Digital Marketing Training Course in Velachery
    "

    ReplyDelete