在那个执行事务管理的地方和最后U调用方法的时候报空指针异常,怎么解决

package dao;


import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.SQLException;


import util.DBHelper;

import entity.User;


public class Userdao {

Connection conn = null;

PreparedStatement ptmt = null;


public int add() {

int updateRows = 0;

try {

conn.setAutoCommit(false);// 开启jdbc事务管理

Connection conn = DBHelper.getConnection();

String sql = "insert into chaoshi_user(userCode,UserName)"

+ "value(?,?)";

ptmt = conn.prepareStatement(sql);

ptmt.setString(1, "chentao");

ptmt.setString(2, "陈涛");

updateRows = ptmt.executeUpdate();

conn.commit();// 开始执行事务

if (updateRows > 0) {

System.out.println("add success");

} else {

System.out.println("add failure");

}


} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

try {

conn.rollback();

} catch (SQLException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

} finally {

try {

if(ptmt!=null){

ptmt.close();}

if(conn!=null){

conn.close();}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

return updateRows;


}


public static void main(String[] args) {

Userdao u = new Userdao();

       u.add();

}

}


224314224321
浏览 1289回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java