Switch to full style
What's behind JSP & Servlets
Post a reply

servlet coding

Fri Apr 08, 2011 4:08 am

plz send the complete steps for making a connection between tomcat 6.0 and Ms access. I am trying to develop a
web application.



Re: servlet coding

Fri Apr 08, 2011 1:51 pm

hi
First you will need to open a connection to your MS access database using the JDBC driver and you will need to add the JDBC needed jars to your tomcat web project libs .

Here a code to open a connection to msaccess db from java code :
Code:

import java
.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class 
MSAccessDAO {


    private 
String username;
    private 
String password;
    private 
String driver;
    private 
String URL =
            
"jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:\\MSDB\\rundb.mdb;}";

    public 
MSAccessDAO(String usernameString passwordString driver) {
        
this.username username;
        
this.password password;
        if (
driver == null || driver.trim() == "") {
            
this.driver "sun.jdbc.odbc.JdbcOdbcDriver";
        }

    }

    public 
Connection getConnection() {
        try {

            Class.
forName(driver);

            
Connection connection DriverManager.getConnection(URLusernamepassword);
            return 
connection;
        } catch (
SQLException e) {
            
e.printStackTrace();
        } catch (
ClassNotFoundException ex) {
            
ex.printStackTrace();
        }
        return 
null;

    }

    public static 
void main(String[] argsthrows Exception {
        
Connection connection null;
        try {
               
MSAccessDAO accessConnect = new MSAccessDAO("admin","admin"null);
               
connection =accessConnect.getConnection();
               if(
connection!=null){
                   
// Do your work .
               
}else {
                   
System.out.println("Failed to create connection to DB");
               }


        }
finally {
            
connection.close();
        }
    }

    public 
String getURL() {
        return 
URL;
    }

    public 
void setURL(String URL) {
        
this.URL URL;
    }

    public 
String getDriver() {
        return 
driver;
    }

    public 
void setDriver(String driver) {
        
this.driver driver;
    }

    public 
String getPassword() {
        return 
password;
    }

    public 
void setPassword(String password) {
        
this.password password;
    }

    public 
String getUsername() {
        return 
username;
    }

    public 
void setUsername(String username) {
        
this.username username;
    }


}
 


Post a reply
  Related Posts  to : servlet coding
 how to run servlet and rmi     -  
 JSP and Servlet     -  
 jsp and servlet     -  
 Servlet & JSP     -  
 Java Coding     -  
 I am new in PHP, want coding or PHP file     -  
 Java Servlet and JSP     -  
 SERVLET/JSP DOUBT     -  
 Servlet Help for my project!!     -  
 Standard Huffman Coding     -