Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » jdbc generic connection code?(how to connect to a data object using jdbc in a file open dialog)
jdbc generic connection code? [message #688551] Fri, 24 June 2011 22:23 Go to next message
Gary Dale is currently offline Gary DaleFriend
Messages: 5
Registered: June 2011
Junior Member
I'm presumably not the first to try this. I want to open a file using a jdbc connection so that no matter what type of file I'm opening (Access, dBASE, Excel, ods, odb, etc.) I can use normal SQL database commands to handle it. As I understand it, that's the point behind odbc & jdbc.

I've found some example code (I'm new to Java & Eclipse) for a MySql connection that I've modified slightly as best I can to handle this:

public Connection dbConnector(File f) {
try {
Connection con = DriverManager.getConnection("jdbc:odbc:" + f);
return con;
} catch ( SQLException err ) {
System.out.println( err.getMessage( ) );
return null;
}
}

This of course doesn't work. The error message is:
No suitable driver found for jdbc:odbc:/path/to/file/sample.xls
where /path/to/file/sample.xls are the full path in the real message.

I'm using Eclipse 3.5.2 on Debian/Wheezey.

Can someone point me in the right direction for at least getting a connection to an Excel spreadsheet?

TIA!
(no subject) [message #689349 is a reply to message #688551] Mon, 27 June 2011 13:40 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: Rich Kulp

Hi,

There is no such thing as a generic JDBC driver implementation that can
talk to anything. JDBC provides a common interface of the programming
side but you still specific JDBC drivers for each and every kind of
datasource you want to access.

As for JDBC drivers for Excel, I don't know anything about that. I only
access databases.

--
Thanks,
Rich Kulp
Re: (no subject) [message #689365 is a reply to message #689349] Mon, 27 June 2011 14:02 Go to previous messageGo to next message
Gary Dale is currently offline Gary DaleFriend
Messages: 5
Registered: June 2011
Junior Member
So no one has written a generic jdbc connection class to come back with a connector if a jdbc driver exists for the file?

Reading my own code attempt, it looks like it is using jdbc to connect to an odbc driver. Presumably this would require Windows since odbc is a Microsoft product. This would make my Java non-portable, which defeats my purpose in using Java.

Speaking of "access databases", I did at one time use Access to open spreadsheets and manipulate their data. I note that LibreOffice Base can do the same thing. This suggests that what I am trying to do is possible using free/open source software.
Re: (no subject) [message #689575 is a reply to message #689365] Mon, 27 June 2011 21:55 Go to previous messageGo to next message
Gary Dale is currently offline Gary DaleFriend
Messages: 5
Registered: June 2011
Junior Member
Further to my code above, I've discovered that I need to include the line
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
ahead of the getConnection attempt. I believe this loads the driver. Unfortunately this throws a ClassNotFoundException, leading me to believe that I don't have the named driver in my installation.

My java version is:
java version "1.6.0_18"
OpenJDK Runtime Environment (IcedTea6 1.8.7) (6b18-1.8.7-4)
OpenJDK 64-Bit Server VM (build 16.0-b13, mixed mode)

Does anyone know if I need to install extra libraries or components as part of the Debian Eclipse setup to get jdbc?
Re: (no subject) [message #689584 is a reply to message #689575] Mon, 27 June 2011 22:28 Go to previous messageGo to next message
Gary Dale is currently offline Gary DaleFriend
Messages: 5
Registered: June 2011
Junior Member
OK, here's my latest code:

class Connections {
public Connection Connector;

public void dbConnector(File f) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch ( Exception err ) {
System.out.println( "forName error: " + err.getMessage( ) );
}
try {
this.Connector = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)};DBQ=" + f);
} catch ( SQLException err ) {
System.out.println( "SQL error: " + err.getMessage( ) );
}
}
}

After putting the Class.forName in a try-catch, I'm now getting the console messages:
forName error: sun.jdbc.odbc.JdbcOdbcDriver
SQL error: No suitable driver found for jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)};DBQ=/home/garydale/Jan2809.xls

which seems to confirm my theory that I'm not getting the jdbc driver loaded. Any ideas anyone?


Re: (no subject) [message #691161 is a reply to message #689584] Thu, 30 June 2011 20:56 Go to previous message
David Wegener is currently offline David WegenerFriend
Messages: 1351
Registered: July 2009
Senior Member
You really need to find a Java programing forum for your questions. This forum is for questions about the Java Development Tools provided by Eclipse. It is not for general Java programming questions.
Previous Topic:Indigo JDT batch compiler issue
Next Topic:data source enterprise explorer?
Goto Forum:
  


Current Time: Fri Dec 19 10:10:22 GMT 2014

Powered by FUDForum. Page generated in 0.14987 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software