Hibernate:无法打开连接

时间:2016-08-10 07:17:15

标签: java mysql hibernate

我收到的消息无法打开连接,也没有生成任何特定的日志

connection cannot open

我的hibernate.cfg.xml是: -

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_oar</property>
    <property name="hibernate.connection.user"> root</property>
    <property name="hibernate.connection.password">dinga</property>

    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.hbm2ddl.auto">create</property>
    <property name="use_sql_comments">true</property>
    <property name="hibernate.validator.apply_to_ddl">false</property>
    <property name="hibernate.validator.autoregister_listeners">false</property>
    <property name="minPoolSize"> 5</property>
    <property name="maxPoolSize">100</property>

    <property name="initialPoolSize">5</property>
    <property name="validateConnectionOnBorrow">true</property>
    <property name="maxStatements">10</property>

    <mapping class="com.model.Student" />
</session-factory>

我正在生成会话

public class DataAccessObject {
private static Session session = null;

    public static Session getSession() {

        AnnotationConfiguration cfg = new AnnotationConfiguration().configure();
        //cfg.addAnnotatedClass(hello.Message.class);
        SessionFactory annotedSessionFactory = cfg.buildSessionFactory();
        Session session = annotedSessionFactory.openSession();
        return session;
    }
}

我正在使用session从mysql数据库中检索数据

public class CrudDao {

private Session session;
public CrudDao() {
    session = DataAccessObject.getSession();
}

public List<Student> getAllStudents() {

    Transaction tx = null;
    Query query = null;

    List<Student> students = new ArrayList();
    String myquery = "select s.studentId,s.name,s.department,s.emailId,s.status from Student s";
    try {
        query = session.createQuery(myquery);

        List<Student> list = query.list();
        System.out.println(list);
        Iterator<Student> itr = list.iterator();
        Student newstudent = new Student();
        while (itr.hasNext()) {
            Student mystudent = itr.next();
            newstudent.setStudentId(mystudent.getStudentId());

            newstudent.setName(mystudent.getName());
            newstudent.setDepartment(mystudent.getDepartment());
            newstudent.setEmailId(mystudent.getEmailId());
            newstudent.setStatus(mystudent.getStatus());
            students.add(newstudent);
            System.out.println(students);
        }
    } catch (HibernateException e) {
        System.err.println(e.getMessage());
    } finally {
        session.close();
    }
    return students;
}
}

0 个答案:

没有答案