<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration> <session-factory>
 
  <property name="connection.username">sa</property>
  <property name="connection.password">cstp</property>
  <property name="connection.url">
   jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=househire
  </property>
  <property name="dialect">
   org.hibernate.dialect.SQLServerDialect
  </property>
  <property name="connection.driver_class">
   com.microsoft.jdbc.sqlserver.SQLServerDriver
  </property>
  <mapping resource="com/cstp/table/Hireinfo.hbm.xml" />
  <mapping resource="com/cstp/table/Users.hbm.xml" /> </session-factory></hibernate-configuration>配置有什么问题么?问什么总是报错javax.servlet.ServletException: Cannot open connection
 org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:535)
 org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:433)
 org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
 org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
 org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
 com.cstp.filter.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:26)郁闷死了~ 

解决方案 »

  1.   

    Cannot open connection
    数据没有打开,数据库驱动jar包有没。数据连接字符串对不,是不是这个数据库。
    自己看看
      

  2.   

    org.hibernate.exception.JDBCConnectionException: Cannot open connection
    org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:74)
    org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
    org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
    org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:420)
    org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144)
    org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:105)
    org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1561)
    org.hibernate.loader.Loader.doQuery(Loader.java:661)
    org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
    org.hibernate.loader.Loader.doList(Loader.java:2145)
    org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
    org.hibernate.loader.Loader.list(Loader.java:2024)
    org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:375)
    org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:308)
    org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:153)
    org.hibernate.impl.SessionImpl.list(SessionImpl.java:1106)
    org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
    com.cstp.dao.UserDao.getUserByName(UserDao.java:21)
    com.cstp.struts.action.UserAction.login(UserAction.java:35)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)
    org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:270)
    org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:187)
    org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
    org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
    org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    com.cstp.filter.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:26)还有 一部分异常
      

  3.   

    package com.cstp.factory;import org.hibernate.HibernateException;
    import org.hibernate.Session;
    import org.hibernate.cfg.Configuration;
    public class HibernateSessionFactory {  
        private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
    private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
        private  static Configuration configuration = new Configuration();
        private static org.hibernate.SessionFactory sessionFactory;
        private static String configFile = CONFIG_FILE_LOCATION; static {
         try {
    configuration.configure(configFile);
    sessionFactory = configuration.buildSessionFactory();
    } catch (Exception e) {
    System.err
    .println("%%%% Error Creating SessionFactory %%%%");
    e.printStackTrace();
    }
        }
        private HibernateSessionFactory() {
        }
        public static Session getSession() throws HibernateException {
            Session session = (Session) threadLocal.get(); if (session == null || !session.isOpen()) {
    if (sessionFactory == null) {
    rebuildSessionFactory();
    }
    session = (sessionFactory != null) ? sessionFactory.openSession()
    : null;
    threadLocal.set(session);
    }        return session;
        }
    public static void rebuildSessionFactory() {
    try {
    configuration.configure(configFile);
    sessionFactory = configuration.buildSessionFactory();
    } catch (Exception e) {
    System.err
    .println("%%%% Error Creating SessionFactory %%%%");
    e.printStackTrace();
    }
    }
        public static void closeSession() throws HibernateException {
            Session session = (Session) threadLocal.get();
            threadLocal.set(null);        if (session != null) {
                session.close();
            }
        }
    public static org.hibernate.SessionFactory getSessionFactory() {
    return sessionFactory;
    }
    public static void setConfigFile(String configFile) {
    HibernateSessionFactory.configFile = configFile;
    sessionFactory = null;
    }
    public static Configuration getConfiguration() {
    return configuration;
    }}
      

  4.   

    你的sql server如果是2000需要打sp4补丁,还需要想要的驱动
    jdts只需要一个包
    其他的要三个
    自己看看吧
      

  5.   

    这个问题 已经发现了 然后驱动的包也换了
    hibernate-configuration>
    <session-factory>

    <property name="connection.username">sa</property>
    <property name="connection.password">cstp</property>
    <property name="connection.url">
    jdbc:jtds:sqlserver://localhost:1433;DatabaseName=househire
    </property>
    <property name="dialect">
    org.hibernate.dialect.SQLServerDialect
    </property>
    <property name="connection.driver_class">
    net.sourceforge.jtds.jdbc.Driver
    </property>
    <mapping resource="com/cstp/table/Hireinfo.hbm.xml" />
    <mapping resource="com/cstp/table/Users.hbm.xml" /> </session-factory>
    但是问题依然存在