[英]How can I use the MS JDBC driver with MS SQL Server 2008 Express?
My configuration: 我的配置:
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?
有帮助吗?
You have the wrong URL. 您的网址错误。
I don't know what you mean by "JDBC 2005". 我不知道你的意思是“JDBC 2005”。 When I looked on the microsoft site, I found something called the Microsoft SQL Server JDBC Driver 2.0 .
当我查看微软网站时,我发现了一个名为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]
截至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. Microsoft在初始发布JDBC驱动程序后更改了类名和URL。 The URL you are using goes with the original JDBC driver from Microsoft, the one MS calls the "SQL Server 2000 version".
您使用的URL与Microsoft的原始JDBC驱动程序一起使用,一个MS称为“SQL Server 2000版本”。 But that driver uses a different classname.
但该驱动程序使用不同的类名。
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驱动程序的发行说明。
sqljdbc4.0
) from Microsoft's web site sqljdbc4.0
) 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;.;
编译程序并设置jar类
set classpath=C:\\jdbc\\sqljdbc4.jar;.;
: set classpath=C:\\jdbc\\sqljdbc4.jar;.;
If you have saved your jar
file in C:\\jdbc
after downloading and extracting. 如果在下载
C:\\jdbc
压缩后将jar
文件保存在C:\\jdbc
。
如果您的databaseName
值正确,则使用此命令: DriverManger.getconnection("jdbc:sqlserver://ServerIp:1433;user=myuser;password=mypassword;databaseName=databaseName;")
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构建路径 - >库 - >添加外部JAR
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. 希望这可以帮助。
Named instances? 命名实例?
URL: jdbc:sqlserver://[serverName][\\instanceName][:portNumber][;property=value] URL:jdbc:sqlserver:// [serverName] [\\ instanceName] [:portNumber] [; property = value]
Note: backward slash 注意:反斜杠
You can try the following. 您可以尝试以下方法。 Works fine in my case:
在我的情况下工作正常:
Hope that helps. 希望有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.