写了一个多线程程序,同时会有很多线程执行某条SQL语句,程序执行一会儿后,会报空指针错误(java.lang.NullPointerException),该如何处理???SQL就是简单的语句:UPDATE TABLE SET name = 'zhangshan'DB db = new DB();
ArrayList urlList = db.update(StrSQL);
DB代码如下(使用的Tomcat连接池):
private String jndiName = null;
private Connection conn = null;
private PreparedStatement psmt = null;
private ResultSet rs = null;
private ResultSetMetaData rsmd = null;public DB(){
jndiName = "java:comp/env/www_yuqing_2009/jdbcmssql";
boolean isConnValid = false;
try {
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup(jndiName);
conn = ds.getConnection();
} catch (NamingException e) {
logger.debug("Can not find JNDI Name, datesource configuration error!"); } catch (SQLException e) {
logger.debug("Can not connect to database, please check the path and the username and the password!"); }
}public int update(String strSql) throws SQLException {
int result = 0;
try{
this.psmt = this.conn.prepareStatement(strSql);
result = this.psmt.executeUpdate();
this.psmt.close();
}catch(SQLException e){
if(this.conn.getAutoCommit())
this.close();
throw new SQLException(this.MakeErrMsg(strSql,e));
}
return result;
}
ArrayList urlList = db.update(StrSQL);
DB代码如下(使用的Tomcat连接池):
private String jndiName = null;
private Connection conn = null;
private PreparedStatement psmt = null;
private ResultSet rs = null;
private ResultSetMetaData rsmd = null;public DB(){
jndiName = "java:comp/env/www_yuqing_2009/jdbcmssql";
boolean isConnValid = false;
try {
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup(jndiName);
conn = ds.getConnection();
} catch (NamingException e) {
logger.debug("Can not find JNDI Name, datesource configuration error!"); } catch (SQLException e) {
logger.debug("Can not connect to database, please check the path and the username and the password!"); }
}public int update(String strSql) throws SQLException {
int result = 0;
try{
this.psmt = this.conn.prepareStatement(strSql);
result = this.psmt.executeUpdate();
this.psmt.close();
}catch(SQLException e){
if(this.conn.getAutoCommit())
this.close();
throw new SQLException(this.MakeErrMsg(strSql,e));
}
return result;
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货