Reply to topic  [ 16 posts ]  Go to page 1, 2  Next
JDBC Connections 
Author Message
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
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?

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Wed Apr 29, 2009 9:45 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 6:36 pm
Posts: 5150
Location: /dev/tty0
Reply with quote
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


Wed Apr 29, 2009 10:14 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 6:36 pm
Posts: 5150
Location: /dev/tty0
Reply with quote
BTW, the com.XXX.YYY.ETC. came as a jar called com.jar


Wed Apr 29, 2009 10:15 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
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.

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Wed Apr 29, 2009 10:23 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
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

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Wed Apr 29, 2009 10:38 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 6:36 pm
Posts: 5150
Location: /dev/tty0
Reply with quote
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...


Wed Apr 29, 2009 10:41 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
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.

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Wed Apr 29, 2009 10:55 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 6:36 pm
Posts: 5150
Location: /dev/tty0
Reply with quote
What database are you trying to connect to? I presume the OO.o Base db?


Thu Apr 30, 2009 7:13 am
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
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.

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Thu Apr 30, 2009 7:43 am
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 6:36 pm
Posts: 5150
Location: /dev/tty0
Reply with quote
I think this may be of use to use. From what I've read, you use MySQL to connect with the OO.o db...


Thu Apr 30, 2009 8:59 am
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
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!

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Thu Apr 30, 2009 9:22 am
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 6:36 pm
Posts: 5150
Location: /dev/tty0
Reply with quote
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:


Thu Apr 30, 2009 9:55 am
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
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.

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Thu Apr 30, 2009 5:02 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
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?

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Thu Apr 30, 2009 5:41 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 6:36 pm
Posts: 5150
Location: /dev/tty0
Reply with quote
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)


Thu Apr 30, 2009 6:32 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 16 posts ]  Go to page 1, 2  Next

Who is online

Users browsing this forum: No registered users and 25 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.