hibernate 连接sqlserver 2008

本文介绍了如何使用Hibernate连接SQL Server 2008数据库,包括Customer对象定义、Customer.cfg.xml和hibernate.cfg.xml的配置,以及验证配置正确性的代码示例。在配置过程中,遇到不支持服务器版本的问题,解决方法是删除sqljdbc.jar,保留sqljdbc4.jar。
摘要由CSDN通过智能技术生成

1、首先是跟表对应的对象

package test;

public class Customer {
    private Integer id;
    private String customerName;
    private String address;
    private String zipCode;
    private String linkMan;
    private String phone;
    private String mobilePhone;
    private String email;
    private String remark;
    private int active;
    
    public Customer() {
        super();
    }
    @Override
    public String toString() {
        
        return "ID: "+getId()+" CustomerName: "+getCustomerName();
    }
    
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getCustomerName() {
        return customerName;
    }
    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getZipCode() {
        return zipCode;
    }
    public void setZipCode(String zipCode) {
        this.zipCode = zipCode;
    }
    public String getLinkMan() {
        return linkMan;
    }
    public void setLinkMan(String linkMan) {
        this.linkMan = linkMan;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getMobilePhone() {
        return mobilePhone;
    }
    public void setMobilePhone(String mobilePhone) {
        this.mobilePhone = mobilePhone;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getRemark() {
        return remark;
    }
    public void setRemark(String remark) {
        this.remark = remark;
    }
    public int getActive() {
        return active;
    }
    public void setActive(int active) {
        this.active = active;
    }    

}

2、接着配置对应的Customer.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="test">
    <class name="Customer" table="Customer">
        <id name="id">
            <generator class="native"></generator>
        </id>
        <property name="customerName" />
        <property name="address" />
        <property name="zipCode" />
        <property name="linkMan" />
        <property name="phone" />
        <property name="mobilePhone" />
        <property name="email" />
        <property name="remark" />
        <property name="active" />
    </class>
</hibernate-mapping>

3、配置hibernate.cfg.xml

<?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">
<hibernate-configuration>

<session-factory>
<!--sql方言 -->
    <property name="dialect">
        org.hibernate.dialect.SQLServerDialect
    </property>
    <property name="connection.url">
        jdbc:sqlserver://localhost:1433;database=test
    </property>
    <property name="connection.username">sa</property>
    <property name="connection.password">123456</property>

<!--sql驱动 -->

    <property name="connection.driver_class">
        com.microsoft.sqlserver.jdbc.SQLServerDriver
    </property>
    <property name="show_sql">true</property>
    <property name="format_sql">false</property>

    <mapping resource="test/Customer.hbm.xml" />
</session-factory>

</hibernate-configuration>

4、最后写一个类,验证是否配置正确,连接到数据库

package test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;


public class App {
    public static void main(String[] args) {
        Configuration cfg=new Configuration();
        SessionFactory sf=cfg.configure().buildSessionFactory();        
        Session s=sf.openSession();
        Transaction tx= s.beginTransaction();
        
        Customer customer=(Customer) s.get(Customer.class, 1);
        System.out.println(customer.toString());    
    }
}

5、添加的hibernate需要的包如下图:

注意一点:原本添加了sqlServer的驱动jar包两个:sqljdbc.jar和sqljdbc4.jar,

结果报错: com.microsoft.sqlserver.jdbc.SQLServerException: 不支持此服务器版本。目标服务器必须是 SQL Server 2000 或更高版本。

解决方案:去掉sqljdbc.jar就好了。

运行结果如下图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值