Error while performing database login with the sqljdbc driver:Unable to create connection. Check your URL.

时间:2023-03-09 09:25:42
Error while performing database login with the sqljdbc driver:Unable to create connection. Check your URL.

从微软官网下载jdbc驱动包sqljdbc4,运行sqljdbc_4.0.2206.100_chs.exe,将驱动包解压到了Microsoft JDBC Driver 4.0 for SQL Server文件夹下,在Microsoft JDBC Driver 4.0 for SQL Server\sqljdbc_4.0\chs\auth下有两个文件夹x64和x86,这两个文件夹下都有一个sqljdbc_auth.dll文件,将x64文件夹里面的sqljdbc_auth.dll文件拷贝到windows\system32下,将x86文件夹下的sqljdbc_auth.dll拷贝到windows\sysWOW64下,为什么这样做?

这里稍稍科普下,在32位系统的windows目录下是只有System32文件夹没有sysWOW64文件夹的,System32下面主要存放着32位程序运行所需的库文件,用于营造32位程序的运行环境,但64位系统下的System32下面则存放着64位程序运行所需的库文件,营造的是64位程序运行环境,为了兼容32位程序,微软在windows目录下添加了一个sysWOW64文件夹,sysWOW64文件夹里面存放的是营造32位程序运行环境的文件。所以,在我的电脑中,上面x64文件夹下的sqljdbc_auth.dll得放在system32下,而x86文件夹的sqljdbc_auth.dll应该放在sysWOW64文件夹里。

然后,创建系统环境变量CLASSPATH,将sqljdbc4.jar所在路径加在了CLASSPATH的值里面,不过后来证明这一步在本次实践中没有作用,属于多余一步,后续的实践中会不会用到就不知道了。

启动MyEclipse,在window-Preferences—MyEclipse-Database Drivers中打开DB Browser窗口

Error while performing database login with the sqljdbc driver:Unable to create connection. Check your URL.

Error while performing database login with the sqljdbc driver:Unable to create connection. Check your URL.

在上面红色方框圈出的DB Browser窗口空白处单击右键,选择new,打开如下窗口

Error while performing database login with the sqljdbc driver:Unable to create connection. Check your URL.

Driver template选择Microsoft SQL Server,

Driver name随便填,

Connection URL: jdbc:microsoft:sqlserver://localhost:1433

Username:sa

Password填你自己的sa账户的密码

然后点击“ADD JARS”按钮,将sqljdbc4.jar添加上来,

下面的Driver classname会在添加sqljdbc4.jar后自动生成

填好后如下图

Error while performing database login with the sqljdbc driver:Unable to create connection. Check your URL.

然后就可以测试下驱动行不行,点击Test Driver按钮,输入密码

Error while performing database login with the sqljdbc driver:Unable to create connection. Check your URL.

OK,但却提示出错了,错误提示如下:

Error while performing database login with the sqljdbc driver:Unable to create connection. Check your URL.

上面说是URL的问题,我百度了一下这个问题,发现还真是URL的问题,上面的URL应该是

jdbc:sqlserver://localhost:1433

至于原因,好像是说什么来着,我也不记得了。

改了之后在Test Driver,成功了

Error while performing database login with the sqljdbc driver:Unable to create connection. Check your URL.

然后就可以点Finish了。完成之后DB Browser里面多了一个sqljdbc,我在上面的Driver name中填的是sqljdbc,所以这里多出一个sqljdbc。

Error while performing database login with the sqljdbc driver:Unable to create connection. Check your URL.

嗯,这个是照以前配置MySQL数据库的时候的步骤配置的,后来证明,这一步好像其实也是多余的,在本次实践中没起到实际作用。

到这里准备工作基本完成,下面开始建项目写代码。

启动MyEclipse,新建web project,建好后,右键单击项目名,选择Buildpath-Add External Archives,选择添加sqljdbc4.jar

Error while performing database login with the sqljdbc driver:Unable to create connection. Check your URL.

然后Referenced Libraries目录下就会出现sqljdbc4.jar,这就是导入sqljdbc驱动包的过程,不过后来我发现这一步也是多余的,也压根没起作用。

Error while performing database login with the sqljdbc driver:Unable to create connection. Check your URL.

到此,我已经做了三件多余的事情了,第一次是添加CLASSPAT系统H环境变量,第二次是在MyEclipse中以交互式方式添加sqljdbc驱动,第三次就是导入sqljdbc包。

小白的奋斗史上充满艰辛!

然后才是代码。

给index.jsp添加sql包引用,pageEncoding设置成“utf-8”

Error while performing database login with the sqljdbc driver:Unable to create connection. Check your URL.

在body里面添加代码

<%

try{

Connection Con;

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

Con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=master","sa","123456");

System.out.println("Step1 goes well");

}

catch(Exception e){

System.out.println("Step1 down");

e.printStackTrace();

}

%>

保存,部署到tomcat7.x,然后启动tomcat7.x,启动MyEclipse浏览器,浏览我们的index.jsp页面,发现出错,报ClassNotFoundException。

在走了很多弯路(这些弯路都是无意义的弯路就不记录了)之后,最后终于确定,应该将sqljdbc4.jar拷贝到tomcat路径下wepaap下项目路径的web-inf\lib下才行

Error while performing database login with the sqljdbc driver:Unable to create connection. Check your URL.

然后重启tomcat,访问index.jsp,提示连接成功。

然后就是新建项目,删除之前添加的sqljdbc驱动、删除CLASSPATH环境变量,这次也不Add External Archives,直接写代码,拷贝sqljdbc4.jar到上面说的web-inf\lib目录下,测试连接,连接成功,说明了之前那三步都是无用功。