且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用 JDBC 连接到 SQL Server 2008 数据库?

更新时间:2023-02-22 13:49:15

使用 JDBC 主要有两种方式——使用 Windows 身份验证和 SQL 身份验证.SQL 身份验证可能是最简单的.你可以做的是:

There are mainly two 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);

将 sqljdbc4.jar 添加到构建路径后.

after adding sqljdbc4.jar to the build path.

对于 Window 身份验证,您可以执行以下操作:

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

然后将sqljdbc_auth.dll的路径添加为VM参数(构建路径中仍然需要sqljdbc4.jar).

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

请查看这里 一个简短的分步指南,显示如果您需要更多详细信息,如何使用 jTDS 和 JDBC 从 Java 连接到 SQL Server.希望有帮助!

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 should you need more details. Hope it helps!