Hibernate 不同数据库的连接及SQL方言
<!--MySql 驱动程序 eg. mysql-connector-java-5.0.4-bin.jar-->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property> <!-- JDBC URL -->
<property name="connection.url">jdbc:mysql://localhost/dbname?characterEncoding=gb2312</property> <!-- 数据库用户名-->
<property name="connection.username">root</property> <!-- 数据库密码-->
<property name="connection.password">root</property>
<!--Sql Server 驱动程序 eg. jtds-1.2.jar-->
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property> <!-- JDBC URL -->
<property name="connection.url">jdbc:jtds:sqlserver://localhost:1433;DatabaseName=dbname</property> <!-- 数据库用户名-->
<property name="connection.username">sa</property> <!-- 数据库密码-->
<property name="connection.password"></property>
<!--Oracle 驱动程序 ojdbc14.jar-->
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <!-- JDBC URL -->
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:dbname</property> <!-- 数据库用户名-->
<property name="connection.username">test</property> <!-- 数据库密码-->
<property name="connection.password">test</property>如果出现如下错误,则可能是Hibernate SQL方言 (hibernate.dialect)设置不正确。
Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]'last_insert_id' 不是可以识别的 函数名。
<!--MySql 驱动程序 eg. mysql-connector-java-5.0.4-bin.jar-->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property> <!-- JDBC URL -->
<property name="connection.url">jdbc:mysql://localhost/dbname?characterEncoding=gb2312</property> <!-- 数据库用户名-->
<property name="connection.username">root</property> <!-- 数据库密码-->
<property name="connection.password">root</property>
<!--Sql Server 驱动程序 eg. jtds-1.2.jar-->
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property> <!-- JDBC URL -->
<property name="connection.url">jdbc:jtds:sqlserver://localhost:1433;DatabaseName=dbname</property> <!-- 数据库用户名-->
<property name="connection.username">sa</property> <!-- 数据库密码-->
<property name="connection.password"></property>
<!--Oracle 驱动程序 ojdbc14.jar-->
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <!-- JDBC URL -->
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:dbname</property> <!-- 数据库用户名-->
<property name="connection.username">test</property> <!-- 数据库密码-->
<property name="connection.password">test</property>如果出现如下错误,则可能是Hibernate SQL方言 (hibernate.dialect)设置不正确。
Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]'last_insert_id' 不是可以识别的 函数名。
解决方案 »
- 如何学好ssh
- 关于jcombobox的问题,无法实现,求大师,
- 自己写了三个框架,烦劳大家辛苦下载测试一下并小弟弟一点介意,是关于IOC,WEB,ORM
- hiberante
- 请教大虾:SPRING异常,不明白!!在线等
- 如何安装安全管理器?如何引导程序命名服务?
- 为什么hibernate自动生成PO其date类型用java.util.Date,可以用java.sql.Date吗?会不什么影响吗?谢了!
- action的弱智问题
- Java同步块问题
- 大神们帮忙解答下添加在hashset中的元素为什么要实现hashcode和equals方法
- tomcat虚拟目录问题,紧急求助!
- 关于socketChannel的使用的问题
可以在HQL中使用函数,经常使用的函数如下:
count():统计记录条数。
min():求最小值。
max():求最大值。
sum():求和。
avg():求平均值。
例如,要取得Student实例的数量,可以编写如下HQL语句:
select count(*) from Student
取得Student平均年龄的HQL语句:
select avg(s.age) from Student as s
可以使用distinct去除重复的数据:
select distinct s.age from Student as s
从第2万条开始取出100条记录 [code:1]Query q = session.createQuery("from Cat as c");
q.setFirstResult(20000);
q.setMaxResults(100);
List l = q.list();[/code:1]
你试试在Hibernate中就你那样可以吗?来点儿实践的让俺开开眼,你上面的那我也知道.
return ((Integer)orderList.iterate().next()).longValue();或者
return ((Integer)orderList.uniqueResult()).intValue();
可以在HQL中使用函数,经常使用的函数如下:
count():统计记录条数。
min():求最小值。
max():求最大值。
sum():求和。
avg():求平均值。
例如,要取得Student实例的数量,可以编写如下HQL语句:
select count(*) from Student
取得Student平均年龄的HQL语句:
select avg(s.age) from Student as s
可以使用distinct去除重复的数据:
select distinct s.age from Student as s
throws HibernateException {
String method = "countOrderHistory";
Session session = null; try {
String sql = "SELECT COUNT(*) FROM order_history as oh JOIN meal as m ON oh.muid = m.muid WHERE oh.expired = "
+ Constant.ORDERHISTORY_EXPIRED
+ " and oh.created_date >= "
+ startTime
+ " and oh.created_date <=" + endTime; session = HibernateUtil.currentSession(); SQLQuery q = session.createSQLQuery(sql); List list = q.list(); return ((BigInteger) list.get(0)).intValue(); } catch (HibernateException e) {
error("", method, "hibernate exception", e);
throw e;
}
}
第二个
SELECT count(*) FROM Emp 可以得到总行数
你说的是这样的吧.
Query query=session.createQuery(" select count(*) from map.manytoone.Student");
query.executeUpdate();你试试可以吗?
比如:select * from 数据库1.数据表1 where 数据库1.数据表1.userID=数据库二.数据表2.usernameID;
上面我是经SQL的语句表达出来了。
我想如果用HQL表达如何表达出来?2.在Hibernate中如何统计总记录数.用类似的 count(*)实现怎么作?(必须使用HQL语句就一个统计操作有那些方法). -----------------------------------------
等待中………………
我用的:
Query query=session.createQuery(" select count(*) from map.manytoone.Student");
query.executeUpdate();
出现如下情况:
--------------------------------------------------------------------------------------org.hibernate.HibernateException: Not supported for select queries
at org.hibernate.hql.ast.QueryTranslatorImpl.errorIfSelect(QueryTranslatorImpl.java:260)
at org.hibernate.hql.ast.QueryTranslatorImpl.executeUpdate(QueryTranslatorImpl.java:321)
at org.hibernate.impl.SessionImpl.executeUpdate(SessionImpl.java:1019)
at org.hibernate.impl.QueryImpl.executeUpdate(QueryImpl.java:89)
at map.manytoone.manytooneTest.testCountExam(manytooneTest.java:87)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
query.uniqueResult();
!可以
或者
Query query=session.createQuery(" from map.manytoone.Student");
List list=query.list();
return list.size();
我的代码如下:
------------------------------------------------------
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import java.util.*;
public class manytooneTest extends TestCase{
Session session=null;
static SessionFactory sessionFactory=null;
static {
try {
Configuration config = new Configuration().configure();
sessionFactory=config.buildSessionFactory();
} catch(HibernateException e) {
e.printStackTrace();
}
}
public void testCountExam(){
Session s = HibernateUtil.currentSession();
SQLQuery q = s.createSQLQuery(" select count(*) from map.manytoone.Student");
List str=q.list();
//如何得到统计数量
}
}
List list=query.list();
return list.size();
这个可以得出一个统计的数量。那query.uniqueResult(),你是如何得到的。是Integer n=query.uniqueResult();可我试了不可以呀。那query.executeUpdate();是返回的进行添,删,改所影响的数量。那仅仅查询我怎么得不到呢。麻烦你再看看是不是这样的,再回复的详细点儿。
谢谢你了。我会给你分的.
这样有点浪费了。Query q = session.createQuery("select count(*) from table");
int count = ((Integer)q.uniqueResult()).intValue();
我有个方法你可以试试~~
/**
*@param pid 是个外键
**/
public ScrollableResults findAllcount(int pid)//多表查询
{
try {
String queryString ="select 表1.*,表2.* from 表1,表2 where 表1.outkey=表2.id and 表1.outkey=?";
Session g=getSession();
Query queryObject =g.createQuery(queryString);
queryObject.setParameter(0, pid);
//g.close();
return queryObject.scroll();
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}这个方法查询语句和SQL语句就一样了,只不过表名用的是类名这个你应该知道吧 导入一下import org.hibernate.ScrollableResults;这个包就行了