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();
}
}
相关分类