日期:2014-05-20  浏览次数:21005 次

到主机 的 TCP/IP 连接失败java.net.ConnectException: Connection refused: connect
我用的sqlserver2005

Java code

public class ConnDB 
{
    public Connection conn=null;
    public Statement stmt=null;
    public ResultSet rs=null;
    private static String propFileName="/com/connDB.properties";
    private static Properties prop=new Properties();
    private static String dbClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver";//com.microsoft.jdbc.sqlserver.SQLServerDriver
    private static String dbUrl="jdbc:sqlserver://127.0.0.1:1422;DatabaseName=OnLineExam";
    private static String dbUser="sa";
    private static String dbPwd="123";
    
    public static Connection getConnection()
    {
        Connection conn=null;
        try
        {
            Class.forName(dbClassName).newInstance();
            conn=DriverManager.getConnection(dbUrl,dbUser,dbPwd);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        if(conn==null)
        {
            System.err.println("数据库连接失败!");
        }
        return conn;
    }
}




以下是异常代码
Java code


com.microsoft.sqlserver.jdbc.SQLServerException: 到主机  的 TCP/IP 连接失败。 java.net.ConnectException: Connection refused: connect
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(Unknown Source)
    at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(Unknown Source)
    at java.sql.DriverManager.getConnection(DriverManager.java:582)
    at java.sql.DriverManager.getConnection(DriverManager.java:185)
    at com.hs.core.ConnDB.getConnection(ConnDB.java:40)
    at com.hs.core.ConnDB.excuteQuery(ConnDB.java:56)
    at com.hs.dao.StudentDAO.checkStudent(StudentDAO.java:17)
    at com.hs.action.StudentAction.studentLogin(StudentAction.java:64)
    at com.hs.action.StudentAction.execute(StudentAction.java:26)
    at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
    at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
    at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.hs.core.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:39)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Thread.java:619)
数据库连接失败!
2010-2-24 17:00:37 org.apache.struts.action.RequestProcessor processException
警告: Unhandled Exception thrown: class java.lang.NullPointerException
2010-2-24 17:00:37 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet action threw exception
java.lang.NullPointerException
    at com.hs.core.ConnDB.excuteQuery(ConnDB.java:57)
    at com.hs.dao.StudentDAO.checkStudent(StudentDAO.java:17)
    at com.hs.action.StudentAction.studentLogin(StudentAction.java:64)
    at com.hs.action.StudentAction.execute(StudentAction.java:26)
    at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
    at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
    at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.hs.core.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:39)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Thread.java:619)