0
你好,我試圖插入一些數據使用休眠到MySQL數據庫。休眠沒有采取通過html/jsp字段輸入值
的index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Register a Device</title>
</head>
<body>
<form action="register.jsp" method="post">
Make:<input type="text" name="make"/><br><br/>
Model:<input type="text" name="model"/><br><br/>
Release Date:<input type="text" name="relDate"/><br><br/>
Rating:<input type="text" name="rating"/><br><br/>
Price:<input type="text" name="price"/><br><br/>
<input type="submit" value="Register"/>
</form>
</body>
</html>
模型類
@Entity
@Table(name = "PHONES")
public class Phone {
@Id @GeneratedValue
@Column(name = "ID")
private int id;
@Column(name = "MAKE")
private String make;
@Column(name = "MODEL")
private String model;
@Column(name = "RELEASE_YEAR")
private String relDate;
@Column(name = "RATING")
private String rating;
@Column(name = "PRICE")
private String price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getMake() {
return make;
}
public void setMake(String make) {
this.make = make;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getRelDate() {
return relDate;
}
public void setRelDate(String relDate) {
this.relDate = relDate;
}
public String getRating() {
return rating;
}
public void setRating(String rating) {
this.rating = rating;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
DAO類
public class PhoneDao {
private static SessionFactory factory;
public static int register(Phone ph) {
int i = 0;
Session session = null;
Transaction t = null;
try{
session = new Configuration().configure().addAnnotatedClass(Phone.class).buildSessionFactory().openSession();
t = session.beginTransaction();
System.out.println(ph.getRating());
i = (Integer) session.save(ph);
t.commit();
}catch(Throwable ex){
if (t != null)
t.rollback();
ex.printStackTrace();
}finally{
session.close();
}
return i;
}
register.jsp
Register.jsp
<%@page import="org.nag.hibernate.kudos.Dao.PhoneDao"%>
<jsp:useBean id="u" class="org.nag.hibernate.kudos.model.Phone" />
<jsp:setProperty property="*" name="u" />
<%
int i = PhoneDao.register(u);
if (i > 0)
out.print("You have successfully registered the device");
%>
Record:<br>
<jsp:getProperty property="make" name="u"/><br>
<jsp:getProperty property="model" name="u"/><br>
<jsp:getProperty property="relDate" name="u" /><br>
<jsp:getProperty property="rating" name="u"/><br>
<jsp:getProperty property="price" name="u" /><br>
CFG文件
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">create</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3301/library</property>
<property name="connection.username">root</property>
<property name="connection.password">xxxxxx</property>
<mapping class="org.nag.hibernate.kudos.model.Phone"/>
</session-factory>
當我張貼它成功地做它在數據庫中,但空值所有的Fileds。
我已經使用了的getProperty JSP的看到壽/ p和有它本身不是借字段的index.jsp進入
請幫助我。
謝謝 mark。
爲什麼不將數據發佈到servlet而不是jsp? –
是的,這是一個好主意,但。這樣它的工作。 – mark