多线程程序中操作数据库的问题,线程数量比较多!

qdcomn 2010-04-27 11:44:48
写了一个多线程程序,同时会有很多线程执行某条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;
}
...全文
87 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
qdcomn 2010-04-28
  • 打赏
  • 举报
回复
我的线程比较多,可能同时会有几十个线程执行一条SQL语句
Trinx 2010-04-28
  • 打赏
  • 举报
回复
楼主没发现哪句抛空指针异常了

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧