hibernate.cfg.xml
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
jdbc:oracle:thin:@127.0.0.1:1521:house
oracle.jdbc.driver.OracleDriver
text.java
package cn.jdit.houserent.bean;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import cn.jdit.houserent.bean.*;
public class text {
public static void main (String[]args)
{
userDB user = new userDB();
user.setName("liwei");
user.setPassword("123");
Configuration conf=null;
SessionFactory sessionFactory = null;
Session session=null;
Transaction tx=null;
try{
conf=new Configuration().configure();
sessionFactory=conf.buildSessionFactory();
session=sessionFactory.openSession();
tx=session.beginTransaction();
session.save(user);
tx.commit();
}
catch(HibernateException e)
{
tx.rollback();
e.printStackTrace();
}finally
{session.clear();
sessionFactory.close();}
}
}
结果显示
Exception in thread "main" java.lang.NullPointerException
at cn.jdit.houserent.bean.text.main(text.java:35)
如果使用java单独操作数据库是没有问题的