x404.co.uk
http://www.x404.co.uk/forum/

JDBC Connections
http://www.x404.co.uk/forum/viewtopic.php?f=4&t=244
Page 1 of 2

Author:  Fogmeister [ Wed Apr 29, 2009 9:45 pm ]
Post subject:  JDBC Connections

I'm trying to connect to a database on my PC.

It is something I have never done before and I'm struggling to get over the first hurdle.

I am trying to load the driver class...

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

But I get a class not found exception. Now, I know the driver is available as it is included with OpenOffice Base. Is there something I need to do with a file or anything? Or import something into the class?

Author:  forquare1 [ Wed Apr 29, 2009 10:14 pm ]
Post subject:  Re: JDBC Connections

You'll need to include the driver class, and if using something like Eclipse, tell it to use them as external archives (right click on project > Build path > external archives).

Code:
import java.sql.*;
import java.util.Properties;
import java.util.Vector;
import com.sybase.jdbcx.SybConnection;
import com.sybase.jdbcx.SybDriver;

Code:
try{
         Class.forName("com.sybase.jdbc3.jdbc.SybDriver");
         conn = DriverManager.getConnection(url, user, pass);
      }catch(SQLException e){
         //If we got an SQL exception, tell us what it was.
         System.err.println("Connection failed");
         System.err.println("\\-----------------------------------------/");
         while (e != null) {
            System.err.println("Message:   " + e.getMessage ());
            System.err.println("SQLState:  " + e.getSQLState ());
            System.err.println("ErrorCode: " + e.getErrorCode ());
            e = e.getNextException();
            System.err.println("");
         }
      }catch(Exception e){
         //If it wasn't an SQL exception, not too sure what it was.
         //Print stack trace to see if we can figure it out.
         System.err.println("Failed to connect for unknown reason");
         e.printStackTrace();
      }


Then you can use the connection like this:
Code:
String cluster_id = cid;
      Connection conn = c;
      CallableStatement cs = null;
      ResultSet rs = null;

      String query = "{call find_very_long_bookings " + cluster_id + "}";

      //Try to connect and query database
      try{
         cs = conn.prepareCall(query);
         rs = cs.executeQuery();
      }


HTH,

Ben

Author:  forquare1 [ Wed Apr 29, 2009 10:15 pm ]
Post subject:  Re: JDBC Connections

BTW, the com.XXX.YYY.ETC. came as a jar called com.jar

Author:  Fogmeister [ Wed Apr 29, 2009 10:23 pm ]
Post subject:  Re: JDBC Connections

forquare1 wrote:
You'll need to include the driver class, and if using something like Eclipse, tell it to use them as external archives (right click on project > Build path > external archives).

Code:
import java.sql.*;
import java.util.Properties;
import java.util.Vector;
import com.sybase.jdbcx.SybConnection;
import com.sybase.jdbcx.SybDriver;

Thanks,

The trouble I am coming across is that I don't know where the driver class is? What is it? Is it an actual file? Do I have to download it from somewhere?

I can't import something if I don't have the class on my computer to import it.

forquare1 wrote:
BTW, the com.XXX.YYY.ETC. came as a jar called com.jar
So the code I have got ... sun.jdbc.odbc.JDBCDriver ... should that be included by default? Where can I get that from?

Where do you get the jar files from?

In all the forums I have seen there are people saying "put your jar files into the classpath" but I don't have a jar file?

Sorry for being thick :(.

Thanks again for the help.

Author:  Fogmeister [ Wed Apr 29, 2009 10:38 pm ]
Post subject:  Re: JDBC Connections

Fogmeister wrote:
Class.forName("com.sun.jdbc.odbc.JdbcOdbcDriver");

That's the code. I missed off the com from the beginning in my OP.

Anywho, slowly beginning to work out what I need to do :D

Author:  forquare1 [ Wed Apr 29, 2009 10:41 pm ]
Post subject:  Re: JDBC Connections

No worries Oli, I had the same thing two weeks ago...

I nabbed my com.jar file (which is the driver file) from someone else's project which connected to the same database...Then copied the "Class.forName("com.sun.jdbc.odbc.JdbcOdbcDriver");" from their code too...

I think you can go to the website of the people that make the database and download the drivers somehow...Though if the drivers from OpenOffice are in your class path, you might be ok...

Author:  Fogmeister [ Wed Apr 29, 2009 10:55 pm ]
Post subject:  Re: JDBC Connections

I've given up for now.

Not even sure I've got anything set up to connect to. (Even if I could get past loading the driver).

Oh well.

Author:  forquare1 [ Thu Apr 30, 2009 7:13 am ]
Post subject:  Re: JDBC Connections

What database are you trying to connect to? I presume the OO.o Base db?

Author:  Fogmeister [ Thu Apr 30, 2009 7:43 am ]
Post subject:  Re: JDBC Connections

forquare1 wrote:
What database are you trying to connect to? I presume the OO.o Base db?
Yeah.

TBH I've never done anything with databases at all before (from scratch). I develop in Progress at work which is attached to a database but I would have no idea how to set it up from the beginning.

In Open Office I created the database (just with one table atm) and went into preferences and enabled the com.sun.star.comp.sdbc.JdbcDriver connection in the "Connections" tab.

Is that all I would need to do to be able to get a connection (database side)? All I need now is to be able to use the JdbcDriver that I enabled but I can't seem to find it or anything info on how to get it.

Author:  forquare1 [ Thu Apr 30, 2009 8:59 am ]
Post subject:  Re: JDBC Connections

I think this may be of use to use. From what I've read, you use MySQL to connect with the OO.o db...

Author:  Fogmeister [ Thu Apr 30, 2009 9:22 am ]
Post subject:  Re: JDBC Connections

forquare1 wrote:
I think this may be of use to use. From what I've read, you use MySQL to connect with the OO.o db...

Ooh, thanks!

I'lll give it a go :D

I'll let you know how it goes tonight!

Author:  forquare1 [ Thu Apr 30, 2009 9:55 am ]
Post subject:  Re: JDBC Connections

Not that my code is very good, but if you PM me an email address I'll send you the my class I wrote to connect to the db I was using, might be able to strip something out of it? It's a bit crappy as it was only for a script, though Perl would have been easier, I couldn't get my head around it, so write a Java app instead :D I did ponder whether I could have done it in shell, but the lack of arrays was an issue :lol:

Author:  Fogmeister [ Thu Apr 30, 2009 5:02 pm ]
Post subject:  Re: JDBC Connections

forquare1 wrote:
I think this may be of use to use. From what I've read, you use MySQL to connect with the OO.o db...

Having read that one through I think that is how to connect OpenOffice to an existing database that is held elsewhere.

Author:  Fogmeister [ Thu Apr 30, 2009 5:41 pm ]
Post subject:  Re: JDBC Connections

Woo! I'm slowly, slowly getting there :D

I know have the driver loaded but just need to make sure I have set the database up correctly to actually connect to it.

Do you know if there are any MySQL databases just kicking around for people to connect to?

Author:  forquare1 [ Thu Apr 30, 2009 6:32 pm ]
Post subject:  Re: JDBC Connections

I've got no idea...However you might like to ask the same question on the Java/MySQL forums...(Not sure if MySQL has forums, there is a generic Sun forum though)

Page 1 of 2 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/