views:

3754

answers:

4

I know this is a basic question, but I can't seem to find an answer and I apologize, if this question is way too stupid, but here we go:

I am supposed to work with SQL Server (no problem so far) and with Java (love java, so no problem here either), but now: What am I supposed to do to make the combination work? I got: JRE 1.6 and the sqljdbc4.jar ... Before I put sqljdbc4.jar into my classpath I had sqljdbc.jar in it and with a test-program I got this exception:

21.08.2009 09:26:59 com.microsoft.sqlserver.jdbc.SQLServerConnection <init>
SCHWERWIEGEND: Die Java-Laufzeitumgebung (Java Runtime Environment, JRE), Version 1.6,
wird von diesem Treiber nicht unterstützt. Verwenden Sie die Klassenbibliothek 
'sqljdbc4.jar', die Unterstützung für JDBC 4.0 bietet.
java.lang.UnsupportedOperationException: Die Java-Laufzeitumgebung (Java Runtime 
Environment, JRE), Version 1.6, wird von diesem Treiber nicht unterstützt. Verwenden 
Sie die Klassenbibliothek 'sqljdbc4.jar', die Unterstützung für JDBC 4.0 bietet.
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.<init>(SQLServerConnection.java:223)
    at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:840)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at msSqlTest.DB.dbConnect(DB.java:13)
    at msSqlTest.TestConnection.main(TestConnection.java:7)

Sorry for the German ... It basically means, that I should use sqljdbc4.jar, b/c the JRE I am using is not supported by the driver. So I put sqljdbc4.jar into my classpath, but it didn't work, so I am kinda lost, what I could do.

Maybe someone could tell be in an idiot-proof way what I should do :(

Oh yeah, here is the test-prog I use:

import java.sql.*;

public class TestConnection{
    public static void main(String[] args){
        // Neue DB und los geht's :)
        DB db = new DB();
        db.dbConnect(
            "jdbc:sqlserver://localhost:1433/muff",
            "user",
            "pw" );
    }
}


class DB{
    public DB() {}

    public void dbConnect(  String db_connect_string, 
                            String db_userid, 
                            String db_password){
        try{
        Class.forName( "com.microsoft.sqlserver.jdbc.SQLServerDriver" );
            Connection conn = DriverManager.getConnection(
                      db_connect_string, 
                  db_userid, 
                  db_password);
            System.out.println( "connected" );
        }
        catch( Exception e ){
            e.printStackTrace();
        }
    }
};
+7  A: 

Have you tried the jtds driver for SQLServer?

oxbow_lakes
+1 - that's the one to use
Nick Holt
not yet, but something tells me I should ;)
doro
So, I downloaded ... and now what happened? IT WORKS!!!What I've learned: listen to your inner-voice (or stackoverflow ;) ...)
doro
Do you use jTDS oxbow?
pjp
Haha - yes, it's the recommended SQL Server driver to use with Atlassian's Confluence and JIRA, which we have installed here
oxbow_lakes
A: 

What about the official JDBC 4.0 compatible JDBC driver from Microsoft?

Don't go near it, intentionally or not it's pretty poor or was the last time I used it - the conspiracy theorist in me might say that's intentional but I suspect they simple didn't devote enough resources to it...
Nick Holt
that's the one I am using :(
doro
+1  A: 

Do not put both the old sqljdbc.jar and the new sqljdbc4.jar in your classpath - this will make it (more or less) unpredictable which classes are being used, if both of those JARs contain classes with the same qualified names.

You said you put sqljdbc4.jar in your classpath - did you remove the old sqljdbc.jar from the classpath? You said "it didn't work", what does that mean exactly? Are you sure you don't still have the old JAR in your classpath somewhere (maybe not explicitly)?

Jesper
I think you'll find that whichever jar is in the classpath first will be favored (with the Sun JVM), although agreed, it's best to only include the jar you actually want to use.
Nick Holt
thnx for the reply. I didn't make it clear, sorry, but yes, I did remove the sqljdbc.jar from the classpath and put the sqljdbc4.jar into the classpath, but I still get the same exception. How can I see if it is still "not explicitly" in the classpath?
doro
With "not explicitly", I meant that maybe you are running code in a server (Tomcat for example) and you have the JAR in a lib directory of the server, or maybe it's even in your lib/ext directory of the JRE (JAR files in there are automatically put in the classpath).
Jesper
+1  A: 

The driver you are using is the MS SQL server 2008 driver (sqljdbc4.jar). As stated in the MSDN page it requires Java 6+ to work.

http://msdn.microsoft.com/en-us/library/ms378526.aspx

sqljdbc4.jar class library requires a Java Runtime Environment (JRE) of version 6.0 or later.

I'd suggest using the 2005 driver which I beleive is in (sqljdbc.jar) or as Oxbow_Lakes says try the jTDS driver (http://jtds.sourceforge.net/).

pjp
thnx for the hint with the jtds ... I tried it and it worked :)
doro