Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2493097
  • 博文数量: 709
  • 博客积分: 12251
  • 博客等级: 上将
  • 技术积分: 7905
  • 用 户 组: 普通用户
  • 注册时间: 2005-07-17 00:00
个人简介

实现有价值的IT服务

文章存档

2012年(7)

2011年(147)

2009年(3)

2008年(5)

2007年(74)

2006年(431)

2005年(42)

分类: IT职场

2006-07-12 16:30:03


准备工作:
1.hibernate3
到这下载hibernate3:
2.mysql (注意一定要用mysql5.0和最新驱动)
 mysql官方网站


1 .建张表
CREATE   TABLE  `proctest` (
  `id` 
int ( 11 NOT   NULL  auto_increment,
  `Name` 
varchar ( 20 default   '''''' ,
  `age` 
int ( 11 default   NULL ,
  `address` 
varchar ( 50 default   '' ,
  
PRIMARY   KEY   (`id`)
) ENGINE
= InnoDB  DEFAULT  CHARSET = gb2312;
插入几条记录
INSERT   INTO  `proctest`  VALUES  ( 1 , ' tom ' , 22 , 'http://www.blogjava.net ' );
INSERT   INTO  `proctest`  VALUES  ( 2 , ' wujun ' , 25 , 'http://www.blogjava.net/wujun ' );
INSERT   INTO  `proctest`  VALUES  ( 3 , ' jerry ' , 30 , ' 深圳 ' );
INSERT   INTO  `proctest`  VALUES  ( 4 , ' wujun ' , 40 , ' 南昌 ' );
创建存储过程
-- 这只是一个例子,就来个简单存储过程
create   PROCEDURE  testProc()
begin
   
select   *   from  proctest;
end ;



打开eclipce新建个java工程,记的把hiberbate3类库也一起加进去..
看下结构图:


1.新建UserVO.java文件

package  net.wj.proc.vo;

public   class  UserVO  {
    
private   int  id;
    
private  String name;
    
private   int  age;
    
private  String address;
    
public  UserVO()
    
{}
顺便把它相对应的配置文件也写上。。
UserVO.hbm.xml 

xml version="1.0" encoding='UTF-8'?>
DOCTYPE hibernate-mapping PUBLIC
                            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                            "" 
>




<hibernate-mapping>

    
<class name="net.wj.proc.vo.UserVO" table="proctest">
        
<id name="id" column="id">
            
<generator class="native"/>
        
id>
        
        
<property name="name" column="name" type="string" />
        
<property name="age" column="age" type="integer" />
        
<property name="address" column="address" type="string" />

    
class>

    

     
<sql-query name="select">
     
select {usr.*} from proctest usr ]]>
     
<return alias="usr" class="net.wj.proc.vo.UserVO" />
     
sql-query>

     

    
<sql-query name="getUser" callable="true">
     
<return alias="user" class="net.wj.proc.vo.UserVO">
     
     
<return-property name="id" column="id" />
      
<return-property name="name" column="name" />
       
<return-property name="age" column="age" />
        
<return-property name="address" column="address" />
     
return>
     

     {call testProc()}
     
sql-query>
hibernate-mapping>

测试代码
package net.wj.proc.test;


import java.util.List;

import org.hibernate.*;
import org.hibernate.cfg.*;
import net.wj.proc.vo.*;
import org.apache.log4j.*;


public class ProcTest {

    
/**
     * 
@param args
     
*/

    Logger log
=Logger.getLogger(this.getClass());
    
public ProcTest()
    
{}
    
public static void main(String[] args) {
        System.out.print(
"start.............................");
        ProcTest tt
=new ProcTest();
       
// tt.LoadAll();
       
// tt.ExampleSelect();
       tt.ExampleProc();
        
    }

    
    
//得到Session,
    public Session  getSession()
    
{
        
try
        
{
            Configuration cfg 
= new Configuration().configure();
            SessionFactory sf
=cfg.buildSessionFactory();
            Session ss
= sf.openSession();
            
return ss;

        }

        
catch(Exception ee)
        
{
            System.out.print(
"失败"+ee.getMessage());
            
return null;
        }

      
    }

    
//这里调我们在UserVO.hbm.xml
    
//sql-query 写上的name属性getUser
    public void ExampleProc()
    
{
        Session ss
=this.getSession();
        List li
=ss.getNamedQuery("getUser").list();
        
for(int i=0;i<li.size();i++)
        
{
            UserVO vo
=(UserVO)li.get(i);
            log.info(
"name:"+vo.getName());
            log.info(
"age"+vo.getAge());
            log.info(
"address"+vo.getAddress());
        }

        ss.close();
    }

    
//配置文件的sql查询
    public void ExampleSelect()
    
{
           Session ss
=this.getSession();
           List li
= ss.getNamedQuery("select").list();
        
           
for(int i=0;i<li.size();i++)
           
{
            UserVO vo
=(UserVO)li.get(i);
            log.info(
"name:"+vo.getName());
            log.info(
"age"+vo.getAge());
            log.info(
"address"+vo.getAddress());
           }

           ss.close();  
    }
}


记的用最新的驱动:
要不然可能会报这个错
Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not execute query
    at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:
91)
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:
79)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:
43)
    at org.hibernate.loader.Loader.doList(Loader.java:
2148)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:
2029)
    at org.hibernate.loader.Loader.list(Loader.java:
2024)
    at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:
111)
    at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:
1674)
    at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:
147)
    at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:
164)
    at net.wj.proc.test.ProcTest.ExampleProc(ProcTest.java:
45)
    at net.wj.proc.test.ProcTest.main(ProcTest.java:
22)
Caused by: java.sql.SQLException: Callable statments not supported.
    at com.mysql.jdbc.Connection.prepareCall(Connection.java:
1278)
    at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:
439)
    at org.hibernate.jdbc.AbstractBatcher.prepareCallableQueryStatement(AbstractBatcher.java:
115)
    at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:
1557)
    at org.hibernate.loader.Loader.doQuery(Loader.java:
661)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:
224)
    at org.hibernate.loader.Loader.doList(Loader.java:
2145)
     
8 more
09:38:18,837  INFO SessionFactoryImpl:153 - building session factory
09:38:18,917  WARN Configurator:126 - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/E:/lib/hibernate3/ehcache-1.1.jar!/ehcache-failsafe.xml
09:38:21,951  INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured
Hibernate: 
{call testProc()}
09:38:22,482  WARN JDBCExceptionReporter:71 - SQL Error: 0, SQLState: S1C00
09:38:22,482 ERROR JDBCExceptionReporter:72 - Callable statments not supported.

源代码http://www.blogjava.net/Files/wujun/Proc.rar

是不是挺简单的.
阅读(1422) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~