64位windows 7下安装SQL Server 2008 R2连接Tomcat出现1433端口未开启的错误

meng4411yu 2011-11-20 12:02:15
这是错误的内容:
com.microsoft.sqlserver.jdbc.SQLServerException: 通过端口 1433 连接到主机 localh
ost 的 TCP/IP 连接失败。错误:“Connection refused: connect。请验证连接属性,并检
查 SQL Server 的实例正在主机上运行,且在此端口接受 TCP/IP 连接,还要确保防火墙没
有阻止到此端口的 TCP 连接。”。
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(S
QLServerException.java:171)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLSer
verConnection.java:1033)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConne
ction.java:817)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerCon
nection.java:700)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.
java:842)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.zeng.LoginCl.doGet(LoginCl.java:27)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.jav
a:291)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcesso
r.java:877)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.pr
ocess(Http11AprProtocol.java:594)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:16
75)
at java.lang.Thread.run(Unknown Source)

注:LoginCl.java
/* @ 验证servlet */

package com.zeng;

import javax.servlet.http.*;
import java.io.*;
import javax.servlet.*;
import java.sql.*;

public class LoginCl extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res){

Connection ct=null;
Statement sm=null;
ResultSet rs=null;

try{
res.setContentType("text/html;charset=gbk");
PrintWriter pw=res.getWriter();
String u=req.getParameter("username");
String p=req.getParameter("passwd");
/* @ 连接数据库 com.microsoft.sqlserver.jdbc.SQLServerDriver*/
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//ct=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=spdb","sa","654321");

String connectionUrl = "jdbc:sqlserver://localhost:1433;"+"databaseName=spdb;user=sa;password=654321;";
ct = DriverManager.getConnection(connectionUrl);

sm=ct.createStatement();
rs=sm.executeQuery("select top 1 * from users where username='"+u+"'and passwd='"+p+"'");
//rs=sm.executeQuery("select top 1 passwd from users where username='"+"'");

if(rs.next()){
String dbPasswd=rs.getString(1);
if(dbPasswd.equals(p)){
if(rs.next()){
if(u.equals("admin")&&p.equals("123")){
/* @ 合法*/
HttpSession hs=req.getSession(true);
hs.setMaxInactiveInterval(30);
hs.setAttribute("uname",u);
res.sendRedirect("wel?uname="+u);
res.sendRedirect("wel?passw="+p);
}else{
res.sendRedirect("login");
}
}
}else{
res.sendRedirect("login?info=error");
}

/*if(rs.next()){
if(u.equals("admin")&&p.equals("123")){

HttpSession hs=req.getSession(true);
hs.setMaxInactiveInterval(30);
hs.setAttribute("uname",u);
res.sendRedirect("wel?uname="+u);
res.sendRedirect("wel?passw="+p);
}else{
res.sendRedirect("login");
}*/
}



}
catch(Exception ex){
ex.printStackTrace();
}finally{
try{
if(rs!=null){rs.close();
}
if(sm!=null){sm.close();
}
if(ct!=null){ct.close();
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}
public void doPost(HttpServletRequest req,HttpServletResponse res){
this.doGet(req,res);
}
}

这是参照网上的开启1433端口的bat文件:
@echo ========= SQL Server Ports ===================
@echo Enabling SQLServer default instance port 1433
netsh firewall set portopening TCP 1433 "SQLServer"
@echo Enabling Dedicated Admin Connection port 1434
netsh firewall set portopening TCP 1434 "SQL Admin Connection"
@echo Enabling conventional SQL Server Service Broker port 4022
netsh firewall set portopening TCP 4022 "SQL Service Broker"
@echo Enabling Transact-SQL Debugger/RPC port 135
netsh firewall set portopening TCP 135 "SQL Debugger/RPC"
@echo ========= Analysis Services Ports ==============
@echo Enabling SSAS Default Instance port 2383
netsh firewall set portopening TCP 2383 "Analysis Services"
@echo Enabling SQL Server Browser Service port 2382
netsh firewall set portopening TCP 2382 "SQL Browser"
@echo ========= Misc Applications ==============
@echo Enabling HTTP port 80
netsh firewall set portopening TCP 80 "HTTP"
@echo Enabling SSL port 443
netsh firewall set portopening TCP 443 "SSL"
@echo Enabling port for SQL Server Browser Service's 'Browse' Button
netsh firewall set portopening UDP 1434 "SQL Browser"
@echo Allowing multicast broadcast response on UDP (Browser Service Enumerations OK)
netsh firewall set multicastbroadcastresponse ENABLE
pause

这是查询端口的结果:
活动连接
协议 本地地址 外部地址 状态
TCP 127.0.0.1:1037 lenovo-PC:2239 ESTABLISHED
TCP 127.0.0.1:2239 lenovo-PC:1037 ESTABLISHED

我暂时理解的原因应该是1433端口未开启,但是我找不到开启的方法,跪求大神求解。
...全文
862 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
fangzhaoguo 2011-11-26
  • 打赏
  • 举报
回复
[Quote=引用楼主 meng4411yu 的回复:]
这是错误的内容:
com.microsoft.sqlserver.jdbc.SQLServerException: 通过端口 1433 连接到主机 localh
ost 的 TCP/IP 连接失败。错误:“Connection refused: connect。请验证连接属性,并检
查 SQL Server 的实例正在主机上运行,且在此端口接受 TCP/IP 连接,还要确保防火墙没
有阻止……
[/Quote]

Windows下使用SQL Server 2008,Web服务器不能用Tomcat吧,好象必须得用Windows的IIS吧
Internet Information Service
meng4411yu 2011-11-26
  • 打赏
  • 举报
回复
没人回复?

24,923

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Apache
社区管理员
  • Apache
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧