无法与Hibernate-SQL Server 2008建立与jdbc的连接

时间:2014-03-26 20:40:18

标签: java sql-server hibernate jdbc

我用Hibernate配置向导连接到SQL服务器,当我点击测试连接时,我得到了成功消息,但是当我想做hibernate映射时,我无法得到错误信息,无法建立与jdbc的连接。下面的代码是与hibernate的jdbc连接;请说我,问题是什么?

<hibernate-configuration>
 <session-factory>
<property   name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property     name="hibernate.connection.url">jdbc:sqlserver://localhost; databaseName=Test</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">sa123</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
</session-factory>
</hibernate-configuration>

最好的问候

2 个答案:

答案 0 :(得分:0)

查看代码后,我发现您没有指定数据库连接的端口号

<property     name="hibernate.connection.url">jdbc:sqlserver://localhost; databaseName=Test</property>

如果您尝试在本地计算机上进行连接,则应该有一个端口号,请参阅以下行

 <property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;DatabaseName=syv</property>  

如果还有其他问题,请检查并告知我们

好的,我发送基本的hibernate配置: -

第1步: * 创建Java文件“AddStudent”并将以下代码粘贴到其中。 *

//package code;
import java.sql.*;
import java.io.*;
import org.hibernate.*;
import org.hibernate.cfg.*;

public class AddStudent {
    private static SessionFactory sessionFactory;

    public static void main(String args[]) throws Exception {

        DataInputStream d = new DataInputStream(System.in);
        System.out.println("ENTER YOUR NAME");
        String name = d.readLine();
        System.out.println("ENTER YOUR DEGREE");
        String degree = d.readLine();
        System.out.println("ENTER YOUR PHONE");
        String phone = d.readLine();
        System.out.println("Name: " + name);
        System.out.println("Degree: " + degree);
        System.out.println("Phone: " + phone);
        if ((name.equals("") || degree.equals("") || phone.equals(""))) {
            System.out.println("All informations are Required");
        } else {
            try {
                // begin try
                sessionFactory = new Configuration().configure()
                        .buildSessionFactory();
            } catch (Exception e) {
                System.out.println(e.getMessage());
                System.err.println("Initial SessionFactory creation failed."
                        + e);
            }
            Session s = sessionFactory.openSession();
            Transaction tx = s.beginTransaction();
            Student stu = new Student();
            stu.setName(name);
            stu.setDegree(degree);
            stu.setPhone(phone);
            s.save(stu);
            tx.commit();
            System.out.println("Added to Database");
            if (s != null)
                s.close();
        }
    }
}

第2步:创建Java文件“学生”将以下代码粘贴到其中。

//package code;
import java.io.*;

public class Student implements Serializable {
    private long id;
    private String name;
    private String degree;
    private String phone;

    public long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getDegree() {
        return degree;
    }

    public String getPhone() {
        return phone;
    }

    public void setId(long string) {
        id = string;
    }

    public void setName(String string) {
        name = string;
    }

    public void setDegree(String string) {
        degree = string;
    }

    public void setPhone(String string) {
        phone = string;
    }

    public String toString() {
        return name;
    }
}

步骤3:创建一个xml文件“hibernate.cfg.xml”,将以下代码放入其中。

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory name="studentFactory">
        <property name="connection.driver_class">
            oracle.jdbc.OracleDriver
            </property>
        <property name="connection.url">
            jdbc:oracle:thin:@localhost:1521:XE
        </property>
        <property name="connection.username">
            system
        </property>
        <property name="connection.password">
            system
        </property>
        <property name="connection.pool_size">5</property>
        <!-- SQL dialect -->
        <property name="dialect">
            org.hibernate.dialect.OracleDialect
            </property>
        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">false</property>
        <mapping resource="Student.hbm.xml" />
    </session-factory>
</hibernate-configuration>

步骤4:创建一个xml文件“student.hbm.xml”并将代码放在其中。

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping >
    <class name="Student" table="Stutbl" >
         <id name="id" type="long" column ="ID">
        <generator class="increment"/>
         </id>
         <property name="name" column="name" not-null="true"/>
         <property name="degree" column="degree" />
         <property name="phone" column="phone" />
    </class>
</hibernate-mapping>

步骤5:将所需的jar添加到项目中

希望它能解决你的问题

答案 1 :(得分:-1)

添加:

    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>

    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>

    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>

并验证您的数据库是否接受连接。