技术开发 频道

Hibernate调用mysql5.0存储过程小记

   测试代码 

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

  是不是挺简单的。

0
相关文章