<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<p>Accessing Database from servlets through JDBC! Accessing Access Database From Servlet This article shows you how to access database from servlets.</p>
<h2>Create Table</h2>
<p>To create the Users table in TEST database, use the following steps:</p>
<p><b>Create Table</b></p>
<pre class="highlight">CREATE TABLE `user` ( 
 `id` bigint(20) NOT NULL AUTO_INCREMENT, 
 `age` bigint(20) DEFAULT NULL, 
 `gender` varchar(255) DEFAULT NULL, 
 `Hobbies` varchar(255) DEFAULT NULL, 
 `jobtype` varchar(255) DEFAULT NULL, 
 `username` varchar(255) DEFAULT NULL, 
 PRIMARY KEY (`id`) 
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1 
</pre>
<p> ;</p>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;"><span style="color: red; font-size: x-large; text-align: center;"><b>Popular Tutorials</b></span></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/spring-tutorial/"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-spring-boot-a-spring-boot-complete-guide/"><strong>Spring Boot Tutorial</strong> </a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint/"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/using-spring-jdbc-framework-chapter-32/"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-hateoas-hypermedia-driven-restful-web-service/"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/microservices-with-spring-boot/"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/jax-rs-web-service-tutorial/"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/hibernate-3-on-baby-steps/"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-batch-process-with-example/"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<p>We are assuming there are many records in this table. In this example, we are getting the data from the database in servlet and printing it. We are doing all the database logic in servlet for simplicity of the program. But it will be better to separate it from the servlet file.</p>
<h2><b>Example of Fetching Result-</b></h2>
<pre class="highlight">import java.io.*; 
import java.util.Enumeration; 
import javax.servlet.*; 
import javax.servlet.http.*; 
import java.sql.*; 
import java.net.*; 
 
public class ServletData extends HttpServlet{ 
 Connection theConnection; 
 private ServletConfig config; 
 
public void init(ServletConfig config) 
 throws ServletException{ 
 this.config=config; 
 } 
 
public void service (HttpServletRequest req, HttpServletResponse res) 
throws ServletException, IOException { 
 
 HttpSession session = req.getSession(true); 
 
 res.setContentType("text/html"); 
 
 PrintWriter out = res.getWriter(); 
 
 out.println("<;HTML>;<;HEAD>;<;TITLE>;Emai List.<;/TITLE>;"); 
 
 out.println("<;/HEAD>;"); 
 
 out.println("<;BODY bgColor=blanchedalmond text=#008000 topMargin=0>;"); 
 
 out.println("<;P align=center>;<;FONT face=Helvetica>;<;FONT color=fuchsia style="BACKGROUND-COLOR: white">;<;BIG>;<;BIG>;List of users.<;/BIG>;<;/BIG>;<;/FONT>;<;/P>;"); 
 
 out.println("<;P align=center>;"); 
 
out.println("<;TABLE align=center border=1 cellPadding=1 cellSpacing=1 width="75%">;"); 
 
 
 
 out.println("<;TR>;"); 
 
 out.println("<;TD>;ID<;/TD>;"); 
 
 out.println("<;TD>;AGE<;/TD>;"); 
out.println("<;TD>;GENDER<;/TD>;"); 
out.println("<;TD>;Hobbies<;/TD>;"); 
out.println("<;TD>;jobtype<;/TD>;"); 
 out.println("<;TD>;username<;/TD>;<;/TR>;"); 
 
try{ 
 
 
 //Loading Sun's JDBC ODBC Driver 
 Class.forName("com.mysql.jdbc.Driver"); 
 
 
 //Connect to emaildb Data source 
 theConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/DOJDB", "root", "root"); 
 
 
 Statement theStatement=theConnection.createStatement(); 
 
 ResultSet theResult=theStatement.executeQuery("select * from user"); //Select all records from user table. 
 
 //Fetch all the records and print in table 
 while(theResult.next()){ 
 
 out.println(); 
 
 out.println("<;TR>;"); 
 
 out.println("<;TD>;" + theResult.getString(1) + "<;/TD>;"); 
 
 out.println("<;TD>;" + theResult.getString(2) + "<;/TD>;"); 
 
 String s=theResult.getString(3); 
 
 out.println("<;TD>;<;a href=" + s + ">;" + s + "<;/a>;<;/TD>;"); 
 out.println("<;TD>;" + theResult.getString(4) + "<;/TD>;"); 
 out.println("<;TD>;" + theResult.getString(5) + "<;/TD>;"); 
 out.println("<;TD>;" + theResult.getString(6) + "<;/TD>;"); 
 out.println("<;/TR>;"); 
 
 } 
 
 theResult.close();//Close the result set 
 
 theStatement.close();//Close statement 
 
 theConnection.close(); //Close database Connection 
 
 }catch(Exception e){ 
 
 out.println(e.getMessage());//Print trapped error. 
 
 } 
 
 out.println("<;/TABLE>;<;/P>;"); 
 
 out.println("<;P>;&;nbsp;<;/P>;<;/FONT>;<;/BODY>;<;/HTML>;"); 
 
 } 
 
 public void destroy(){ 
 
 } 
 
} 
 
</pre>
<p> ;</p>
<div class="separator" style="clear: both; text-align: center;"><img title="Servlets - Database Access" src="https://dineshonjava.com/wp-content/uploads/2014/01/1111-19.png" border="0" /></div>
<p> ;</p>
</div>
<p> ;</p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/servlet-filters/">Previous</a> <;<; || <a href="https://dineshonjava.com/java-servlets-overview/">Index </a>|| >;>;<a href="https://dineshonjava.com/servlet-upload-file-example/" target="_blank" rel="noopener">Next</a> >;>;</b></div>
<p> ;</p>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;">
<p><b>Servlet Related Posts</b></p>
<ol style="text-align: left;">
<li><b><a href="https://dineshonjava.com/java-servlets-overview/">Java Servlets Overview</a></b></li>
<li><b><a href="https://dineshonjava.com/servlet-life-cycle/">Servlet Life Cycle</a></b></li>
<li><b><a href="https://dineshonjava.com/hello-world-example-in-servlet-interface/">Servlet Example</a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-servletconfig-and-cervletcontext-in-java-servlet/ "> Difference between ServletConfig and ServletContext<br />
</a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-genericservlet-and/"> Difference between GenericServlet and HttpServlet<br />
</a></b></li>
<li><b><a href="https://dineshonjava.com/what-is-web-application-servlet/">What is web application?</a></b></li>
<li><b><a href="https://dineshonjava.com/advantages-of-servlets-over-cgi/">Advantages of Servlets over CGI</a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-genericservlet-and/">GenericServlet Example</a></b></li>
<li><b><a href="https://dineshonjava.com/requestdispatcher-interface/">RequestDispatcher Example</a></b></li>
<li><b><a href="https://dineshonjava.com/servletconfig-interface/">ServletConfig</a></b></li>
<li><b><a href="https://dineshonjava.com/servletcontext-interface/">ServletContext </a></b></li>
<li><b><a href="https://dineshonjava.com/servlet-filters/">Servlet Filter Example</a></b></li>
<li><b><a href="https://dineshonjava.com/servlet-upload-file-example/">File Uploading Example using Servlet</a></b></li>
</ol>
</div>
<p> ;</p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/sending-email-through-javamail-api-in/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/javalangclassnotfoundexception/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '252'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…