亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

如何在jsp中調用hibernate

StudentDao類:
package?com.dao;

/*import?java.util.ArrayList;
import?java.util.List;*/

import?org.hibernate.Session;
import?org.hibernate.SessionFactory;
import?org.hibernate.Transaction;
import?org.hibernate.cfg.Configuration;
/*import?org.hibernate.query.Query;*/

import?com.student.Student;

public?class?StudentDao?{
	
	public?Student?getById(int?id){
		Configuration?cfg=new?Configuration().configure();
		SessionFactory?sessionFactory=cfg.buildSessionFactory();
		Session?session=sessionFactory.openSession();
		Transaction?tran=session.beginTransaction();
		Student?s=session.get(Student.class,?id);
		tran.commit();
		session.close();
		return?s;
	}
	
}

jsp代碼:
<%
	StudentDao?dao=new?StudentDao();
	Student?s=dao.getById(13);
%>
<table>
	<tr>
		<td><%=s.getName()?%></td>
		<td><%=s.getNumber()?%></td>
		<td><%=s.getBirthday()?%></td>
	</tr>
提示Configuration?cfg=new?Configuration().configure();這一行代碼有錯,可是我寫的測試類可以成功過去s,在jsp頁面中調用
就有錯,類已經導入了,數據庫也有數據,是運行時的錯誤。

正在回答

3 回答

hibernate2.5是不用Configure創建對象的

0 回復 有任何疑惑可以回復我~
<%@?page?language="java"?contentType="text/html;?charset=UTF-8"
????pageEncoding="UTF-8"%>?????
<%@page?import="pojo.Students"%>
<%@page?import="test.StudentTest"%>
<!DOCTYPE?html?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN"?"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta?http-equiv="Content-Type"?content="text/html;?charset=UTF-8">
<%!
????StudentTest?studentTest=new?StudentTest();
????Students?s=studentTest.test();
%>
<title>Test</title>
</head>
<body>
????
????<label><%=s.getSid()?%></label>
????<label><%=s.getSname()?%></label>
????<label><%=s.getGender()?%></label>
????<label><%=s.getBirthday()?%></label>
????<label><%=s.getAddress()?%></label>
</body>
</html>

lib下引入包,jsp文件中引入java類,數據庫中要有數據。。。。


0 回復 有任何疑惑可以回復我~
#1

慕容8326568 提問者

這些我都做了,運行時提示 Configuration cfg=new Configuration().configure();這一行出錯,但是我寫的測試類并沒有問題。
2017-09-22 回復 有任何疑惑可以回復我~

什么錯誤?你倒是貼出來呀

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

如何在jsp中調用hibernate

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號