问题描述
我在本地 PC 上安装了 MSSQL 2008,我的 Java 应用程序需要连接到 MSSQL 数据库.我是 MSSQL 的新手,我想在为我的 Java 应用程序创建用户登录和通过 JDBC 获取连接方面获得一些帮助.到目前为止,我尝试为我的应用程序创建一个用户登录名并使用以下连接字符串,但我根本不起作用.任何帮助和提示将不胜感激.
I have MSSQL 2008 installed on my local PC, and my Java application needs to connect to a MSSQL database. I am a new to MSSQL and I would like get some help on creating user login for my Java application and getting connection via JDBC. So far I tried to create a user login for my app and used following connection string, but I doesn't work at all. Any help and hint will be appreciated.
jdbc:jtds:sqlserver://127.0.0.1:1433/dotcms
username="shuxer" password="itarator"
推荐答案
使用 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!
这篇关于如何使用 JDBC 连接到 SQL Server 2008 数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!