Monday, August 13, 2012

How to connect Java Application with Oracle Database using JDBC Driver



How to connect Java Application with Oracle Database using JDBC Driver -

Today I will go through the basic steps to connect a Java Application with Oracle Database using JDBC Driver (Java Database Connectivity). To develop Enterprise Product connectivity of java application and oracle database is essential. When I was new to java, i have faced some problem for java and database connectivity using jdbc. So I want to make all this process bit easier for all the new comer in java and windows platform.





Prerequisites :

    Microsoft Windows Xp Service Pack 2 or above.
    Java Developement Kit.
    Oracle Database.
    Oracle JDBC Driver.According to your database version.
    One Integrated Development Environment.like Netbeans,Eclipse

Steps for configuring the system and creating DSN (ODBC Data Source):

    1.Install Java Development Kit and Oracle Database.
    2.Place Oracle JDBC Driver (*.jar file) in java runtime folder and java development kit folder . eg: 'C:\Program Files\Java\jre1.6.0\lib\ext' and 'C:\Program Files\Java\jdk1.6.0\jre\lib\ext'.
   3. Create a Data Source Name (DSN) required for database connectivity in windows.
    For DSN go to Start->Control Panel->Administrative Tools->Data Sources (ODBC).
    4.ODBC Data Source Administrator will open. Click on the Add button to create a new data source.
    





5.Select 'Microsoft ODBC for Oracle' & click Next.



 7.Fill the Data Source Name - name for the data source, Description - Just a simple description, User Name - Oracle Database User name, Server - name of the server where database is installed ,( localhost - if it is installed in the local machine or the IP if the the database is installed in a remote machine). Click OK and close the ODBC Data Source Administrator.







Now, the system is configured to run any java oracle connectivity program.You can start coding the program in any IDE or open a notpad.

Below is the source code of a jdbc program to determine wheather all your setting are OK or not. Compile the program and execute it. If the program is executed without any error or exception and you get 'JDBC Driver loaded' and 'Connected to the Oracle Database' as output then you have done all the setting correctly and you are able to connect to the oracle database through your java program.
If you encounter any problem please get back to me, I will help you to sought out the problem.


Source code :

import java.sql.*;

public class ConnectOracle {

public static void main(String[] args) {
String driver="sun.jdbc.odbc.JdbcOdbcDriver"; //
String cs="jdbc:odbc:connectOracle"; //connectOracle is the data source name
String user = "system"; //username of oracle database
String pwd = "tom"; //password of oracle database
Connection con = null; //connection variable assigned to null
try
{
Class.forName(driver);// for loading the jdbc driver
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println("JDBC Driver loaded");
try
{
con=DriverManager.getConnection(cs,user,pwd);// for establishing connection with database
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println("Connected to the Oracle Database");
try
{
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}//end of main()
}//end of class()

ODBC Driver for Access 2000, 2002, 2003 and 2007 Databases

The Easysoft ODBC-Access Driver connects 32-bit and 64-bit applications on Linux and Unix to Microsoft Access databases.

To ensure compatibility with databases created in as many versions of Access as possible, the Easysoft ODBC-Access Driver supports both the MDB and ACCDB database formats. The Easysoft ODBC-Access Driver can connect to .mdb databases created in Access 2000–2003 and Office Access 2007. The forthcoming release of Access, Office Access 2010, will continue to support .mdb format databases. The Easysoft ODBC-Access Driver can also connect to .accdb databases, which is the default format for databases created in Office Access 2007 and later. The Easysoft ODBC-Access Driver is a flexible, future-proof solution therefore, which will integrate Linux/Unix with whatever version of Access suits your organisation best.

The Easysoft ODBC-Access Driver provides read-write access to Access databases, so your Linux/Unix applications can update as well as retrieve Access data. If you need to restrict users to read-only access, you can configure the driver to open the database in read-only mode, just as you can with the Access front-end application on Windows.

Choosing the Easysoft ODBC-Access Driver to connect Linux/Unix with Access will not disrupt your existing Access solution’s environment or users. There is no software to install on your Windows machines. You do not need to change where your database file is located. The Easysoft ODBC-Access Driver can open an Access database stored on a Windows share, so there is no need to copy the database file to your Linux/Unix machine. To ensure concurrent access to the database by Windows and Linux/Unix users is problem free, the Easysoft ODBC-Access Driver uses Access’ locking file mechanism. The driver will also prevent its users from opening a database if another user has the database opened for exclusive access, again mirroring the Access front-end’s behaviour for seamless integration.

The Easysoft ODBC-Access Driver supports encrypted/encoded .mdb files, which is an Access security feature that protects Access data from unauthorised viewing. When you use the Easysoft ODBC-Access Driver to load a remote .mdb file, encrypting/encoding the database protects the privacy of the Access data as it is sent across the network.

To ensure compatibility with multiple Linux/Unix applications, the Easysoft ODBC-Access Driver supports the unixODBC driver manager. Most (if not all) Linux/Unix applications support unixODBC, which, as a mature, non-proprietary, robust driver manager, is part of the standard installation of many Linux distributions. To provide a self-contained solution, the Easysoft ODBC-Access Driver distribution itself includes the unixODBC driver manager. To get you going quickly, the Easysoft ODBC-Access Driver automatically installs itself into unixODBC, making the driver immediately available to your applications.
Platforms

The Easysoft ODBC-Access Driver is currently available on these platforms:
Version     Platform     Distribution
v1.0 (Access 2000 - 2007)     AIX (PPC) (32 - Bit)     (4.3-6.1)
v1.0 (Access 2000 - 2007)     AIX (PPC) (64 - Bit)     (5.0-6.1)
v1.0 (Access 2000 - 2007)     HP-UX (Itanium i64) (32 - Bit)     (11i)
v1.0 (Access 2000 - 2007)     HP-UX (Itanium i64) (64 - Bit)     (11i)
v1.0 (Access 2000 - 2007)     HP-UX (PA-Risc) (32 - Bit)     (10.10-11)
v1.0 (Access 2000 - 2007)     HP-UX (PA-Risc 2) (64 - Bit)     (11)
v1.0 (Access 2000 - 2007)     Linux (x86) (32 - Bit)     (kernel 2.2-2.6, glibc 2.1+)
v1.0 (Access 2000 - 2007)     Linux (x86) (64 - Bit)     (kernel 2.6, glibc 2.3.5)
v1.0 (Access 2000 - 2007)     Solaris (Sparc) (32 - Bit)     (2.6-2.10)
v1.0 (Access 2000 - 2007)     Solaris (Sparc) (64 - Bit)     (2.8-2.10)
v1.0 (Access 2000 - 2007)     Solaris (x86) (32 - Bit)     (2.8)
v1.0 (Access 2000 - 2007)     Solaris (x86) (64 - Bit)     (2.8)

If the Access ODBC driver is not available for your platform, we have an alternative Access solution, which is available for additional Unix platforms.

Download Access ODBC driver.
MSAccess Linux ODBC Driver

Connects to MS Access databases (.mdb or .accdb) from 32-bit and 64-bit Linux platforms such as CentOS, Debian GNU/Linux, Fedora, Kubuntu/Ubuntu (Edgy Eft/Feisty Fawn/Gutsy Gibbon/Hardy Heron/Intrepid Ibex/Jaunty Jackalope/Karmic Koala), Mandrake/Mandriva, OpenSUSE/SUSE, Red Hat, Slackware and more.
MSAccess Unix ODBC Driver

Connects to MS Access databases (.mdb or .accdb) from 32-bit and 64-bit IBM AIX, HP-UX and Sun Solaris platforms.

MS Access JDBC Driver -- Connecting MS Access with Java



To connect Java with MS Access, you need a JDBC driver. Although Microsoft do not produce a JDBC driver for MS Access, Easysoft provide two Microsoft Access JDBC drivers. Use these JDBC drivers to provide the connectivity layer between your Java code and MS Access database.

    Download Type 2 MS Access JDBC driver for Windows

    The Easysoft JDBC-Access Gateway connects Java running on Windows to local MS Access databases. For more information, see the JDBC-Access Gateway Getting Started Guide.
    Download Type 3 MS Access JDBC driver for Linux and Unix

    The JDBC-ODBC Bridge connects Java running on Linux or Unix to remote MS Access databases. For more information, see this tutorial.

Accessing MS Access from Java

This tutorial shows how to access MS Access databases from Java.

Java -> JOB Client -> TCP/IP -> JOB Server -> Microsoft Driver Manager -> Microsoft Access ODBC Driver -> Microsoft Access
Contents

    Prerequisites
    Assumptions
    Configuring the Microsoft Access ODBC Data Source
    Running the Demo Applet
    Running the Demo Application
    Common Problems

Prerequisites

    An installed and licensed Easysoft JDBC-ODBC Bridge (JOB) server on a supported Windows platform that has Microsoft Office installed.

    If you have not yet installed the JOB software or you are having problems with the installation, use our Getting Started Guide to help you through the installation.
    An existing Access database file (.mdb) on the Windows machine.

Assumptions

    You accepted the default options during the install.
    This tutorial was written assuming the demonstration clients were installed on the same Windows machine as the JOB server.

    You can check to see if you have these installed by choosing Start > Programs > Easysoft > JDBC-ODBC Bridge.

    From here you should see both an Applet demo and an Application demo.
    We have also assumed that you have installed the JOB software as a valid Windows User who has access to the target database.

Configuring the Microsoft Access ODBC Data Source

You will find the ODBC Administrator within Administrative Tools from your Control Panel.

ODBC Data Source Administrator System DSN tab.

From here you will create your new System DSN. Click the Add button and then select the Microsoft Access Driver and click Finish.

ODBC Microsoft Access Setup dialog box.

Give your DSN a meaningful name and click Select. Here you will be able to browse to your existing .mdb file.

Once you have selected the database you want to access, click OK and then OK again to exit the dialog box. You have now created your data source.
Running the Demo Applet

Within your services manager, make sure that the JOB service is running, and then open a web browser and go to the following URL http://localhost:8031.

This will open the JOB Web Administrator where you have many configuration options. On the left hand side, you will see a Test Applet link. This will show you a box with various parameters. The Logon User and Password should be the same as your valid Windows details.

Once you have entered your username and password, click the Connect button. This will then display a pop-up box:

Easysoft JDBC-ODBC Bridge Browse Connect dialog box.

As you can see from the image, we have created a system DSN that points to northwind.mdb called NORTHWIND. (There is a Northwind database included in most versions of Microsoft Office.) If you have more than one system DSN, your newly created DSN will appear at the bottom of the drop down list.

From here, simply select the DSN you wish to connect to and click Connect. (You will not need to add a database username and password as the Access database will use NT authentication.)

Once connected, you will see a list of tables. You can issue a simple select * from table by double clicking the table you wish to query. Alternatively, you can issue your own SQL in the SQL field.

This will return the result set in an applet window:

Result set displayed in an applet window.

Download Windows JDBC Driver for MS Access. Free Trial. Download UNIX JDBC Driver for MS Access. Free Trial.
Running the Demo Application

To run the Demo Application, choose Start > Programs > Easysoft > JDBC-ODBC Bridge > AWT Application Demo.

This should now display the demo application:

Easysoft JDBC-ODBC Bridge demo application.

You can see that the connection URL already has the first part of the connection string in it -- jdbc:easysoft:

The complete connection string should take the form of:

 jdbc:easysoft://servername/datasource:logonuser=username:logonpassword=password

From here, you do not need to add any further details. Simply click Connect.

From here, you will be able to see a list of tables from your Access database. The same applies to the SQL commands as it does in the Demo Applet section. Simply issue your SQL command and click Submit.
Common Problems
Why can’t I connect to my Microsoft Access .mdb file using the Easysoft JDBC-ODBC Bridge?

The problem is likely to be that the Microsoft Access .mdb file is on a mapped network drive that can only be accessed by a named user.

There are three possible solutions:

    Move the .mdb file to a drive that can be accessed by a service.
    Run the JOB service as the user who has access to the drive/file.
    Add the following attributes to the JDBC connection URL:

    logonuser=username:logonpassword:password

Why do I get error "[JOB] Connection failed[JOB] RPC Exception: access denied (java.net.SocketPermission ip address/server name:8031 connect,resolve)" when trying to connect in the test applet using a fully qualified JDBC URL?

If you have run the test applet from the Start menu, the browser will default to 127.0.0.1 (localhost). This will then be the only IP address/server name you will be able to use in your JDBC URL. You will not be able to use the server’s actual IP address/server name unless you have used either of them in your initial browser address (http://ip address/server name:8031).
Why do I get error "RPC Exception: access denied java.net.SocketPermission" in the JOB applet?

Check that your JDBC connection URL is connecting to the same machine as your browser. For example, http://127.0.0.1:8031 is not the same as the machine name / IP address, so in your connection URL it must match:

 jdbc:easysoft://servername/datasource:logonuser=username:logonpassword=password

Why do I get the error "Client denied access due to access control rule"?

The machine you are running the JOB JDBC driver on has attempted to connect to the specified server (where the JOB Server is running) but there is an access control rule on the server that denies your client access. You need to use the JOB web administration interface to examine the access control rules. Point a web browser at:

http://myserver:8031

Then select Security. The access control rules are defined on this page.

JDBC-Access Gateway


How to connect to Microsoft Access or Microsoft Excel from Java.



Introduction



JDBC is a Java application programming interface (API) that connects Java to relational databases (and other tabular data, such as spreadsheets and flat files). To interact with a database, a Java application uses a JDBC driver. A JDBC driver implements the JDBC API for a particular database.

Microsoft do not produce a JDBC driver for Jet, the underlying database engine for the Microsoft Office product suite. However, Microsoft do produce a Jet-based ODBC driver. (ODBC is another data access technology, the Microsoft implementation of which is included with Windows.) To provide a JDBC interface to Office applications via this native Microsoft interface, a JDBC driver must be able to convert JDBC calls to ODBC calls. As far as the Java application is concerned, it is using a normal JDBC driver. As far as the Office application is concerned, it is being accessed via the normal ODBC driver.

The Easysoft JDBC-Access Gateway is a JDBC driver for Access that uses the Java Native Interface (JNI) to communicate with the Access ODBC driver library. Because this ODBC driver library also supports Excel, the Easysoft JDBC-Access Gateway also provides JDBC access to Excel.

You can use the Easysoft JDBC-Access Gateway to connect Java applications, servlet/JavaServer Pages (JSP) engines, integrated development environments (IDE) and application servers with Access and Excel.

The Easysoft JDBC-Access Gateway has both a Java component and a native Windows component, which is used to communicate with the Microsoft ODBC driver. By default, Java applets cannot load native code libraries; the Java security mechanism prevents this because it cannot police what happens at native code level. To use the Easysoft JDBC-Access Gateway with a Java applet, your Java security policy needs to allow the applet to load native libraries. For example:


/* Sample user .java.policy file */
grant codeBase "http://my_webserver/my_jdbc_applet_dir/-" {
  permission java.lang.RuntimePermission "loadLibrary.*";
};

Note that using applets with the Easysoft JDBC-Access Gateway is not the only way to publish your Access or Excel data on the Web or manipulate the data from within a Web browser. As mentioned, the Easysoft JDBC-Access Gateway is compatible with servlet/JSP engines (such as Apache Tomcat) and application servers (such as Adobe ColdFusion), and these products allow Web-based applications with database backends to be created.
Supported Microsoft File Formats

The native library for the Easysoft JDBC-Access Gateway is odbcjt32.dll, which is Microsoft’s ODBC driver for Access and Excel (also known as the Microsoft ODBC Desktop Database Drivers).

odbcjt32.dll supports the following Access database file formats:

    Version 4.0 .mdb files, which is the default format for databases created in Access 2000, 2002 and 2003.
    Version 3.0 .mdb files, which is the default format for databases created in Access 95 and Access 97.
    Version 2.0 .mdb files, which is the default format for databases created in Access 2.0.

Although Office Access 2007 introduced a new database file format (.accdb), version 4.0 .mdb files continue to be supp
orted by Access 2007 and later.

In addition, odbcjt32.dll supports .xls format workbooks created in:

    Excel 97–2003
    Excel 5.0/95
    Excel 4.0
    Excel 3.0

Although Office Excel 2007 introduced new spreadsheet file formats (.xlsb, .xlsm, .xlsx), .xls files continue to be supported by Excel 2007 and later.
Before You Begin
What You Need To Know

Obtain this information from your system administrator:

    The path to the target Access database (.mdb) or Excel workbook (.xls) on the machine you intend to install the Easysoft JDBC-Access Gateway on.

    The target file must be visible through the local file system on the Easysoft JDBC-Access Gateway machine. For example, in a folder on this machine or a mapped network drive or in a shared folder.

Prerequisite Software
Java Runtime Environment Requirements

You need a Java Runtime Environment (JRE) installed on the Windows machine you want to install the Easysoft JDBC-Access Gateway on. The JRE contains the Java virtual machine (JVM), runtime class libraries, and Java application launcher that are necessary to run programs written in the Java programming language. The Easysoft JDBC-Access Gateway is compatible with JRE 1.6.0 and later.

To check whether you have the JRE installed on your machine and that your JRE version is one the Easysoft JDBC-Access Gateway supports, open a Command Prompt window, and type java -version. For example:

c:\>java -version
java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03)
Java HotSpot(TM) Client VM (build 20.1-b02, mixed mode, sharing)

If the reported JRE version is 1.5.n or earlier (or you get the error "'java' is not recognized as an internal or external command, operable program or batch file."), you need to obtain a JRE for your machine.

If the output produced by running java -version contains 64-Bit Server VM, you have a 64-bit JRE. The Easysoft JDBC-Access Gateway requires a 32-bit JRE; a 32-bit JRE (x86) will install and function correctly on a 64-bit machine. (The Easysoft JDBC-Access Gateway uses the ODBC Desktop Database Drivers, which is a 32-bit library. There is no 64-bit version of this library. The native component of the Easysoft JDBC-Access Gateway and the JRE must also be 32-bit as you cannot mix 32-bit and 64-bit libraries.)

Windows versions of the JRE are available to download from:

    http://www.oracle.com/technetwork/java/javase/downloads/index.html

Note The Java Development Kit (JDK), which includes the JRE, is also available to download from this web page. However, unless you are going to develop a Java application to run against the Easysoft JDBC-Access Gateway, you only need to download the JRE package.
ODBC Desktop Database Drivers

The ODBC Desktop Database Drivers must be installed on the machine on which you install the Easysoft JDBC-Access Gateway. Note that you do not need to install Access or Excel on the Easysoft JDBC-Access Gateway machine.

Since Windows 2000, the ODBC Desktop Database Drivers (one of the Jet Database Engine Components) have shipped with Windows and so should be already present on your machine as part of the Windows installation.

To check whether the ODBC Desktop Database Drivers are installed, use ODBC Data Source Administrator:

    Do one of the following:
        On 32-bit Windows, in the Windows Run dialog box, type:

        %windir%\system32\odbcad32.exe

        On 64-bit Windows, in the Windows Run dialog box, type:

        %windir%\syswow64\odbcad32.exe

        Note On 64-bit Windows, the ODBC Data Source Administrator that is accessible from Control Panel is a 64-bit application. The 64-bit ODBC Data Source Administrator only lists 64-bit ODBC drivers, and so cannot be used to check whether the ODBC Desktop Database Drivers are installed.
    In ODBC Data Source Administrator, click the Drivers tab.
    In the File column, look for the file name ODBCJT32.DLL.

    If ODBCJT32.DLL is listed, the ODBC Desktop Database Drivers are installed, and you have the prerequisite software for the Easysoft JDBC-Access Gateway. Otherwise, you need to ask your system administrator to install the Microsoft driver before you can use the Easysoft JDBC-Access Gateway.

Installing the Easysoft JDBC-Access Gateway

    Log into the Easysoft website.

    If you have not yet done so, you need to register first. On the registration form, an asterisk (*) indicates that a field is mandatory.
    Download the Easysoft JDBC-Access Gateway distribution.
    Save the distribution file to a temporary directory on the machine where you intend to install the Easysoft JDBC-Access Gateway.
    Execute the file distribution that you downloaded in the previous step.

    Follow the on screen instructions.
    Obtain a free trial licence:
        In Easysoft Data Access License Manager, complete the contact information and click Request License.

        The email contact address you supply must be the email address you registered with on the Easysoft web site.
        Choose Time Limited Trial and click Next.
        Choose Easysoft JDBC-Access Gateway from the drop-down list of products and click Next.
        Click On-Line Request. You will get a message telling you that your license has been added. Click OK.
        Click Finish.

Using the Easysoft JDBC-Access Gateway
Setting the Class Path

The Java class library file for the Easysoft JDBC-Access Gateway is named esmdb.jar. As esmdb.jar is not part of the Java platform, the class path must include the esmdb.jar file. Otherwise, your application will throw a ClassNotFoundException exception when trying to use the Easysoft JDBC-Access Gateway. The class path is a parameter that tells the JVM and Java programs where to find to find third-party and user-defined classes.

esmdb.jar is installed in the following location:

easysoft_installation_folder\Libs

The default location for easysoft_installation_folder is drive:\Program Files\Easysoft Limited\Easysoft JDBC-Access Gateway.

What class path configuration is required depends on your application:
Applications that are Run at the Command Prompt

The CLASSPATH environment variable is used, which can be set on a system, user or session basis. For example, this command sets the class path for Java applications run in a particular command prompt session:

C:\MyJavaApps>set CLASSPATH="%CLASSPATH%;C:\Program Files\Easysoft Limited\Easysoft JDBC-Access Gateway\Libs\esmdb.jar"

Alternatively, you can specify the class path on the Java command line that runs an application by using the java -classpath option.

C:\MyJavaApps> java -cp ".;C:\Program Files\Easysoft Limited\Easysoft JDBC-Access Gateway\Libs\esmdb.jar" MyJavaApp

The following steps show how to set the class path when compiling and running the sample Easysoft JDBC-Access Gateway Java application. (Note that the procedure assumes that you have the JDK installed.)

    Save the sample Java code to a file named ConnectToAccess.java.
    In a Command Prompt, cd to the directory where you saved ConnectToAccess.java.
    Set the class path for the Java compiler and application launcher:

    C:\MyJavaApps>set CLASSPATH="%CLASSPATH%;C:\Program Files\Easysoft Limited\Easysoft JDBC-Access Gateway\Libs\esmdb.jar"
    Compile and run the sample application. For example:

    C:\MyJavaApps> "C:\Program Files\Java\jdk1.6.0_26\bin\javac" ConnectToAccess.java

    C:\MyJavaApps> java ConnectToAccess

    JDBC Driver Information
         Driver name: easysoft.sql.esMdbDriver
         Driver Major Version: 1
         Driver Minor Version: 0
         .
         .
         .

Applications that Run in an IDE

Each IDE has a different method for setting its class path. Setting the CLASSPATH environment variable will not work. You need to add the esmdb.jar folder to the IDE class path.
See Also

    Using the Easysoft JDBC-Access Gateway with Eclipse IDE for Java Developers
    Using the Easysoft JDBC-Access Gateway with NetBeans IDE
    Using the Easysoft JDBC-Access Gateway with Embarcadero JBuilder

Servlets and JSPs

Servlets and JSPs are run in a servlet/JSP engine such as Apache Tomcat. The class path must be set according to the servlet/JSP engine documentation. Setting the CLASSPATH environment variable will not work. You may have to set the engine class path through a GUI or configuration file. Alternatively, you may have to copy esmdb.jar to a particular folder such as lib.
See Also

    Using the Easysoft JDBC-Access Gateway with Apache Tomcat

Connecting to your Access Database or Excel Workbook

The Easysoft JDBC-Access Gateway provides two JDBC drivers: a driver for Access databases and a driver for Excel workbooks.

The Java class for the Access JDBC driver is easysoft.sql.esMdbDriver. The Java class for the Excel JDBC driver is easysoft.sql.esXlsDriver.

To register a Easysoft JDBC-Access Gateway JDBC driver, your Java application must specify the appropriate class. For example:

// Register the Access JDBC driver.
Class.forName("easysoft.sql.esMdbDriver");

–Or–

// Register the Excel JDBC driver.
Class.forName("easysoft.sql.esXlsDriver");

When the relevant JDBC driver is registered, you can establish a connection by using a connection URL and the getConnection method of the DriverManager class. For example:

String connectionUrl = "jdbc:easysoft:mdb" +
   "?DBQ=C:/Users/Public/Northwind.mdb";
Connection con = DriverManager.getConnection(connectionUrl);

To establish a connection with the Easysoft JDBC-Access Gateway, use a connection URL of the form:

jdbc:easysoft:?DBQ=[;=]

where:

    is either mdb for the Access JDBC driver or xls for the Excel JDBC driver.
    is the path to the Access database (.mdb) or Excel workbook (.xls).
    is an Access ODBC driver or an Excel ODBC driver attribute.

Example Connection URLs

Opens an Access database for read-only access:

jdbc:easysoft:mdb?DBQ=C:/Users/Public/Northwind.mdb;ReadOnly=True

Opens an an Access database that is stored in a shared folder:

jdbc:easysoft:mdb?DBQ=//mymachine/myshare/Sales.mdb;

Opens a password-protected Excel workbook from a mapped network drive:

jdbc:easysoft:xls?DBQ=Z:/Orgdata.xls;PWD=p455w0rd

Further Support

    To contact the Easysoft support team, send an email message to support@easysoft.com.

Appendices
Easysoft JDBC-Access Gateway Demonstrators

This sample Java application connects to an Access database and returns a list of tables in the database. The application also prints out information about the database and the Easysoft JDBC-Access Gateway.

import java.sql.*;
import java.util.Properties;

public class ConnectToAccess {

   public static void main(String[] args) {

      // Replace the DBQ value with the path to your Access database.
      String connectionUrl = "jdbc:easysoft:mdb?" +
         "DBQ=C:/Users/Public/Northwind.mdb";

      Driver driver = null;
      DriverPropertyInfo props[] = null;
      Connection con = null;
      DatabaseMetaData dm = null;
      ResultSet rs = null;

      try {
         // Register the Easysoft JDBC-Access Gateway.
         Class.forName("easysoft.sql.esMdbDriver");
         driver = DriverManager.getDriver(connectionUrl);

         System.out.println("JDBC Driver Information");
         System.out.println ("\tDriver name: " +
                     driver.getClass().getName());
         System.out.println("\tDriver Major Version: " +
                 driver.getMajorVersion());
         System.out.println("\tDriver Minor Version: " +
                 driver.getMinorVersion());

         con = DriverManager.getConnection(connectionUrl);

         dm = con.getMetaData();

         System.out.println("\tJDBC Major Version: " +
                 dm.getJDBCMajorVersion());
         System.out.println("\tJDBC Minor Version: " +
                 dm.getJDBCMinorVersion());

         props = driver.getPropertyInfo (connectionUrl, new Properties());

         /* These attributes are Microsoft Access ODBC driver attributes,
            which the Easysoft JDBC-Access Gateway passes to the
            Microsoft driver. Only the DBQ attribute is mandatory. */
         System.out.println ("JDBC URL Attributes");

         for (int i = 0; i < props.length; i++) {
             System.out.print ("\t" + props[i].name);
             System.out.print (" = ");
             System.out.print (props[i].value);
             System.out.print (" : ");
             System.out.println (props[i].description +".");
         }

         System.out.println("Database Information");
         System.out.println("\tDatabase Name: " +
                     dm.getDatabaseProductName());
         System.out.println("\tDatabase Version: " +
                 dm.getDatabaseProductVersion());

         rs = dm.getTables(null, null, "%", null);

         System.out.println("Database Tables");

         // Retrieve type and name for each table in the database.
         while (rs.next()) {
             System.out.println("\t" + rs.getString(3) +
                     " : " + rs.getString(4));
         }
      }

      // Handle any errors that may have occurred.
      catch (Exception e) {
          e.printStackTrace();
      }
      finally {
          if (rs != null) try { rs.close(); } catch(Exception e) {}
          if (con != null) try { con.close(); } catch(Exception e) {}
      }
   }
}

This sample JSP connects to and retrieves data from a JDBC data source that has been configured as a Java Naming and Directory Interface (JNDI) resource named Northwind.


 
   Sample Easysoft JDBC-Access Gateway JSP

 
 
   <%@ page import="javax.naming.*" %>
   <%@ page import="java.sql.*" %>
   <%@ page import="javax.sql.*" %>
   <%@ page import="java.io.PrintWriter" %>

   <%@page contentType="text/html;charset=UTF-8"%>

  

Sample Easysoft JDBC-Access Gateway JSP



   <%

   Connection con = null;
   Statement stmt = null;
   ResultSet rs = null;

   try {

       // Obtain our environment naming context
       Context initCtx = new InitialContext();
       Context envCtx = (Context) initCtx.lookup("java:comp/env");

       // Look up our data source
       DataSource ds = (DataSource) envCtx.lookup("jdbc/Northwind");

       // Allocate and use a connection from the pool
       con = ds.getConnection();

       // Fetch and display data
       stmt = con.createStatement();

       // You need to edit this query
       rs = stmt.executeQuery("SELECT CompanyName FROM suppliers");

       while (rs.next()) {
           // You need to edit this column name
           String s = rs.getString("CompanyName");
           out.print(s + "
");
       }

       rs.close();
       rs = null;
       stmt.close();
       stmt = null;
       con.close(); // Return to connection pool
       con = null;  // Make sure we do not close it twice
   } catch (SQLException e) {
       out.print("Throw e" + e);
   } finally {
     // Always make sure result sets and statements are closed,
     // and the connection is returned to the pool
     if (rs != null) {
       try { rs.close(); } catch (SQLException e) { ; }
       rs = null;
     }
     if (stmt != null) {
       try { stmt.close(); } catch (SQLException e) { ; }
       stmt = null;
     }
     if (con != null) {
       try { con.close(); } catch (SQLException e) { ; }
       con = null;
     }
   }

   %>

 

How to Connect Java to Microsoft Access

Connect Java to Microsoft Access

Easysoft provide two ways to connect Java & MS Access databases:

    The JDBC-Access Gateway, a Type 2 JDBC driver for MS Access. The JDBC-Access Gateway has both a Java component and a native Windows component, which is used to communicate with the Access database file via the MS Access ODBC driver.
    The JDBC-ODBC Bridge, a Type 3 JDBC driver whose client/server architecture allows the Access database file and Java to be on separate machines. The JDBC-ODBC Bridge client is written entirely in Java and does not need any installation — it can be served remotely with a Java applet or copied to a directory where your Java application, servlet/JSP engine, IDE or application server can load JAR files from.

Both the JDBC-Access Gateway and JDBC-ODBC Bridge use the MS Access ODBC driver to access the database file. Using the database vendor’s driver to communicate with the database file may be required to comply with IT company policy at your site.
 



(गेटवे JDBC एक्सेस, टाइप 2 एमएस एक्सेस के लिए JDBC ड्राइवर.JDBC - प्रवेश गेटवे दोनों एक java घटक और एक देशी Windows घटक है, जो ms access  ODBC driver के माध्यम से Access database फ़ाइल के साथ संवाद करने के लिए प्रयोग किया जाता है.
    JDBC ODBC पुल, एक प्रकार 3 JDBC ड्राइवर जिसका क्लाइंट / सर्वर वास्तुकला Access डेटाबेस फ़ाइल और जावा अलग मशीनों पर होना करने के लिए अनुमति देता है. JDBC-ODBC पुल ग्राहक पूरी तरह जावा में लिखा है और किसी भी स्थापना की आवश्यकता नहीं है - यह एक जावा applet के साथ परोसा दूर कर सकते हैं या एक निर्देशिका है, जहां अपने जावा आवेदन, इंजन सर्वलेट JSP / IDE या अनुप्रयोग सर्वर JAR फ़ाइलों को लोड कर सकते हैं नकल से.

दोनों JDBC एक्सेस गेटवे और JDBC ODBC पुल एमएस एक्सेस ODBC ड्राइवर का उपयोग करने के लिए डेटाबेस फ़ाइल का उपयोग. डेटाबेस विक्रेता ड्राइवर का उपयोग करने के लिए डेटाबेस फ़ाइल के साथ संवाद करने के लिए अपनी साइट पर आईटी कंपनी की नीति के साथ अनुपालन की आवश्यकता हो सकती है.)

how can we connect java to mysql database connection

Using JDBC to connect to MySQL from Java Program




This sample Java program connects to MySQL database using JDBC, executes a query and retrieves and prints the value of the database field.

This same sample code can be used to connect to any type of database, all you need to do is change the connection url (dbUrl in the sample). For this code to work properly, you will need to download the mysql driver for JDBC in other words Java Connectors from mysql.com site.

If after downloading the URL it still doesn't work then it is probably due to the classpath. You will have to add the driver jar file in the classpath.
 
(यह नमूना जावा प्रोग्राम MySQL के डेटाबेस JDBC का उपयोग करने के लिए जोड़ता है, एक क्वेरी और retrives के कार्यान्वित और डेटाबेस फ़ील्ड का मूल्य मुद्रित करता है.

यह एक ही नमूना कोड डेटाबेस के किसी भी प्रकार से कनेक्ट करने के लिए इस्तेमाल किया जा सकता है, तुम सब करने की ज़रूरत कनेक्शन यूआरएल (नमूना में dbUrl) बदल रहा है. इस कोड ठीक से काम करने के लिए, आप JDBC के लिए अन्य जावा कनेक्टर्स शब्दों में mysql.com साइट से MySQL चालक डाउनलोड की आवश्यकता होगी.

यदि URL को डाउनलोड करने के बाद यह अभी भी तो नहीं काम करता है यह शायद classpath के कारण. आप classpath में ड्राइवर जार फ़ाइल जोड़ने के लिए होगा.
)

 

import java.sql.*;
import javax.sql.*;

public class jdbcdemo{

public static void main(String args[]){
String dbtime;
String dbUrl = "jdbc:mysql://your.database.domain/yourDBname";
String dbClass = "com.mysql.jdbc.Driver";
String query = "Select * FROM users";

try {

Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection (dbUrl);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);

while (rs.next()) {
dbtime = rs.getString(1);
System.out.println(dbtime);
} //end while

con.close();
} //end try

catch(ClassNotFoundException e) {
e.printStackTrace();
}

catch(SQLException e) {
e.printStackTrace();
}

}  //end main

}  //end class