Java 如何在 MS SQL Server 2008 Express 中使用 MS JDBC 驱动程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/540489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I use the MS JDBC driver with MS SQL Server 2008 Express?
提问by StoneHeart
My configuration:
我的配置:
- windows XP SP3
- JDBC 2005
- MS SQL Server 2008 Express, exposed via tcp/ip on port 1433
- sqljdbc.jar in class path
- 视窗 XP SP3
- JDBC 2005
- MS SQL Server 2008 Express,通过端口 1433 上的 tcp/ip 公开
- 类路径中的 sqljdbc.jar
I tried:
我试过:
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
con = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433/SQLEXPRESS2008;databaseName=Test;selectMethod=cursor", "sa", "");
}
catch (Exception e) {
e.printStackTrace();
}
But it always throws an exception:
但它总是抛出异常:
java.sql.SQLException: No suitable driver
I also tried the following urls:
我还尝试了以下网址:
localhost:1433/SQLEXPRESS2008
localhost/SQLEXPRESS2008
localhost
Same results. Any help?
结果一样。有什么帮助吗?
采纳答案by Cheeso
You have the wrong URL.
您的网址错误。
I don't know what you mean by "JDBC 2005". When I looked on the microsoft site, I found something called the Microsoft SQL Server JDBC Driver 2.0. You're going to want that one - it includes lots of fixes and some perf improvements.[edit: you're probably going to want the latest driver. As of March 2012, the latest JDBC driver from Microsoft is JDBC 4.0]
我不知道你所说的“JDBC 2005”是什么意思。当我查看 microsoft 站点时,我发现了一个叫做Microsoft SQL Server JDBC Driver 2.0 的东西。 你会想要那个 - 它包括许多修复和一些性能改进。[编辑:您可能需要最新的驱动程序。截至 2012 年 3 月,Microsoft 的最新 JDBC 驱动程序是JDBC 4.0]
Check the release notes. For this driver, you want:
检查发行说明。对于此驱动程序,您需要:
URL: jdbc:sqlserver://server:port;DatabaseName=dbname
Class name: com.microsoft.sqlserver.jdbc.SQLServerDriver
It seems you have the class name correct, but the URL wrong.
似乎您的类名正确,但 URL 错误。
Microsoft changed the class name and the URL after its initial release of a JDBC driver. The URL you are using goes with the original JDBC driver from Microsoft, the one MS calls the "SQL Server 2000 version". But that driver uses a different classname.
Microsoft 在 JDBC 驱动程序的初始发布后更改了类名和 URL。您使用的 URL 与 Microsoft 的原始 JDBC 驱动程序一致,MS 称为“SQL Server 2000 版本”。但该驱动程序使用不同的类名。
For all subsequent drivers, the URL changed to the form I have here.
对于所有后续驱动程序,URL 更改为我在此处提供的形式。
This is in the release notes for the JDBC driver.
这在 JDBC 驱动程序的发行说明中。
回答by raupach
You can try the following. Works fine in my case:
您可以尝试以下操作。在我的情况下工作正常:
- Download the current jTDS JDBC Driver
- Put jtds-x.x.x.jar in your classpath.
- Copy ntlmauth.dll to windows/system32. Choose the dll based on your hardware x86,x64...
- The connection url is: 'jdbc:jtds:sqlserver://localhost:1433/YourDB' , you don't have to provide username and password.
- 下载当前的jTDS JDBC 驱动程序
- 将 jtds-xxxjar 放在您的类路径中。
- 将 ntlmauth.dll 复制到 windows/system32。根据您的硬件 x86、x64 选择 dll...
- 连接 url 是: 'jdbc:jtds:sqlserver://localhost:1433/YourDB' ,您不必提供用户名和密码。
Hope that helps.
希望有帮助。
回答by Mritunjay
If your databaseName
value is correct, then use this: DriverManger.getconnection("jdbc:sqlserver://ServerIp:1433;user=myuser;password=mypassword;databaseName=databaseName;")
如果你的databaseName
值是正确的,那么使用这个:DriverManger.getconnection("jdbc:sqlserver://ServerIp:1433;user=myuser;password=mypassword;databaseName=databaseName;")
回答by misguided
The latest JDBC MSSQL connectivity driver can be found on JDBC 4.0
可以在JDBC 4.0上找到最新的 JDBC MSSQL 连接驱动程序
The class file should be in the classpath. If you are using eclipse you can easily do the same by doing the following -->
类文件应该在类路径中。如果您使用的是 eclipse,您可以通过执行以下操作轻松地完成相同的操作 -->
Right Click Project Name --> Properties --> Java Build Path --> Libraries --> Add External Jars
右键单击项目名称--> 属性--> Java 构建路径--> 库--> 添加外部罐子
Also as already been pointed out by @Cheeso the correct way to access is jdbc:sqlserver://server:port;DatabaseName=dbname
同样正如@Cheeso 已经指出的,正确的访问方式是 jdbc:sqlserver://server:port;DatabaseName=dbname
Meanwhile please find a sample class for accessing MSSQL DB (2008 in my case).
同时,请找到一个用于访问 MSSQL DB 的示例类(在我的情况下为 2008)。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class ConnectMSSQLServer
{
public void dbConnect(String db_connect_string,
String db_userid,
String db_password)
{
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = DriverManager.getConnection(db_connect_string,
db_userid, db_password);
System.out.println("connected");
Statement statement = conn.createStatement();
String queryString = "select * from SampleTable";
ResultSet rs = statement.executeQuery(queryString);
while (rs.next()) {
System.out.println(rs.getString(1));
}
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args)
{
ConnectMSSQLServer connServer = new ConnectMSSQLServer();
connServer.dbConnect("jdbc:sqlserver://xx.xx.xx.xxxx:1433;databaseName=MyDBName", "DB_USER","DB_PASSWORD");
}
}
Hope this helps.
希望这可以帮助。
回答by Pradyumna Swain
- Download the latest JDBC Driver (i.e.
sqljdbc4.0
) from Microsoft's web site Write the program as follows:
import java.sql.*; class testmssql { public static void main(String args[]) throws Exception { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); Connection con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433; databaseName=chapter16","sa","123");//repalce your databse name and user name Statement st=con.createStatement(); ResultSet rs=st.executeQuery("Select * from login");//replace your table name while(rs.next()) { String s1=rs.getString(1); String s2=rs.getString(2); System.out.println("UserID:"+s1+"Password:"+s2); } con.close(); } }
Compile the program and set the jar classpath viz:
set classpath=C:\jdbc\sqljdbc4.jar;.;
If you have saved yourjar
file inC:\jdbc
after downloading and extracting.- Run the program and make sure your TCP/IP service is enabled. If not enabled, then follow these steps:
- Go to Start -> All Programs -> Microsoft SQL Server 2008 -> Configuration tools -> SQL Server Configuration Manager
- Expand Sql Server Network Configuration: choose your MS SQL Server Instance viz. MSQSLSERVER and enable TCP/IP.
- Restart your MS SQL Server Instance. This can be done also from the right click menu of Microsoft SQL Server Management Studio at the root level of your MS SQL server instance
sqljdbc4.0
从 Microsoft 网站下载最新的 JDBC 驱动程序(即)编写程序如下:
import java.sql.*; class testmssql { public static void main(String args[]) throws Exception { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); Connection con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433; databaseName=chapter16","sa","123");//repalce your databse name and user name Statement st=con.createStatement(); ResultSet rs=st.executeQuery("Select * from login");//replace your table name while(rs.next()) { String s1=rs.getString(1); String s2=rs.getString(2); System.out.println("UserID:"+s1+"Password:"+s2); } con.close(); } }
编译程序并设置 jar 类路径即:
set classpath=C:\jdbc\sqljdbc4.jar;.;
如果您在下载和解压缩后保存了jar
文件C:\jdbc
。- 运行该程序并确保您的 TCP/IP 服务已启用。如果未启用,请按照以下步骤操作:
- 转到开始 -> 所有程序 -> Microsoft SQL Server 2008 -> 配置工具 -> SQL Server 配置管理器
- 展开 Sql Server 网络配置:选择您的 MS SQL Server 实例即。MSQSLSERVER 并启用 TCP/IP。
- 重新启动您的 MS SQL Server 实例。这也可以从 MS SQL 服务器实例根级别的 Microsoft SQL Server Management Studio 的右键单击菜单中完成
回答by Ricardo Padua Soares
Named instances?
命名实例?
URL: jdbc:sqlserver://[serverName][\instanceName][:portNumber][;property=value]
URL: jdbc:sqlserver://[serverName][\instanceName][:portNumber][;property=value]
Note: backward slash
注意:反斜杠