Sie sind auf Seite 1von 2

Steps Involved in a Java program to Connect a Database JDBC/ODBC Bridge : To connect a database we have to first create, a 32 bit ODBC

driver that is used to communicate with the tables held within the database. Create a 32 bit ODBC driver (DSN) via which the Java UI will communicate with the database tables, as described in the following steps: 1. Go to Start Settings 2. Control Panel 3. Administrative Tools 4. Data Sources(ODBC) : 32 Bit ODBC driver. 5. User/System DSN, Click on ADD 6. Select the appropriate driver like Microsoft Access Driver(*.mdb) for connecting Access database, SQL Server for SQL server database and so on. 7. A form is opened that asks about the Data Source Name. Fill the data Source Name field on the interface. 8. Click on Select button, Select the path for the database by browsing through the directory tree structure of the hard disk until the proper database file is located. Once these steps are completed, a 32 bit ODBC, named Data Source(driver) exists and has been registered with the computer. This driver knows where the database, is and can communicate with any of its tables. The system DSN now exists. The DSN is aware of the existence of the database. The UI (Programs) are coded using Java. Java cannot communicate directly with an ODBC driver as this driver has been created using techniques that are outside Java. Hance, using appropriate Java code and associated Java Drivers Bridge must be set up between the 32 bit ODBC driver and the Java UI. This is a JDBC: ODBC Bridge. This allows the Java UI to communicate with the database. // Example for connecting a database implementing JDBC:ODBC driver import java.sql.*; class JDBCConnect { public static void main(String args[ ]) throws Exception { try { // Load the Driver Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver"); // Populate the variable url with the name of the 32bit ODBC driver String url = "jdbc:odbc:java"; // java is a DSN name // Create a connection object and pass the name of the 32bit ODBC // driver to the driver manager Connection conn = DriverManager.getConnection(url); // Connection object represents a connection with a database.

// DriverManager works as the interface between the application(UI) and // the ODBC driver created to communicate with the table. // getConnection method attempts to establish a connection to the specified // database. // Indicates that the connection was successful System.out.println("Successfully Connected"); // Close the connection conn.close(); } // Handle SQL exceptions if they are thrown catch (SQLException sqlExcep) { System.out.println("ERROR: " + sqlExcep.getMessage()); } } // End of main() method // End of class JDBC Connect

-----------------------------------------------------------------------------------------------------------------Why is JDBC and not ODBC API is used with Java. ODBC API is platform dependent API. i.e. it is written in languages like c, c++ and compiled to native platform. Although they are fast but same is not true if you access them from java. As java needs to do a lot of work to access native API. Second thing is that ODBC datatypes are not easily compatible to java data-types, reason being that ODBC data types are platform dependent while java data-types are independent of platform. Third thing is that if your data-base has jdbc driver then your code communicates directly to db, and that's fast. --------------------------------------------------------------------------------------------------------------------