Connect JDBC with SQL Server
Solution 1
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/
Solution 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.
Solution 3
Your runtime environment uses jre 1.6 and the sql jar you are using are not compatible with java 6.Include sqljdbc4.jar instead.
Solution 4
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
Related videos on Youtube
user1912404
Updated on August 29, 2020Comments
-
user1912404 almost 4 years
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?
-
Mohammod Hossain over 11 yearsJava Runtime Environment (JRE) version 1.6 is not supported by this driver.What is your jdbc driver version?
-
a_horse_with_no_name over 11 yearsThe error message is pretty clear. You either need to use Java7 or the Microsoft driver for Java6
-
matt freake over 11 yearsI'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.
-
user1912404 over 11 yearshow can i update my version of java? or is it not recommended?
-
-
user1912404 over 11 yearsI downloaded it , how can i include it in eclipse and how does it differs from jdbc4?
-
Obl Tobl over 11 yearsyou 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.
-
Obl Tobl over 11 yearshave 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".
-
user1912404 over 11 yearsgot another exception now:
-
user1912404 over 11 yearsException 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.access$000(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method)
-
Obl Tobl over 11 yearsthe driver you downloaded is for Java 7 only. Try an older one, for example version 1.2.6