一、首先先建立一個Web Project 二、然後在程式根目錄建立文件夾“DataBase”和“Doc”,分別存放資料庫文件和保存SQL語句,建完如下所示: 三、建立資料庫“dbHibernate”,保存到文件夾“DataBase”下 四、書寫SQL語句,創建“student”表 -- 資料庫名稱 ...
一、首先先建立一個Web Project
二、然後在程式根目錄建立文件夾“DataBase”和“Doc”,分別存放資料庫文件和保存SQL語句,建完如下所示:
三、建立資料庫“dbHibernate”,保存到文件夾“DataBase”下
四、書寫SQL語句,創建“student”表
-- 資料庫名稱 dbHibernate
-- 版本 SQLServer2008
--學生表
create table student(
ID varchar(10) primary key,
Name varchar(20) not null,
)
--插入數據
insert into student values('101','張三');
insert into student values('102','李四');
insert into student values('103','王五');
五、打開“DB Browser”後,右擊選擇“New...”,Driver template選擇“Microsoft SQL Server”,Driver name為“Hibernate”,Connection URL為“jdbc:sqlserver://localhost:1433; DatabaseName=dbHibernate”,然後就是資料庫的User name以及Password;還要添加“sqljdbc4.jar”驅動包,點擊“Test Driver”,可以看到測試成功
六、然後點擊“Next”,選擇“Display the selected schemas”,點擊“Add”添加資料庫“dbHibernate”,最後“Finish”。
七、右擊項目“Hibernate”->“Myeclipse”->“Add Hibernate Capabilities...”,然後預設點擊“Next”
八、選擇“Browse”,添加自己建立的包名,然後“Next”後,選擇“DB Driver”下的“Hibernate”,下麵的內容將自動調出。然後“Next”,選擇“Java package”
九、建完後,在包“com.langguojie.Hibernate”下添加了“HibernateSessionFactory.java”以及“hibernate.cfg.xml”兩個文件
十、在“DB Browser”下,選擇“Hibernate”->“Open connection”->Connected to Hibernate->dbo->TABLE->student,右擊選擇“Hibernate Reverse Engineering”
十一、選擇 Java package:“com.langguojie.Hibernate.orm”,然後選擇如下圖所示內容,點擊“Finish”即可。
十二、在包“com.langguojie.Hibernate.orm”下添加了“Student.java”和“Student.hbm.xml”兩個文件
十三、在index.jsp中添加如下代碼:
<%@page import="com.langguojie.Hibernate.orm.Student"%> <%@page import="org.hibernate.Query"%> <%@page import="com.langguojie.Hibernate.HibernateSessionFactory"%> <%@page import="org.hibernate.Session"%> <center> 學生管理<br> <br> <a href="StudentAdd.html">添加</a><br> <br> <table border="1" cellspacing="0" cellpadding="4"> <% Session ss = HibernateSessionFactory.getSession(); //建立Session對象ss ss.beginTransaction(); //打開Transaction Query query=ss.createQuery("from Student"); //調用函數Query查詢 List<Student>studentList = new ArrayList<Student>(); //將查詢的結果放到List表中 studentList = query.list(); for(int i = 0 ;i < studentList.size();i++){ %> <tr> <td><%=studentList.get(i).getId()%></td> <!-- 輸出ID --> <td><%=studentList.get(i).getName()%></td> <!-- 輸出姓名 --> <td><a href='StudentEdit.jsp?ID=<%=studentList.get(i).getId()%>'>修改</a> </td> <td><a href='servlet/StudentDelete.do?ID=<%=studentList.get(i).getId()%>'>刪除</a> </td> </tr> <% } ss.close(); //關閉Transaction %> </table> </center>
十四、建立添加頁面“StudentAdd.html”,代碼如下:
<form id="form1" name="form1" method="post"action="servlet/StudentAdd.do"> <center>添加<br><br> 學號:<input type="text" id="ID" name="ID"><br><br> 姓名:<input type="text" id="Name" name="Name" ><br><br> <input type="submit" value="確定"></center> </form>
十五、建立servlet“StudentAdd.java”,使用函數“doPost()”代碼如下:
request.setCharacterEncoding("UTF-8"); String strID = ""; String strName = ""; strID = request.getParameter("ID"); //獲取ID strName = request.getParameter("Name"); //獲取Name Session ss = HibernateSessionFactory.getSession();
ss.beginTransaction(); Student s = new Student(); s.setId(strID); s.setName(strName); ss.save(s); //保存數據 ss.getTransaction().commit(); //提交數據 ss.close(); response.sendRedirect("../index.jsp");
十六、建立編輯頁面“StudentEdit.jsp”,代碼如下:
<%@page import="com.langguojie.Hibernate.orm.Student"%> <%@page import="com.langguojie.Hibernate.HibernateSessionFactory"%> <%@page import="org.hibernate.Session"%> <% request.setCharacterEncoding("UTF-8"); String strID=""; strID = request.getParameter("ID"); Session ss = HibernateSessionFactory.getSession(); ss.beginTransaction(); Student student = (Student)ss.get(Student.class, strID); ss.getTransaction().commit(); ss.close(); %> <center> <%if(student != null){ %> <br><br>編輯學生信息<br> <form name="f1" id="f1" action="servlet/StudentEdit.do" method="post"> <table border="0"> <tr> <td>學號:</td> <td><input type="text" readonly="readonly" name="ID" id="ID" value="<%=student.getId() %>"> 學號不允許編輯</td> </tr> <tr> <td>姓名:</td> <td><input type="text" name="Name" id="Name" value="<%=student.getName() %>"></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value=" 確定 " ></td> </tr> </table> </form> <%} else { %> <br><br>編輯學生信息<br><br> 學號為<%=strID %>的學生數據在資料庫中不存在!<br><br> <a href="index.jsp">返回</a> <%} %> </center>
十七、建立servlet“StudentEdit.java”,使用函數“doPost()”,代碼如下:
request.setCharacterEncoding("utf-8"); String strID = ""; String strName = ""; strID = request.getParameter("ID"); strName = request.getParameter("Name"); Session session = HibernateSessionFactory.getSession(); session.beginTransaction(); Student student = (Student)session.load(Student.class, strID); student.setName(strName); session.update(student); session.getTransaction().commit(); session.close(); response.sendRedirect("../index.jsp");
十八、建立servle“StudentDelete.java”,使用函數“doGet()”,代碼如下:
request.setCharacterEncoding("UTF-8"); String strID=""; strID = request.getParameter("ID"); Session ss = HibernateSessionFactory.getSession(); ss.beginTransaction(); Student student = (Student)ss.load(Student.class, strID); ss.delete(student); ss.getTransaction().commit(); ss.close(); response.sendRedirect("../index.jsp");
十九、運行結果截圖: