2

Am trying to connect my JAVA code to a SQL Server I have locally.This is what I am trying to:

 public static void main(String[] args) throws ClassNotFoundException, SQLException, InvalidFormatException, IOException {
 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
 con = DriverManager.getConnection("jdbc:sqlserver://localhost" + "databaseName=talisman" + "user=karim" + "password=123"); 
//rest of the code
}}

I got this exception:

 Dec 18, 2012 11:29:40 AM com.microsoft.sqlserver.jdbc.SQLServerConnection <init>
SEVERE: Java Runtime Environment (JRE) version 1.6 is not supported by this driver. Use the sqljdbc4.jar class library, which provides support for JDBC 4.0.
Exception in thread "main" java.lang.UnsupportedOperationException: Java Runtime Environment (JRE) version 1.6 is not supported by this driver. Use the sqljdbc4.jar class library, which provides support for JDBC 4.0.
 at com.microsoft.sqlserver.jdbc.SQLServerConnection.<init>(SQLServerConnection.java:304)
 at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1011)
 at java.sql.DriverManager.getConnection(Unknown Source)
 at java.sql.DriverManager.getConnection(Unknown Source)
 at cct.karim.karim.main(karim.java:62)

and am pretty stuck... I would appreciate your help, but please be specific in your answers

Update:

I am using eclipse, and I downloaded the jbdc4 . can you tell me how I can include it in eclipse please?

asked Dec 18, 2012 at 9:37
4
  • 2
    Java Runtime Environment (JRE) version 1.6 is not supported by this driver.What is your jdbc driver version? Commented Dec 18, 2012 at 9:40
  • 1
    The error message is pretty clear. You either need to use Java7 or the Microsoft driver for Java6 Commented Dec 18, 2012 at 9:48
  • 1
    I'm not sure if you are new to Java, but learning to read stack traces is an important skill. That stack trace is pretty clear at saying that your driver is not compatable with your version of Java. Some stack traces are long and intimidating, reading them pays dividends. Commented Dec 18, 2012 at 10:03
  • how can i update my version of java? or is it not recommended? Commented Dec 18, 2012 at 10:05

5 Answers 5

3

you can try connecting JAVA code to a SQL Server locally in the below mentioned way also..

you need to have Microsoft SQL Server JDBC Driver SQL JDBC Authentication file

after authentication you After download the authentication file copy this file to window’s system32 folder. now set the class path for SQL Server JDBC driver(jar file ). now suppose you have a database mssumit , user name and password is sumit. you can also connect to SQL Server with widows authentication, you can use the following code if you want to connect with windows authentication.

Connection con=DriverManager.getConnection("jdbc:sqlserver://localhost;databaseName=mssumit;integratedSecurity=true");
Connection conn =DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=mssumit;user=sumit;password=sumit;");

Sometime it is possible that you are unable to connect with user name in that case please check database connection properties that you are able to connect with SQL Server Authentication. instantiate the SQL Server driver class with the following code

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
Connection conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=mssumit;user=sumit;password=sumit;");

for detailed explanation, procedure and source code you can check this link http://compilr.org/java/jdbc-connection-with-ms-sql-server-in-java/

answered Dec 18, 2012 at 10:26
Sign up to request clarification or add additional context in comments.

Comments

2

Try the jtds driver, thats an alternative driver for MS SQL. For more information how to use the driver correctly, please read the Getting started.

answered Dec 18, 2012 at 9:43

6 Comments

I downloaded it , how can i include it in eclipse and how does it differs from jdbc4?
you can add the jar to your project under "Properties > Java Build Path > Libraries". The jtds driver supports all versions of the MS SQL Server, so your problems should be gone. We had much less problems with this driver.
have you removed the other driver and changed the classname of your driver? try "net.sourceforge.jtds.jdbc.Driver" instead of "com.microsoft.sqlserver.jdbc.SQLServerDriver".
got another exception now:
Exception in thread "main" java.lang.UnsupportedClassVersionError: net/sourceforge/jtds/jdbc/Driver : Unsupported major.minor version 51.0 at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClassCond(Unknown Source) at java.lang.ClassLoader.defineClass(Unknown Source) at java.security.SecureClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.access000ドル(Unknown Source) at java.net.URLClassLoader1ドル.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method)
|
1

Read the exception message and you'll see you are using an old JDBC driver that does not support Java 1.6.

You should get the new version that uses JDBC 4.0 (sqljdbc4.jar) from here

answered Dec 18, 2012 at 9:41

Comments

1

Your runtime environment uses jre 1.6 and the sql jar you are using are not compatible with java 6.Include sqljdbc4.jar instead.

answered Dec 18, 2012 at 9:42

Comments

0

Hope this thread will help you - How do I connect to a SQL Server 2008 database using JDBC?.

Essentially, there are a couple of ways to use JDBC - using Windows authentication and SQL authentication. SQL authentication is probably the easiest. What you can do is something like:

String userName = "username";
String password = "password";
String url = "jdbc:sqlserver://MYPC\\SQLEXPRESS;databaseName=MYDB";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(url, userName, password);

after adding sqljdbc4.jar to the build path.

For Window authentication you can do something like:

String url = "jdbc:sqlserver://MYPC\\SQLEXPRESS;databaseName=MYDB;integratedSecurity=true";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(url);

and then add the path to sqljdbc_auth.dll as a VM argument (still need sqljdbc4.jar in the build path).

Please take a look here for a short step-by-step guide showing how to connect to SQL Server from Java using jTDS and JDBC using eclipse should you need more details. Hope it helps!

answered Aug 28, 2013 at 15:20

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.