java通過hibernate架構配置串連sqlsever2008

來源:互聯網
上載者:User

設定檔如下

<?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>
    <property name="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect</property>
    <property name="hibernate.connection.username">sa</property>
    <property name="hibernate.connection.password">sa123</property>
    <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
    <property name="hibernate.connection.url">jdbc:sqlserver://127.0.0.1:1433;database=hibernate</property>
    <property name="hibernate.hbm2ddl.auto">update</property>
    <property name="hibernate.format_sql">true</property>
    <property name="hibernate.show_sql">true</property>
   <mapping resource="hibernate/test/pojo/Person.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

跟進hibernate的properties 設定資料庫執行個體檔案來看。

## Microsoft Driver (not recommended!)
#hibernate.connection.driver_class com.microsoft.jdbc.sqlserver.SQLServerDriver
#hibernate.connection.url jdbc:microsoft:sqlserver://1E1;DatabaseName=test;SelectMethod=cursor
## The New Microsoft Driver
#hibernate.connection.driver_class com.microsoft.sqlserver.jdbc.SQLServerDriver
#hibernate.connection.url jdbc:sqlserver://localhost

第一種方式不被推薦。所以我嘗試用第二種方式。結果報錯了。錯誤如下,error1.

Error1
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Error calling Driver#connect

第二種方式:按照執行個體上說的沒有 資料庫的說明啊。
<property name="hibernate.connection.url">jdbc:sqlserver://127.0.0.1:1433/hibernate</property>

第一種方式:結果成功了。問題到底出在哪裡呢???
<property name="hibernate.connection.url">jdbc:sqlserver://127.0.0.1:1433;database=hibernate</property>

我想問一下hibernate推薦的串連sqlsever資料庫的方式到底是啥呢?


別附一篇  myEclipse配置hibernate執行個體串連SQL server 2008

SQL create database mySourcedb;--建庫
use mySourcedb;
create table tb_admin--建表
(
    id int identity(10001,1) primary key,
    name varchar(50),
    pwd varchar(50)
)
insert into tb_admin(name,pwd) values('admin','888888');
insert into tb_admin(name,pwd) values('manager','123456');
insert into tb_admin(name,pwd) values('sqlserver','123456');

select * from tb_admin;

--批量插入資料

declare @i int
set @i=1
while @i<10
begin
set @i=@i+1
insert into tb_admin(name,pwd) values('abc','123456')
end
以下內容在配置好myEclipse配置好JDK和tomcat後進行.

一 、配置資料來源
1、開啟MyEclipse,建立一個web工程,這裡命名為hibernate_demo

  2、開啟資料庫設定器:依次單擊【window】-->【Show View】-->【Other…】 如下圖所示:

 

3、在彈出的視窗ShowView中選擇DB Browser,如下圖所示:


4、在DB Browser視窗中,選擇顯示的表徵圖,單擊右鍵執行建立命令,如下圖示


5、彈出Database Driver對話方塊,在此會要求我們設定資料庫的相關資訊,具體設定如下圖所示,設定完成,單擊Finish.


2、 在彈出的視窗中做如下設定:


【Next】


【Next】


單擊Next,把要建立的SessionFactory放置於相應的包中,如果前面沒有設定包名,這裡要先單擊New建立新的包。

單擊【Finish】按鈕,頁面效果如下圖所示:


單擊【Add…】,在Hibernate Properties Wizard頁面填入如下圖所示資訊,最後單擊Ok。


show_sql:預設為false,如果為true,表示在程式運行時,會在控制台輸出SQL語句,這有利於跟中Hibernate的運行狀態。在開發與測試階段,可以將該屬性設定為true,以便跟蹤、偵錯工具,在應用發布以後 ,應將該屬性值設定為false,以減少應用的輸出資訊,提高運行效能。

注:屬性可以再source下開啟設定.

【第三步】添加hibernate對應檔

1、建立org.njy.bean包

2、在前面設定的資料來源上找到我們要操作的表:

在DB Browser中選中建立的資料來源,單擊滑鼠右鍵並選擇open connection..


找到剛才建立的mySourcedb資料庫,然後是TABLE,位置如下圖所示:






 3、 修改TbAdmin.hbm.xml檔案

<hibernate-mapping>
    <class name="org.wcr.bean.Abc" table="abc" schema="dbo" catalog="mySourcedb">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="assigned" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="name" length="50" />
        </property>
        <property name="pwd" type="java.lang.String">
            <column name="pwd" length="50" />
        </property>
    </class>
</hibernate-mapping>


提示:建議刪除catalog=”mySourcedb”,當修改了資料庫名的時候程式會出現錯誤(找不到對應的庫)。

4、 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">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>
        <property name="dialect">
            org.hibernate.dialect.SQLServerDialect
        </property>
        <property name="connection.url">
            jdbc:sqlserver://localhost:1124;
        </property>
        <property name="connection.username">sa</property>
        <property name="connection.password">123456</property>
        <property name="connection.driver_class">
            com.microsoft.sqlserver.jdbc.SQLServerDriver
        </property>
        <property name="myeclipse.connection.profile">
            sql server 2008
        </property>
        <!-- show_sql指定是否列印SQL語句 -->
        <property name="show_sql">true</property>
        <!-- 指定POJO的對應檔 -->
        <mapping resource="org/wcr/bean/TbAdmin.hbm.xml" />
        <mapping resource="org/wcr/bean/Abc.hbm.xml" />
    </session-factory>
</hibernate-configuration>

5、 建立一個包org.njy.test,再建立TestHibernate.java用來測試一下我們的Hibernate。

package org.wcr.test;

import org.hibernate.Transaction;
import org.wcr.bean.TbAdmin;
import org.wcr.db.HibernateSessionFactory;

public class TestHibernate {
    public static void main(String[] args)
    {
        org.hibernate.Session session = HibernateSessionFactory.getSession();  
        Transaction tx = session.beginTransaction();  
        TbAdmin admin = new TbAdmin();
        admin.setId(491752);
        admin.setName("wcr");
        admin.setPwd("123");
        try {  
            session.delete(admin);  
            tx.commit();  
            System.out.print("success!");
        } catch (Exception e) {  
            tx.rollback();  
            e.printStackTrace();  
        }finally{  
            session.close();  
        }
    }
}
在執行添加的時候控制台列印出如下語句,包括兩條sql語句:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select tbadmin_.id, tbadmin_.name as name0_, tbadmin_.pwd as pwd0_ from mySourcedb.dbo.tb_admin tbadmin_ where tbadmin_.id=?
Hibernate: delete from mySourcedb.dbo.tb_admin where id=?
success!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.