Re: Connecting to Oracle on NT via JDBC
Date: 1996/10/15
Message-ID: <32628DF1.3504_at_ihug.co.nz>#1/1
Chris Ash wrote:
>
> Having just installed Oracle Workgroup server on out NT,
> I am now looking to develop a java front end to a simple database
> for internal use in our company. There doesn't seem to be a great
> deal of documentation on the JDBC at the moment.....anyone know
> of any useful resources that I can pilfer ?
>
> Partiuclarly in the area of JDBC/ODBC connectivity......
>
> My knowledge of this area is shakey to say the least. Do I need to
> obtain specific drivers or the ODBC/JDbc bridge to get this to work
> properly ? In fact while I am at it........what is the JDBC/ODBC
> bridge ?
>
> Thanks in advance....Chris.
If you have a look in the sample .java files that come in the JDBC-ODBC
bridge package from JavaSoft, there is a sample file called
simpleselect.java which contains the basic code required to connect to a
database via ODBC. The only requirements is that the client PC know how
to get to the NT box (via hosts file or such like). Then the ODBC
driver software on the client PC needs to be configured to have a Data
Source Name (DSN) for the Oracle server on the NT Box.
Eg..
Here we have an NT Box which is aliased to 'nt_server' in my hosts file,
and a database instance of DEV. I configure a new ODBC DSN to point to
the 'nt_server' and the DEV instance and call the DSN 'NTDEV', then the
code fragment required to connect to the database (assuming that the
JDBC and JDBC-ODBC packages are installed correctly) is as follows;
import java.net.URL;
import java.sql.*;
class DatabaseConnect {
public static void main (String args[]) {
String url = "jdbc:odbc:NTDEV";
try {
// Load the jdbc-odbc bridge driver Class
Class.forName ("jdbc.odbc.JdbcOdbcDriver");
// Attempt to connect to a driver. Each one
// of the registered drivers will be loaded until
// one is found that can process this URL
Connection con = DriverManager.getConnection (
url, "myusername","mypassword"
);
// If we were unable to connect, an exception
// would have been thrown. So, if we get here,
// we are successfully connected to the URL
// Get the DatabaseMetaData object and display
// some information about the connection
DatabaseMetaData dma = con.getMetaData ();
System.out.println("\nConnected to " + dma.getURL());
System.out.println("Driver " +
dma.getDriverName());
System.out.println("Version " +
dma.getDriverVersion());
System.out.println("");
//Close the database connection
con.close();
}
catch (SQLException ex) {
// A SQLException was generated. Catch it and
// display the error information. Note that there
// could be multiple error objects chained
// together
System.out.println ("\n*** SQLException caught ***\n");
while (ex != null) {
System.out.println ("SQLState: " +
ex.getSQLState ());
System.out.println ("Message: " +
ex.getMessage ());
System.out.println ("Vendor: " +
ex.getErrorCode ());
ex = ex.getNextException ();
System.out.println ("");
}
}
catch (java.lang.Exception ex) {
// Got some other type of exception. Dump it. (like the JdbcOdbc Class not being found)
ex.printStackTrace ();
}
System.exit(0);
}
Received on Tue Oct 15 1996 - 00:00:00 CEST
