Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

Search: 

  Tutorial: Accessing Database from servlets through JDBC!

Accessing Database from servlets through JDBC! Accessing Access Database From Servlet T his article shows you how to access database from servlets. Here I am assuming that you are using win95/98/2000 and running Java Web Server. For the sake of

Tutorial Details:

simplicity I have used Microsoft Access Database as backend and Sun's JDBC-ODBC bridge to connect to access data source. First of all download the source code and database used in this tutorial and now follow the following the steps.
Unzip the downloaded file into your favorite directory.
Create an ODBC data source "emaildb" by selecting "emaildb.mdb" database from unzipped folder.
Compile you emaildb.java file, move emaildb.class file to Java Web Servers servlets directory and register the servlet. Now open your browser and run the servlet.
Your browser should display the data from database.
N ow we will examine how the code works.
import java.io.*;
import java.util.Enumeration;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.net.*;
public class emaildb 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("Emai List.");
out.println("");
out.println("");
out.println("

List of E-mail addresses.

");
out.println("

");
out.println("");
out.println("");
out.println("");
out.println("");
out.println("");
try{
//Loading Sun's JDBC ODBC Driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//Connect to emaildb Data source
theConnection = DriverManager.getConnection("jdbc:odbc:emaildb", "admin", "");
Statement theStatement=theConnection.createStatement();
ResultSet theResult=theStatement.executeQuery("select * from emaillists"); //Select all records from emaillists table.
//Fetch all the records and print in table
while(theResult.next()){
out.println();
out.println("");
out.println("");
out.println("");
String s=theResult.getString(3);
out.println("");
out.println("");
}
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("
NameE-mailWebsite
" + theResult.getString(1) + "" + theResult.getString(2) + "" + s + "

");
out.println("

 

");
}
public void destroy(){
}
}
Here we have used Sun's JDBC-ODBC Bridge, but we can easily replace the driver in order to connect the servlet to another database. For example, we can use the following code to connect our program to MySQL database.
Class.forName("org.gjt.mm.mysql.Driver");
theConnection = DriverManager.getConnection("jdbc:mysql://192.192.10.1:3306/mysql", "mysql","mysql");
JDBC driver for MySQL is available at www.mysql.com and is free. Here I have assumed that your mysql server is running at port 3306 of computer, whose IP address is 192.192.10.1 .
------


 

Rate Tutorial:
http://www.roseindia.net/jdbc/jdbcconnectivity.shtml

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
Accessing Database from servlets through JDBC!

View Tutorial:
Accessing Database from servlets through JDBC!

Related Tutorials:

Displaying 1 - 0 of about 0 Related Tutorials.

Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.