Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

connecting to Data Base

 
arunas rao
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to connect to a MySQL database from a servlet using tomcat server?
what is the driver to be used?
 
Yu Tao
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Naren Chivukula
Ranch Hand
Posts: 577
Java Notepad Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic