将JDBC与SQL Server连接 [英] Connect JDBC with SQL Server

查看:145
本文介绍了将JDBC与SQL Server连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将我的JAVA代码连接到我本地的SQL Server。这就是我想要的:

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得到此例外:

    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

更新:

我正在使用eclipse,我已下载jbdc4。你能告诉我如何把它包含在eclipse中吗?

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

推荐答案

你可以尝试在本地将JAVA代码连接到SQL Server下面提到的方式也是..

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

你需要有
Microsoft SQL Server JDBC驱动程序
SQL JDBC身份验证文件

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

认证后你下载认证文件后将此文件复制到window的system32文件夹中。现在设置SQL Server JDBC驱动程序(jar文件)的类路径。现在假设你有一个数据库mssumit,用户名和密码是sumit。您还可以使用widows身份验证连接到SQL Server,如果要连接Windows身份验证,可以使用以下代码。

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;");

在某些情况下,您可能无法连接用户名,请检查数据库连接属性您可以使用SQL Server身份验证进行连接。使用以下代码实例化SQL Server驱动程序类

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;");

有关详细说明,程序和源代码,您可以查看此链接 http://compilr.org/java/jdbc-connection-with-ms-sql- server-in-java /

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

这篇关于将JDBC与SQL Server连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆