I'm sure you've all heard this before, but I can't make head nor tails
about this stuff.
First of all, I assume that the JDBC-ODBC bridge is installed on my
computer (jdk 1.1.5 is installed, and msaccess too) I've understood that
this won't work with applets, but that's no problem so far, because I'm
just trying to connect to a local database in a stand-alone application.
Now is the question: how do I tell java where this database is? I've
been trying to execute the code below with a variety of syntaxes for the
so-called URL, but it just won't work.
To be complete: my database resides on "D:/jdk/source/tomstest.mdb", and
I run the application in "D:/jdk/source"
So I guess the basic question is: how is the String databaseURL below to
be declared?
I've been looking all over sun's site, but they're so worried about
portability that they just won't give any practical solutions :-(
Any help would be greatly appreciated.
This is my code:
<Code>
//package TOPLink.Tutorials.GettingStarted;
import java.sql.*;
import java.util.*;
/**
* This class should be used to test that you can connect through JDBC
to your database.
* Before testing if you can connect through TOPLink you MUST ensure
that you can connect
* to JDBC outside TOPLink. If you cannot connect through JDBC contact
your system administrator,
* JDBC driver provider, or your Java IDE provider to figure out why.
* <p>
* Possible Reason Include:
* <ul>
* <li> You have not installed the JDBC driver correctly.
* <li> You are using a JDBC-ODBC driver and have not install ODBC
correctly.
* <li> Your IDE does not support the JDBC driver that you are using.
* <li> You have not provided the correct connection information to the
driver.
* </ul>
*/
class JDBCConnectTest {
/**
* Test connecting to the database.
* You should put your own connection information here and run this
class.
*/
public static void connect(JDBCConnectTest test)
{
System.out.println("Starting to connect...");
String driverClassName = test.getDriverClassName(); // Insert
your JDBC driver class name here.
String databaseURL = test.getDatabaseURL(); // Insert your JDBC
URL here.
Properties properties = test.getProperties(); // This should
contain any additional information your database requires.
try { // This ensures that the driver has been loaded and registered.
Class.forName(driverClassName);
} catch (ClassNotFoundException exception) {
throw new RuntimeException("Configuration error. Class " +
driverClassName + " not found.");
}
Connection newConnection;
try {
newConnection = DriverManager.getConnection(databaseURL, properties);
} catch (SQLException exception) {
throw new RuntimeException("A database error occured at
login: " + exception.getMessage());
}
System.out.println("Connection successful.");
System.out.println("Starting to disconnect...");
try {
newConnection.close();
} catch (SQLException exception) {
throw new RuntimeException("A database error occured at logout, " +
exception.getMessage());
}
System.out.println("Disconnection successful.");
}
/**
* Insert your JDBC URL here.
*/
public String getDatabaseURL()
{
return "jdbc:odbc:tomstest.mdb";
}
/**
* Insert your JDBC driver class name here.
*/
public String getDriverClassName()
{
return "sun.jdbc.odbc.JdbcOdbcDriver";
}
/**
* This should contain any additional information your database
requires.
*/
public Properties getProperties()
{
Properties properties = new Properties();
properties.put("user", "");
properties.put("password", "");
properties.put("server", "");
properties.put("database", "//D|/jdk/source/tomstest.mdb");
return properties;
}
/**
* Test connecting to the database.
* You should put your own connection information here and run this
class.
*/
public static void main (String args[])
{
connect(new JDBCConnectTest());
}
}
</Code>
--
tomK
Leuven, Belgium
*********************************************************
The man who follows the crowd will usually get no further
than the crowd. The man who walks alone is likely to find
himself in places where no one has ever been.
- Alan Ashley-Pitt
*********************************************************
Since you're using the JDBC-ODBC bridge you have to connect through an ODBC DSN rather
than directly to the database. You set up the DSN using the ODBC manager which should be
in your Control Panel (if not, ODBC drivers are available from Microsoft's site and
include the ODBC manager).
As an example, here's some code from an app I wrote which uses MS Access in the same way:
public void makeConnection() throws SQLException, ClassNotFoundException {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// Assume that the name of our DSN is "mydsn"
dbCon = DriverManager.getConnection("jdbc:odbc:mydsn", username, password);
stmt = dbCon.createStatement();
dbCon.setAutoCommit(true);
}
Tom Klaasen wrote:
> Hello,
>
> I'm sure you've all heard this before, but I can't make head nor tails
> about this stuff.
> First of all, I assume that the JDBC-ODBC bridge is installed on my
> computer (jdk 1.1.5 is installed, and msaccess too) I've understood that
> this won't work with applets, but that's no problem so far, because I'm
> just trying to connect to a local database in a stand-alone application.
> Now is the question: how do I tell java where this database is? I've
> been trying to execute the code below with a variety of syntaxes for the
> so-called URL, but it just won't work.
> To be complete: my database resides on "D:/jdk/source/tomstest.mdb", and
> I run the application in "D:/jdk/source"
> So I guess the basic question is: how is the String databaseURL below to
> be declared?
> I've been looking all over sun's site, but they're so worried about
> portability that they just won't give any practical solutions :-(
>
> Any help would be greatly appreciated.
>
> This is my code:
> <snip>
You may get some help by looking at our example applets that are
demonstrated on our web site at http://www.softsyn.com/JDBLite.
The site has four running demo applets going against MS Access. Each has
full source code.
Regards
David Murphy
Software Synergy
http://java.sun.com/products/jdk/1.1/docs/guide/jdbc/getstart/SimpleSelect.d
oc.html
or under your JDK 1.1 directory under the same relative path. The DNS is
set up in your control panel-32bitODBC as described in a previous reponse.
You must assign the path to your database under the Control
panel-32bitODBC-Configure-Directory as well.