-3
我有一個名爲lablab的表名爲table,所以我的程序是註冊時的註冊,它將保存數據庫中的userName,fullName,birthDay,password和age。我的數據庫有fullName,userName,passWord,birthDay和age的列。我的程序運行但不能插入數據庫。使用mysql,但不能插入到數據庫
這裏是我的映射:
<hibernate-mapping>
<class name="hello.model" table="table" schema="lablab">
<id name="id" column="ID" type="int">
<generator class="native"/>
</id>
<property name="fullName" column="fullName" type="string"/>
<property name="userName" column="userName" type="string"/>
<property name="passWord" column="passWord" type="string"/>
<property name="birthDay" column="birthDay" type="string"/>
<property name="age" column="age" type="int"/>
</class>
</hibernate-mapping>
這是我cfg.xml中
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property
name="hibernate.connection.url">jdbc:mysql://localhost:3306/lablab</property>
<mapping resource="hibernate.hbm.xml"/>
</session-factory>
</hibernate-configuration>
這裏是我的java類
package hello;
import com.opensymphony.xwork2.ActionSupport;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
public class model extends ActionSupport{
private String fullName, passWord, userName, birthDay, yearX, userLogin, passLogin;
private int age, year, yearN;
private static SessionFactory factory;
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getBirthDay() {
return birthDay;
}
public void setBirthDay(String birthDay) {
this.birthDay = birthDay;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getUserLogin() {
return userLogin;
}
public void setUserLogin(String userLogin) {
this.userLogin = userLogin;
}
public String getPassLogin() {
return passLogin;
}
public void setPassLogin(String passLogin) {
this.passLogin = passLogin;
}
public String register() throws Exception {
try
{
Configuration configuration = new Configuration().configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().
applySettings(configuration.getProperties());
factory = configuration.buildSessionFactory(builder.build());
}
catch (HibernateException ex)
{
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
}
addUser(fullName, userName, passWord, birthDay, age);
String success ="success";
return success;
}
private Integer addUser(String fullName, String userName, String passWord, String birthDay, int age) {
Session session = factory.openSession();
Transaction tx = null;
Integer uID = null;
try{
tx = session.beginTransaction();
User item = new User(fullName, userName, passWord, birthDay, age) {};
uID = (Integer) session.save(item);
tx.commit();
}catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
}finally {
session.close();
}
return uID;
}
}
這裏是我的用戶類別
class User {
private String fullName, userName, passWord, birthDay;
private int age;
public User(){}
public User(String fullName, String userName, String passWord, String birthDay, int age){
this.fullName = fullName;
this.userName = userName;
this.passWord = passWord;
this.birthDay = birthDay;
this.age = age;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getBirthDay() {
return birthDay;
}
public void setBirthDay(String birthDay) {
this.birthDay = birthDay;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
哪裏是你的''main''?是否有例外?什麼阻止你調試? – f1sh
*我的程序運行但不能插入數據庫*的意思? – Jens
爲什麼不能呢?任何錯誤?你是否嘗試過插入INSERT? –