Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Java work with SQL Server?

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 appI 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 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();         }     } }; 
like image 845
doro Avatar asked Aug 21 '09 07:08

doro


People also ask

Can I use Java with SQL Server?

SQL Server 2019 supports Python, R, and Java. Now, the support of Java language extension in SQL Server 2019 allows data scientists or data engineers to run the precompiled Java programs (JAR) and securely execute JAR code on SQL Server. Note: SQL Server 2019 supports Java on both Windows and Linux platforms.

How can I use Java and SQL together?

STEP 1: Allocate a Connection object, for connecting to the database server. STEP 2: Allocate a Statement object, under the Connection created earlier, for holding a SQL command. STEP 3: Write a SQL query and execute the query, via the Statement and Connection created. STEP 4: Process the query result.

Is Java needed for SQL Server?

Starting with SQL Server 2022, no Java runtime is installed by SQL Setup. A database engine instance is required. You cannot install just the Java Language Extension features, although you can add them incrementally to an existing instance.


2 Answers

Have you tried the jtds driver for SQLServer?

like image 184
oxbow_lakes Avatar answered Oct 03 '22 19:10

oxbow_lakes


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)?

like image 35
Jesper Avatar answered Oct 03 '22 18:10

Jesper