Get all updates of Java4s
Follow Us: Email Newsletter

How to Connect Servlet to the Database with Example

Servlets » On Feb 7, 2013 { 14 Comments } By Sivateja

Let us see how to connect servelet application with (Oracle) database, for time being i am considering Oracle XE.  In our application i am going to display all the records from the table ‘Java4s‘.

  • Make sure you have java4s table in the database with some data in it
  • Make sure you have ojdbc14.jar file in your classpath and lib folder as well as [ why in two places ? :-) servlet will use ojdbc14.jar file which is in the classpath, for compiling the application, and ojdbc14.jar file in lib folder will be used at the time of running the servlet application], you can check the same in this figure…

Directory Structure

Files Required

  • index.html
  • ServletDatabaseConnect.java
  • web.xml
  • ojdbc14.jar

index.html

ServletDatabaseConnect.java

web.xml

Output

 

You Might Also Like

  ♦ About Author ♦

Java4s_Author
sivateja@java4s.com
Full Stack( Java/J2EE & UI/UX ) Developer and founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog. Sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

14 Responses to “How to Connect Servlet to the Database with Example”
  1. what is the difference between flush and commit?

  2. Akhilesh says:

    Hello sir ! i love ur tutorial..u r doing great work…plz post also jdbc ,jsp and sir in servlet u dont cover all topic..plz also post session ,filter etc..thankx in advance

  3. sidhu says:

    what the means of sqlexception?
    and which situation it will come
    plzz explain

  4. ASRAAR says:

    Hi !!!!!
    Can anyone explain filters in detail with code segments? like we have authentication filter , logging, data compression etc.

  5. Kannan K says:

    Sir ur tutorial is realy useful and could u pls add Servlet Chaining & session tracking in servlet

  6. Raju says:

    hi sir,
    We are eagerly wait for web servies, please provide the implementation of web services ASAP. Thankx a lot.

  7. srikanth says:

    hello sir,
    i want webservice tutorials please provide webservices ASAP,we have so many websites but not equal to your explanation,your explanation is very neat and clearly please provide webservices

    thank youuuuuuuu

  8. MSREDDY says:

    First for providing all topics in software but i need jsp tutorials

  9. RaviReddy says:

    jdbc ok
    can u tell how to connect servlet and hibernate
    login and registration form

  10. nagesh says:

    Very helpful tutorial.Thank you

  11. Sanjeet Kumar says:

    Making connection SQL server Database

    Using the sqljdbc.jar class library, applications must first register the driver as follows:

    Class.forName(“com.microsoft.sqlserver.jdbc.SQLServerDriver”);

    When the driver is loaded, you can establish a connection by using a connection URL and the getConnection method of the DriverManager class:

    String connectionUrl = “jdbc:sqlserver://localhost:1433;” +
    “databaseName=AdventureWorks;user=MyUserName;password=*****;”;
    Connection con = DriverManager.getConnection(connectionUrl);

  12. Alexander says:

    Many thanks.

  13. Gayatri says:

    Thank you for good explaination.

  14. Manoj Soni says:

    Very help full tutorial but plz update complete topic and plz update jsp tutorial..

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Useful Links Current & UpComing Tutorials
Upcoming
  Web Services [ SOAP ], JSP
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2016 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.