java web hibernate java.lang.NullPointerException问题
有一个程序调用数据库总是出现java.lang.NullPointerException问题代码如下hibernate.cfg.xml<!DOCTYPEhibernate...
有一个程序 调用数据库总是出现java.lang.NullPointerException问题 代码如下
hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 数据库URL -->
<property name="connection.url">
jdbc:oracle:thin:@127.0.0.1:1521:house
</property>
<!-- 数据库用户 -->
<property name="connection.username">sa</property>
<!-- 数据库用户 密码-->
<property name="connection.password">mfblove12g</property>
<!-- 数据库JDBC驱动-->
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<!-- 每个数据库都有其对应的Dialect以匹配其平台特性-->
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<!-- 指定当前session范围和上下文-->
<property name="current_session_context_class">thread</property>
<!-- 是否将运行期生成的SQL输出到日志以供调试-->
<!-- 参数为true,则程序运行时在控制台输出SQL语句-->
<property name="show_sql">true</property>
<!-- <mapping resource="org/hibernate/test/legacy/Simple.hbm.xml"/>
<class-cache
class="org.hibernate.test.legacy.Simple"
region="Simple"
usage="read-write"/>-->
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<!--映射文件配置,注意配置文件名必须包含其相对于classpath的全路径-->
<mapping resource="cn/jbit/houserent/bean/userDB.hbm.xml"/>
</session-factory>
</hibernate-configuration>
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单独操作数据库是没有问题的 展开
hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 数据库URL -->
<property name="connection.url">
jdbc:oracle:thin:@127.0.0.1:1521:house
</property>
<!-- 数据库用户 -->
<property name="connection.username">sa</property>
<!-- 数据库用户 密码-->
<property name="connection.password">mfblove12g</property>
<!-- 数据库JDBC驱动-->
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<!-- 每个数据库都有其对应的Dialect以匹配其平台特性-->
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<!-- 指定当前session范围和上下文-->
<property name="current_session_context_class">thread</property>
<!-- 是否将运行期生成的SQL输出到日志以供调试-->
<!-- 参数为true,则程序运行时在控制台输出SQL语句-->
<property name="show_sql">true</property>
<!-- <mapping resource="org/hibernate/test/legacy/Simple.hbm.xml"/>
<class-cache
class="org.hibernate.test.legacy.Simple"
region="Simple"
usage="read-write"/>-->
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<!--映射文件配置,注意配置文件名必须包含其相对于classpath的全路径-->
<mapping resource="cn/jbit/houserent/bean/userDB.hbm.xml"/>
</session-factory>
</hibernate-configuration>
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单独操作数据库是没有问题的 展开
7个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询