Using the JDBC-ODBC bridge to connect to the Oracle database and JDBC driver is almost the same, so we don't have to set the environment variable. There are two key statements for connecting to the database in JDBC:
Class. forname ("oracle. JDBC. Driver. oracledriver ");
Connection con = drivermanager. getconnection ("JDBC: oracle: thin: @ 127.0.0.1: 1521: dbname", "username", "password ");
Similarly, bridge connections are the two statements, but the drive and method have changed, as shown below:
Class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver ");
Connection con = drivermanager. getconnection ("JDBC: ODBC: userdsn", "username", "password ");
Userdsn must be configured in the ODBC source to test the complete connection.ProgramSee:
Import Java. SQL. *;
public class jdbc_odbc
{< br> Public static void main (string [] ARGs)
{< br>
connection conn;
try
{< br> class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver ");
}< br> catch (exception e)
{< br> system. out. println ("Driver exception! ");
}< br> try
{conn = drivermanager. getconnection ("JDBC: ODBC: User", "Scott", "Tiger");
system. out. println ("connect to the database normally. ");
}< br> catch (sqlexception e)
{< br> system. Out. println (" connection error! ");
}< br>
}< BR >}