【问题标题】:Using mysql but can't insert to database使用mysql但无法插入数据库
【发布时间】:2017-10-27 07:01:58
【问题描述】:

我有一个名为 lablab 的数据库,其中有一个名为 table 的表,所以我的程序在注册时是一个注册程序,它将在数据库中保存用户名、全名、生日、密码和年龄。我的数据库有 fullName、userName、passWord、birthDay 和 age 列。我的程序运行了,但无法插入数据库。

这是我的映射:

<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在哪里?有例外吗?是什么阻止你调试?
  • 我的程序运行但无法插入数据库是什么意思?
  • 为什么不能呢?有什么错误吗?您是否尝试过单步执行 INSERT?

标签: java mysql database hibernate


【解决方案1】:

如果您使用的是 Hibernate 5,您应该通过这种方式创建 SessionFactory

factory = new Configuration().configure().buildSessionFactory();

这是不正确的方式:

 Configuration configuration = new Configuration().configure();
 StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().
 applySettings(configuration.getProperties());
 factory = configuration.buildSessionFactory(builder.build());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-14
    • 1970-01-01
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多