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?
-
2Java Runtime Environment (JRE) version 1.6 is not supported by this driver.What is your jdbc driver version?Mohammod Hossain– Mohammod Hossain2012年12月18日 09:40:57 +00:00Commented Dec 18, 2012 at 9:40
-
1The error message is pretty clear. You either need to use Java7 or the Microsoft driver for Java6user330315– user3303152012年12月18日 09:48:06 +00:00Commented Dec 18, 2012 at 9:48
-
1I'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.matt freake– matt freake2012年12月18日 10:03:55 +00:00Commented Dec 18, 2012 at 10:03
-
how can i update my version of java? or is it not recommended?user1912404– user19124042012年12月18日 10:05:47 +00:00Commented Dec 18, 2012 at 10:05
5 Answers 5
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/
Comments
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.
6 Comments
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
Comments
Your runtime environment uses jre 1.6 and the sql jar you are using are not compatible with java 6.Include sqljdbc4.jar instead.
Comments
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!