This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Servlets and the fly likes connecting to Data Base Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "connecting to Data Base" Watch "connecting to Data Base" New topic

connecting to Data Base

arunas rao

Joined: Jun 28, 2005
Posts: 5
How to connect to a MySQL database from a servlet using tomcat server?
what is the driver to be used?
Yu Tao

Joined: Dec 12, 2004
Posts: 28
do you know that JSP how to connection servlet???

if your know

i will tell you serlvet how connection database
[ June 29, 2005: Message edited by: Yu Tao ]
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Originally posted by arunas rao:
How to connect to a MySQL database from a servlet using tomcat server?
what is the driver to be used?

With a JDBC driver for MySQL. Here is one.

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Naren Chivukula
Ranch Hand

Joined: Feb 03, 2004
Posts: 577

Hi arunas,

As I'm working on the same thing. Here is the readymade code for you.

package classes.db2db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class ReadDBClient {
Connection connection=null;
Statement statement=null;
PreparedStatement preparedStatement=null;
String url=null;
String userId=null;
String password=null;
ResultSet resultSet=null;
ArrayList result=null;
public ReadDBClient(){

/*Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
url="jdbc dbc:<datasource-name>";*/
/* Use above two statements if u use JDBC-ODBC bridge driver and where your datasource is configured in the system*/

Class.forName ("com.mysql.jdbc.Driver");
url = "jdbc:mysql://localhost/<databasename>";
/* Use above two statements if you got ur MySql driver */
connection=DriverManager.getConnection(url, userId, password);
System.out.println ("Database connection with "+url+" is established");
}catch(Exception exp){

My sincere suggestion is not to use above code unless the application is very small. Configure Data Source in your App server and make use of that server to make database transactions. Below is the code in which data source is already configured in JBoss server. Refer mysql-ds.xml for Data source configuration. To run the program you App Sever should be running

import java.util.Properties;

import javax.naming.InitialContext;
import javax.naming.Context;
import javax.sql.DataSource;
import javax.rmi.PortableRemoteObject;
import java.sql.Connection;
import java.sql.SQLException;

public class UsingDS {

Properties props=null;
Context context;
Object obj=null;
DataSource dataSource=null;
String jndiName=null;
Connection connection=null;
public UsingDS(){
props.put (Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
props.put (Context.PROVIDER_URL, "jnp://localhost:1099/");
props.put("naming.factory.url.pkgs","java.naming.factory.url.pkgs=org.jboss.naming rg.jnp.interfaces");

context=new InitialContext(props);
obj = PortableRemoteObject.narrow(context.lookup(jndiName), DataSource.class);
System.out.println ("Database connection is established");
}catch(Exception exp){
public static void main(String[] args){
UsingDS db=new UsingDS();


(OCEEJBD6, SCWCD5, SCDJWS, SCJP1.4 and Oracle SQL 1Z0-051)
I agree. Here's the link:
subject: connecting to Data Base
It's not a secret anymore!