aspose file tools*
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
JavaRanch » Java Forums » Java » Servlets
Bookmark "connecting to Data Base" Watch "connecting to Data Base" New topic
Author

connecting to Data Base

arunas rao
Greenhorn

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
Greenhorn

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
Bartender

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: 576

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(){
try{

/*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 */
userId="flowbrix30";
password="flowbrix30";
connection=DriverManager.getConnection(url, userId, password);
statement=connection.createStatement();
System.out.println ("Database connection with "+url+" is established");
}catch(Exception exp){
exp.printStackTrace();
}
}
}

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(){
try{
props=System.getProperties();
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);
jndiName="java:/<jndi-nam>";
obj = PortableRemoteObject.narrow(context.lookup(jndiName), DataSource.class);
dataSource=(DataSource)obj;
connection=dataSource.getConnection();
System.out.println ("Database connection is established");
}catch(Exception exp){
exp.printStackTrace();
}
}
public static void main(String[] args){
UsingDS db=new UsingDS();
}
}

Regards,
Narendranath


Cheers,
Naren
(OCEEJBD6, SCWCD5, SCDJWS, SCJP1.4 and Oracle SQL 1Z0-051)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: connecting to Data Base
 
Similar Threads
Driver Loading
JDBC database connections in servlets
Connection Pooling Managment
Setting Connection Time Out
how to make my application run faster