EJB3.0在取得数据源之后,第二次操作数据时 java.sql.SQLException: The statement is closed
EJB3.0在取得数据源之后,如果执行完插入数据操作后,总是提示,让我自己关闭连接,
代码改了之后,第一次执行没问题,第二次在插入数据时,提示,java.sql.SQLException: The statement is closed异常,sql语句没问题
就一个WEB工程里面的一个简单的页面传参,
我的代码看了没看出什么问题
private EntityManager entityManager;
@Resource(mappedName = "OracleDS")
private DataSource ds;
private Statement sta;
private ResultSet set;
private Connection con;
private String sql;
public void getStatement(){
if(sta==null||con==null){
try {
con=ds.getConnection();
sta=con.createStatement();
} catch (SQLException e) {
System.out.println("取得连接异常");
e.printStackTrace();
}
}
}
public void close() throws SQLException{
if(set!=null){
set.close();
set=null;
}
if(sta!=null){
sta.close();
set=null;
}
if(con!=null){
con.close();
set=null;
}
}
下面是保存数据操作
public void save(String TableName,String pFieldList,String pValueList) throws SQLException {
try {
sql="insert into "+TableName+"("+pFieldList+")values("+pValueList+")";
getStatement();
sta.executeUpdate(sql);
} catch (RuntimeException re) {
throw re;
}finally{
close();
}
}
如果,是用JAVA工程就没这么多提示,就运行一次直接通过。但是必顺不能加close那方法,要不一次也执行不了就提示The statement is closed.异常,我都感觉很奇怪