【问题标题】:Can not established a connection to jdbc with Hibernate-SQL Server 2008无法使用 Hibernate-SQL Server 2008 建立与 jdbc 的连接
【发布时间】:2014-03-26 20:40:17
【问题描述】:

我使用 Hibernate 配置向导连接到 SQL Server,当我单击测试连接时,我收到成功消息,但是当我想进行休眠映射时,我无法收到错误消息,无法建立与 jdbc 的连接。下面的代码是jdbc与hibernate的连接;请告诉我,有什么问题?

<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>

最好的问候

【问题讨论】:

  • 请粘贴日志
  • 当我想在新的休眠映射中进行映射->选择映射类时,出现错误:“无法连接。无法建立与 jdbc 的连接”

标签: java sql-server hibernate jdbc


【解决方案1】:

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

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

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

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

请检查并让我知道...如果有任何进一步的问题

好的,我在这里给你发送基本的休眠配置:-

第 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 文件“Student”,将以下代码粘贴到其中。

//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 添加到您的项目中

希望它能解决你的问题

【讨论】:

  • 您是否在 WEB-INF/lib 文件夹中包含了正确的 jdbc.jar,以便您的应用程序能够与数据库建立连接
  • 谢谢,我的应用程序是java应用程序,我将jar文件“sqljdbc4.jar”添加到我的项目中。
  • 请发布您在运行应用程序时遇到的异常日志...
  • 当我想在新的休眠映射中进行映射->选择映射类时,出现错误:“无法连接。无法建立与 jdbc 的连接”
  • 您是否使用任何插件来创建与类的映射?
【解决方案2】:

添加这个:

    <!-- 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>

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-24
    • 1970-01-01
    • 1970-01-01
    • 2014-10-16
    • 1970-01-01
    • 1970-01-01
    • 2018-06-04
    • 1970-01-01
    相关资源
    最近更新 更多