http://blog.163.com/jackie_howe/blog/static/19949134720125173539380/

Eclipse连接SQL Server 2008数据库
在连接数据库之前必须保证SQL Server 2008是采用SQL Server身份验证方式而不是windows身份验证方式。如果在安装时选用了后者,则重新设置如下:
http://blog.163.com/jackie_howe/blog/static/19949134720122261121214/
SQL Server 2008装好后,默认协议是没有开启的,可打开配置工具下的 SQL Server配置管理器中开启,如下2图:
保证SQL Server 2008是采用SQL Server身份验证方式下开始配置:
一、SQL Server 2008的配置:
1、安装好SQL Server 2008后,运行 开始 → 所有程序 → Microsoft SQL Server 2008 → 配置工具 → SQL Server配置管理器,在打开的窗口的左边找到 MSSQLSERVER的协议,在右边右单击 TCP/IP,选择 已启用。如果Named Pipes 未启用也设为启用。如图5所示:
图5
2、双击图5右边的 TCP/IP,在弹出的窗口中选择 IP地址 标签,把 IPAll 中的 TCP端口 设成 1433,并将上方所有的 已启用 选项设置成 是。如图6所示:
图6
4、然后重启数据库,重启完毕后,接下来使用命令测试1433端口是否打开。
5、开始菜单 → 运行cmd → 在命令提示符下输入:telnet 127.0.0.1 1433,(注意telnet与127之间有空格,1与1433之间有空格)若提示“不能打开到主机的连接,在端口 1433: 连接失败”,则说明1433端口没有打开,需要重新进行以上配置。若连接成功,显示如图8所示:
图8
注:Windows7如何开启telnet服务http://blog.163.com/jackie_howe/blog/static/199491347201251723939691/
二、环境变量CLASSPATH配置:
1、下载Microsoft JDBC Driver for SQL Server
http://www.microsoft.com/zh-cn/download/details.aspx?id=11774 (最新版4.0)
http://www.microsoft.com/zh-cn/download/details.aspx?id=21599 (3.0版)
http://www.microsoft.com/zh-cn/download/details.aspx?id=2505 (2.0版)
下载JDBC的驱动文件,例如下载得到的文件是sqljdbc_4.0.2206.100_chs.exe,解压文件。设将其解压到C:\Microsoft JDBC Driver 4.0 for SQL Server目录下。
2、在桌面上右击 我的电脑,依次选择 属性 → 高级 → 环境变量,在 系统变量中双击 CLASSPATH变量,追加“;C:\Microsoft JDBC Driver 4.0 for SQL Server\sqljdbc_4.0\chs\sqljdbc4.jar”若不存在应当新建CLASSPATH变量,并且将其值设为“C:\Microsoft JDBC Driver 4.0 for SQL Server\sqljdbc_4.0\chs\sqljdbc4.jar”。如图9示:
图9
3、连续点击 确定 以退出环境变量配置。
三、使用Eclipse测试连接SQL Server2008数据库:
1、打开SQL Server 2008,在其中新建数据库 Test,然后退出SQL Server 2008。
2、运行Eclipse,新建一个Java Project 名为 Test。
3、右单击工程名,依次选择 Build Path → Configure Build Path,在打开的窗口的右边选择 Libraries标签,然后单击 Add External JARs,找到 sqljdbc4jar 文件并打开,然后单击 OK 完成构建路径的配置。如图10
图10
提示:也可以把sqljdbc4jar类 放到jre/lib/ext下,并放到tomcat/lib下
这样就可以应用到所有jsp文件,减少对每个项目进行sqljdbc4jar的加载;
4、在Test中新建包pkg,在pkg中新建一个类Main,在其中输入代码如下:
package pkg;
}
import java.sql.*;
public class Main {
public static void main(String [] args)
{
String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=填写你的数据库名";
String userName="填写你的用户名,我的是sa";
String userPwd="填写你的密码";
try
{
Class.forName(driverName);
System.out.println("加载驱动成功!");
}catch(Exception e){
e.printStackTrace();
System.out.println("加载驱动失败!");
}
try{
Connection dbConn=DriverManager.getConnection(dbURL,userName,userPwd);
System.out.println("连接数据库成功!");
}catch(Exception e)
{
e.printStackTrace();
System.out.print("SQL Server连接失败!");
}
}
5、点击右键,选择run as——>Java Application,出现下图11则连接成功!
图11
历史上的今天
评论
点击登录|昵称:取消运行结果如下,该怎么改:
加载驱动成功!
SQL Server连接失败!com.microsoft.sqlserver.jdbc.SQLServerException: 通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败。错误:“Connection refused: connect。请验证连接属性。确保 SQL Server 的实例正在主机上运行,且在此端口接受 TCP/IP 连接,还要确保防火墙没有阻止到此端口的 TCP 连接。”。

回复
该改的我都改了,但是出现这样的错误
加载驱动成功!
SQL Server连接失败!com.microsoft.sqlserver.jdbc.SQLServerException: 通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败。错误:“Connection refused: connect。请验证连接属性。确保 SQL Server 的实例正在主机上运行,且在此端口接受 TCP/IP 连接,还要确保防火墙没有阻止到此端口的 TCP 连接。”。

回复





加载驱动成功!com.microsoft.sqlserver.jdbc.SQLServerException: 用户 '填写你的用户名,我的是sa' 登录失败。 ClientConnectionId:f9fe365a-2f17-452d-9734-9799a1f2010cat com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:254)at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:84)at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:2908)
求解答
回复

回复

回复



回复


回复


回复








回复

at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at pkg.Main.m回复


所有上面的东西都好了,但是运行程序的时候出现了下面的异常,求解
Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)

你就是我的再生父母啊,纠结了3天的问题,今天看了你的博客,终于解决了,不易啊………………
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError
at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF
at com.microsoft.sqlserver.jdbc.TDSParser.parse
at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon
at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000
at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4575)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect
at java.sql.DriverManager.getConnection(Unknown Source)
回复


at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at pkg.Main.main(Main.java:13)
这几个错误怎么解决哦,朋友~谢谢了哈,感谢,研究好几天连接数据库了,网上各种版本我都用不了~楼下说的"DatabaseName=Test",这个问题我也想清楚,感觉应该是Test,但改了之后错误还是显示以上~望能解决,感谢
回复
前面建了数据库是Test 这里DatabaseName应该是Test吧回复

请教是不是可以不用追加?回复

提示:也可以把sqljdbc4jar类 放到jre/lib/ext下,并放到tomcat/lib下这样就可以应用到所有jsp文件,减少对每个项目进行sqljdbc4jar的加载;
是再次考到这两个目录下吗?之前的配置有影响吗??
回复

回复


我配置的时候前面全部通过了,但是就是把代码弄上去之后出现很多问题,不知道问题出在哪里
回复
回复

Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at test1.main(test1.java:9)

上面的(Native Method)是可以点击的链接。点击第一个“Native Method”时,出现“找不到源,源代码连接不包含文件ClassLoader.class的源代码。可以通过单击下面的“更改连接的源代码”来更改源代码连接”;其他几个“Native Method”点击后也是类似。at test1.main(test1.java:9这句指向 Class.forName(driverName); 代码如下:
import java.sql.*;
public class test1{
public static void main(String[] args) {
String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=学生管理系统";
String userName="tanliyuan"; //默认用户名
String userPwd="123456"; //密码
try{
Class.forName(driverName);



回复


回复

回复
连接失败
回复



回复上一页1...-1-1-1-1-1-1-1...-1下一页