本站首页    管理页面    写新日志    退出


«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
31


公告
 本博客在此声明所有文章均为转摘,只做资料收集使用。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:1304
评论数量:2242
留言数量:5
访问次数:7593754
建立时间:2006年5月29日




[Hibernate]hibernate调用mysql5.0存储过程小记
软件技术

lhwork 发表于 2007/1/7 14:09:29

准备工作:1.hibernate3到这下载hibernate3:http://sourceforge.net/project/showfiles.phpgroup_id=40712&package_id=127784&release_id=4032232.mysql (注意一定要用mysql5.0和最新驱动) mysql官方网站http://www.mysql.com/ 500)this.width=500'> 500)this.width=500'> 1 .建张表500)this.width=500'> CREATE   TABLE  `proctest` (500)this.width=500'>  `id`  int ( 11 )  NOT   NULL  auto_increment,500)this.width=500'>  `Name`  varchar ( 20 )  default   '''''' ,500)this.width=500'>  `age`  int ( 11 )  default   NULL ,500)this.width=500'>  `address`  varchar ( 50 )  default   '' ,500)this.width=500'>   PRIMARY   KEY   (`id`)500)this.width=500'>) ENGINE = InnoDB  DEFAULT  CHARSET = gb2312;500)this.width=500'>插入几条记录500)this.width=500'> INSERT   INTO  `proctest`  VALUES  ( 1 , ' tom ' , 22 , 'http://www.blogjava.net ' );500)this.width=500'> INSERT   INTO  `proctest`  VALUES  ( 2 , ' wujun ' , 25 , 'http://www.blogjava.net/wujun ' );500)this.width=500'> INSERT   INTO  `proctest`  VALUES  ( 3 , ' jerry ' , 30 , ' 深圳 ' );500)this.width=500'> INSERT   INTO  `proctest`  VALUES  ( 4 , ' wujun ' , 40 , ' 南昌 ' );500)this.width=500'>创建存储过程500)this.width=500'> -- 这只是一个例子,就来个简单存储过程 500)this.width=500'> create   PROCEDURE  testProc()500)this.width=500'> begin 500)this.width=500'>    select   *   from  proctest;500)this.width=500'> end ; 打开eclipce新建个java工程,记的把hiberbate3类库也一起加进去..看下结构图:500)this.width=500'>1.新建UserVO.java文件 500)this.width=500'> package  net.wj.proc.vo;500)this.width=500'>500)this.width=500'>500)this.width=500'> public   class  UserVO  500)this.width=500'> {500)this.width=500'>     private   int  id;500)this.width=500'>     private  String name;500)this.width=500'>     private   int  age;500)this.width=500'>     private  String address;500)this.width=500'>     public  UserVO()500)this.width=500'>500)this.width=500'>     500)this.width=500'> {} 顺便把它相对应的配置文件也写上。。UserVO.hbm.xml  500)this.width=500'><?xml version="1.0" encoding='UTF-8'?>500)this.width=500'><!DOCTYPE hibernate-mapping PUBLIC500)this.width=500'>                            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"500)this.width=500'>                            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >500)this.width=500'>500)this.width=500'><!-- DO NOT EDIT: This is a generated file that is synchronized -->500)this.width=500'><!-- by MyEclipse Hibernate tool integration.                   -->500)this.width=500'><!-- Created Sat Apr 22 17:08:56 CST 2006                         -->500)this.width=500'><hibernate-mapping>500)this.width=500'>500)this.width=500'>    <class name="net.wj.proc.vo.UserVO" table="proctest">500)this.width=500'>        <id name="id" column="id">500)this.width=500'>            <generator class="native"/>500)this.width=500'>        </id>500)this.width=500'>        500)this.width=500'>        <property name="name" column="name" type="string" />500)this.width=500'>        <property name="age" column="age" type="integer" />500)this.width=500'>        <property name="address" column="address" type="string" />500)this.width=500'>500)this.width=500'>    </class>500)this.width=500'>500)this.width=500'>    <!--sql查询-->500)this.width=500'>     <sql-query name="select">500)this.width=500'>     <![CDATA[select {usr.*} from proctest usr ]]>500)this.width=500'>     <return alias="usr" class="net.wj.proc.vo.UserVO" />500)this.width=500'>     </sql-query>500)this.width=500'>500)this.width=500'>     <!--调用存储过程就在这里配了-->500)this.width=500'>    <sql-query name="getUser" callable="true">500)this.width=500'>     <return alias="user" class="net.wj.proc.vo.UserVO">500)this.width=500'>     500)this.width=500'>     <return-property name="id" column="id" />500)this.width=500'>      <return-property name="name" column="name" />500)this.width=500'>       <return-property name="age" column="age" />500)this.width=500'>        <return-property name="address" column="address" />500)this.width=500'>     </return>500)this.width=500'>     <!--这里就是我们刚刚创建的存储过程名-->500)this.width=500'>     {call testProc()}500)this.width=500'>     </sql-query>500)this.width=500'></hibernate-mapping>500)this.width=500'>测试代码500)this.width=500'>package net.wj.proc.test;500)this.width=500'>500)this.width=500'>500)this.width=500'>import java.util.List;500)this.width=500'>500)this.width=500'>import org.hibernate.*;500)this.width=500'>import org.hibernate.cfg.*;500)this.width=500'>import net.wj.proc.vo.*;500)this.width=500'>import org.apache.log4j.*;500)this.width=500'>500)this.width=500'>500)this.width=500'>500)this.width=500'>public class ProcTest 500)this.width=500'>{500)this.width=500'>500)this.width=500'>500)this.width=500'>    /** *//**500)this.width=500'>     * @param args500)this.width=500'>     */500)this.width=500'>    Logger log=Logger.getLogger(this.getClass());500)this.width=500'>    public ProcTest()500)this.width=500'>500)this.width=500'>    500)this.width=500'>{}500)this.width=500'>500)this.width=500'>    public static void main(String[] args) 500)this.width=500'>{500)this.width=500'>        System.out.print("start.............................");500)this.width=500'>        ProcTest tt=new ProcTest();500)this.width=500'>       // tt.LoadAll();500)this.width=500'>       // tt.ExampleSelect();500)this.width=500'>       tt.ExampleProc();500)this.width=500'>        500)this.width=500'>    }500)this.width=500'>    500)this.width=500'>    //得到Session,500)this.width=500'>    public Session  getSession()500)this.width=500'>500)this.width=500'>    500)this.width=500'>{500)this.width=500'>        try500)this.width=500'>500)this.width=500'>        500)this.width=500'>{500)this.width=500'>            Configuration cfg = new Configuration().configure();500)this.width=500'>            SessionFactory sf=cfg.buildSessionFactory();500)this.width=500'>            Session ss= sf.openSession();500)this.width=500'>            return ss;500)this.width=500'>500)this.width=500'>        }500)this.width=500'>        catch(Exception ee)500)this.width=500'>500)this.width=500'>        500)this.width=500'>{500)this.width=500'>            System.out.print("失败"+ee.getMessage());500)this.width=500'>            return null;500)this.width=500'>        }500)this.width=500'>      500)this.width=500'>    }500)this.width=500'>    //这里调我们在UserVO.hbm.xml500)this.width=500'>    //sql-query 写上的name属性getUser500)this.width=500'>    public void ExampleProc()500)this.width=500'>500)this.width=500'>    500)this.width=500'>{500)this.width=500'>        Session ss=this.getSession();500)this.width=500'>        List li=ss.getNamedQuery("getUser").list();500)this.width=500'>        for(int i=0;i<li.size();i++)500)this.width=500'>500)this.width=500'>        500)this.width=500'>{500)this.width=500'>            UserVO vo=(UserVO)li.get(i);500)this.width=500'>            log.info("name:"+vo.getName());500)this.width=500'>            log.info("age"+vo.getAge());500)this.width=500'>            log.info("address"+vo.getAddress());500)this.width=500'>        }500)this.width=500'>        ss.close();500)this.width=500'>    }500)this.width=500'>    //配置文件的sql查询500)this.width=500'>    public void ExampleSelect()500)this.width=500'>500)this.width=500'>    500)this.width=500'>{500)this.width=500'>           Session ss=this.getSession();500)this.width=500'>           List li= ss.getNamedQuery("select").list();500)this.width=500'>        500)this.width=500'>           for(int i=0;i<li.size();i++)500)this.width=500'>500)this.width=500'>           500)this.width=500'>{500)this.width=500'>            UserVO vo=(UserVO)li.get(i);500)this.width=500'>            log.info("name:"+vo.getName());500)this.width=500'>            log.info("age"+vo.getAge());500)this.width=500'>            log.info("address"+vo.getAddress());500)this.width=500'>           }500)this.width=500'>           ss.close();  500)this.width=500'>    }}记的用最新的驱动:要不然可能会报这个错 500)this.width=500'>Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not execute query500)this.width=500'>    at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:91)500)this.width=500'>    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:79)500)this.width=500'>    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)500)this.width=500'>    at org.hibernate.loader.Loader.doList(Loader.java:2148)500)this.width=500'>    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)500)this.width=500'>    at org.hibernate.loader.Loader.list(Loader.java:2024)500)this.width=500'>    at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:111)500)this.width=500'>    at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1674)500)this.width=500'>    at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:147)500)this.width=500'>    at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:164)500)this.width=500'>    at net.wj.proc.test.ProcTest.ExampleProc(ProcTest.java:45)500)this.width=500'>    at net.wj.proc.test.ProcTest.main(ProcTest.java:22)500)this.width=500'>Caused by: java.sql.SQLException: Callable statments not supported.500)this.width=500'>    at com.mysql.jdbc.Connection.prepareCall(Connection.java:1278)500)this.width=500'>    at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:439)500)this.width=500'>    at org.hibernate.jdbc.AbstractBatcher.prepareCallableQueryStatement(AbstractBatcher.java:115)500)this.width=500'>    at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1557)500)this.width=500'>    at org.hibernate.loader.Loader.doQuery(Loader.java:661)500)this.width=500'>    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)500)this.width=500'>    at org.hibernate.loader.Loader.doList(Loader.java:2145)500)this.width=500'>    500)this.width=500'> 8 more500)this.width=500'>09:38:18,837  INFO SessionFactoryImpl:153 - building session factory500)this.width=500'>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.xml500)this.width=500'>09:38:21,951  INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured500)this.width=500'>500)this.width=500'>Hibernate: 500)this.width=500'>{call testProc()}500)this.width=500'>09:38:22,482  WARN JDBCExceptionReporter:71 - SQL Error: 0, SQLState: S1C00500)this.width=500'>09:38:22,482 ERROR JDBCExceptionReporter:72 - Callable statments not supported.500)this.width=500'>源代码http://www.blogjava.net/Files/wujun/Proc.rar是不是挺简单的.


阅读全文(2087) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.414 second(s), page refreshed 144765337 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号