开发工具:MyEclipse+MySql
我想把这段 Servlet连接数据库查询的改成 Hibernate 怎么改啊?给位指教下。List list = new ArrayList();
int count = 0;
try {
conn = dbUtils.getConn();//连接数据库
state = conn.createStatement();
rs = state.executeQuery("select * from test limit 5,5");
while (rs.next()) {
Test Tbean = new Test();
bean.setId(rs.getInt(1));
bean.setTitle(rs.getString(2));
list.add(bean);
}
rs.close();
rs = state.executeQuery("select count(*) from test");
if (rs.next()) {
count = rs.getInt(1);
} } catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
rs.close();
state.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
request.setAttribute("count", count);
request.setAttribute("list", list);
我想把这段 Servlet连接数据库查询的改成 Hibernate 怎么改啊?给位指教下。List list = new ArrayList();
int count = 0;
try {
conn = dbUtils.getConn();//连接数据库
state = conn.createStatement();
rs = state.executeQuery("select * from test limit 5,5");
while (rs.next()) {
Test Tbean = new Test();
bean.setId(rs.getInt(1));
bean.setTitle(rs.getString(2));
list.add(bean);
}
rs.close();
rs = state.executeQuery("select count(*) from test");
if (rs.next()) {
count = rs.getInt(1);
} } catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
rs.close();
state.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
request.setAttribute("count", count);
request.setAttribute("list", list);
解决方案 »
- div嵌套iframe ,iframe里的东西不显示!
- 没有项目做的时候怎么提高自己的it技能?
- java.lang.NoClassDefFoundError: org/apache/log4j/Layout
- fckeditor不显示
- 网上下载的JSP留言板程序,没有一个能用的!
- stsuts2为什么得不到jsp页面传的值
- 哪位有wap最简单的聊天室程序(java)
- help me .about j2ee and corba. comeon
- 怎样看Java applet和servlet中System.out.println的输出?
- 菜鸟求问switch语句怎么取键盘值呢?
- 下载多个文件
- 关于TomCat问题
看下HIBERNATE的资料自己的摸索吧,进步更快!
List list = session.createQuery(" select t from Test t ").setMaxResults(5).setFirstResult(5).list();
//setFirstResult(5)从第5条开始
//setMaxResults(5)每页显示5条
//综合以上两句表示 显示 第5-第10条数据 request.setAttribute("count", count);
request.setAttribute("list", list);
try{
openSession(); //打开session,类似与jdbc打开连接
beginTransaction();//开启事务
String hql = "from Test t where t.id=5"; //查询
Query q = session.createQuery(hql);
List q = q.list();
commit(); // 结束事务
}catch(HibernateException e){
e.printStackTrace();
rollback(); //如果操作方法有错,则执行事务回滚
}finally{
closeSession(); //不管有没有异常都要关闭session,类似与jdbc关闭连接
}
import org.hibernate.tool.hbm2ddl.SchemaExport;public class ExportDB { public static void main(String[] args) {
//读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);
}
}
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;public class Client { public static void main(String[] args) {
//读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();
//创建SessionFactory
SessionFactory factory = cfg.buildSessionFactory();
Session session = null;
try {
session = factory.openSession();
//开启事务
session.beginTransaction();
User user = new User();
user.setName("张三");
user.setPassword("123");
user.setCreateTime(new Date());
user.setExpireTime(new Date());
//保存数据
session.save(user);
//提交事务
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
//回滚事务
session.getTransaction().rollback();
}finally {
if (session != null) {
if (session.isOpen()) {
//关闭session
session.close();
}
}
}
}
}
<!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.connection.url">jdbc:mysql://localhost/hibernate</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">198912</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="com/name/hibernate/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
//读取hibernate.cfg.xml文件
Configuration config = new Configuration().configure();
SessionFactory sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.openSession();
String hql="From Test";
Query query=session.createQuery(hql);
List list=query.list();
System.out.println("list:"+list);
}
测试勒,这样报错为什么啊、、、
Exception in thread "main" org.hibernate.exception.GenericJDBCException: Cannot open connection这是我的hibernate配置文件
<session-factory>
<property name="connection.username">root</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8;</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="myeclipse.connection.profile">mybean</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="show_sql">true</property>
<mapping resource="com/hbm/Bean.hbm.xml" /> </session-factory>网上查勒说是连接不可以,不知道哪里不可以,各位指教下,谢谢勒
2、创建User Library,加入如下jar
HIBERNATE_HOME/hibernate3.jar
HIBERNATE_HOME/lib/*.jar
MySql jdbc驱动
3、创建hibernate配置文件hibernate.cfg.xml,为了便于调试最好加入log4j配置文件4、定义实体类5、定义User类的映射文件User.hbm.xml6、将User.hbml.xml文件加入到hibernate.cfg.xml文件中7、编写hbm2ddl工具类,将实体类生成数据库表8、开发客户端
为了方便跟踪sql执行,在hibernate.cfg.xml文件中加入<property name="hibernate.show_sql">true</property>hibernate配置文件要按我上面的来!
mysql jar包都打上了吗?
User.hbm.xml<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.name.hibernate.User">
<id name="id">
<generator class="uuid"/>
</id>
<property name="name"/>
<property name="password"/>
<property name="createTime"/>
<property name="expireTime"/>
</class>
</hibernate-mapping>
package com.name.hibernate;import java.util.Date;public class User {
private String id;
private String name;
private String password;
private Date createTime;
private Date expireTime; public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public Date getCreateTime() {
return createTime;
} public void setCreateTime(Date createTime) {
this.createTime = createTime;
} public Date getExpireTime() {
return expireTime;
} public void setExpireTime(Date expireTime) {
this.expireTime = expireTime;
}
}
推荐 传质博客 张孝祥的视频挺好的