/**
 * 创建日期 2004-5-28
* 针对(client_acec1_jqal库)
 * @author Administrator
 * 更改所生成文件模板为
 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */
package jml;import java.sql.*;
import java.util.*;
import java.util.regex.*;import mypool.*;public class CommDAO {
DBConnectionManager dbm= DBConnectionManager.getInstance();
private String fail="fail";
private String success="success"; /**插入、更新、删除一条记录
* @param sql 传入sql语句
* @return 表示成功或失败字符串
        * @exception 抛出Exception
*/
public String  insertComm(String sql) throws Exception {
Connection con=null;
PreparedStatement  pstm=null; try{
con=dbm.getConnection("sqlserver");


pstm = con.prepareStatement(sql);
int result=pstm.executeUpdate();
if(result>0)
return success;
else
return fail;
}
finally{
if(pstm!=null)pstm.close();
if(con!=null)dbm.freeConnection("sqlserver",con);
}
 }    /**批量插入、更新、删除数据
   *@param sqls 批量插入、更新、删除的sql语句数组
   *@return 成功或失败字符串
           *@exception 抛出Exception
   */
   public String insertsComm(String[] sqls) throws Exception{
  Connection con=null;
  Statement  pstm=null;
  int[] k=null;
  try{
  con=dbm.getConnection("sqlserver");
  con.setAutoCommit(false);
  pstm = con.createStatement();
  for(int j=0;j<sqls.length;j++){
  pstm.addBatch(sqls[j]);   }
  k= pstm.executeBatch();
  pstm.close();
  boolean flag=true;
  for(int i=0;i<k.length;i++){
  if(k[i]<1){
  flag=false;
  break;
  }
  }
  if(flag==true){
  con.commit();
  return success;
  }else{
  con.rollback();
   return fail;
  }
   }
  finally{
con.setAutoCommit(true);
if(pstm!=null)pstm.close();
if(con!=null)dbm.freeConnection("sqlserver",con);
  }
   } /**
*查询记录
*@param length 查询记录的字段数量,
*@param sql 要查询的SQL语句
*@return 返回Collection 集合 集合保存为字符串数组
       *@exception 抛出Exception
*/
public Collection queryComm(int length,String sql) throws Exception{ Connection con=null;
PreparedStatement  pstm=null;
ResultSet rs=null; ArrayList list=new ArrayList();
try{
 con=dbm.getConnection("sqlserver");  pstm=con.prepareStatement(sql);
 rs=pstm.executeQuery();
while(rs.next()){
String[] strs=new String[length];
for(int j=0;j<length;j++){
strs[j]=rs.getString(j+1);//.trim();
if(strs[j]!=null)
strs[j]=strs[j].trim();
} list.add(strs);
}
}
finally{
if(rs!=null)rs.close();
if(pstm!=null)pstm.close();
if(con!=null)dbm.freeConnection("sqlserver",con);
}
return list;
} /**
*查询记录
*@param sql 要查询的SQL语句
*@return 返回Collection 集合 ,集合保存为字符串
        * @exception 抛出Exception
*/
    public Collection getOneCol(String sql) throws Exception
    {
        Connection conn=null; ResultSet rs = null;
        ArrayList xm = new ArrayList();
        int column = 0;
        try
        { //         conn = ds.getConnection();
  conn=dbm.getConnection("sqlserver");
            Statement stmt =conn.createStatement();            rs = stmt.executeQuery(sql);
            column = rs.getMetaData().getColumnCount();            while(rs.next())
            {
                int i = 1;
                while(i < column + 1)
                {
                    String colvalue = rs.getString(i);
                    if(colvalue == null)
                    {
                        colvalue = "";
                    } else
                    {
                        colvalue = colvalue.trim();
                    }
                    xm.add(colvalue);                    i++;
                }
            }
rs.close();
stmt.close();
        }
        finally
        {
if(conn!=null)dbm.freeConnection("sqlserver",conn);
        }
        return xm;
    }
    /**
    *@param sql 语句包含二个字段1 序号,2 文本内容
    *@return 返回值为map,map的键值为序号,对象值为文本内容
    * @exception 抛出Exception
    */
    public Map queryHfy(String sql) throws Exception{ Connection con=null;
PreparedStatement  pstm=null;
ResultSet rs=null; Map map = new HashMap();
try{
con=dbm.getConnection("sqlserver");
pstm=con.prepareStatement(sql);
rs=pstm.executeQuery();
while(rs.next()){
String xh=rs.getString(1);
String nr=rs.getString(2);
map.put(xh,nr);
}
rs.close();
pstm.close();
}
finally{
if(con!=null)dbm.freeConnection("sqlserver",con);
}
return map;
}


/**

解决方案 »

  1.   

    编译没有问题
    运行报以下错误
    java.lang.NullPointerException
            at jml.CommDAO.insertComm(CommDAO.java:36)
            at jml.hftv_man.processMess(hftv_man.java:26)
            at jml.hftv_man.main(hftv_man.java:183)
    SQLinsert man_jsmsg (sjh,mdhm,jsmsg) values ('13600000000','018882','015,016')
    java.lang.NullPointerException
            at jml.CommDAO.getOneCol(CommDAO.java:146)
            at jml.hftv_man.processMess(hftv_man.java:39)
            at jml.hftv_man.main(hftv_man.java:183)
    SQLselect sjh from man_sj_user where sjh='13600000000'
    Press any key to continue...
      

  2.   

    怎么没有catch块???
    在try和fianlly之间都加上
    catch(Exception e){
      e.printStackTrace();
    }
    这样就可以很清楚的定位到出空指针异常的代码行了~~~
      

  3.   

    太长了,至于空指针异常一般都是没有被实例化所导致的错误,也就是没有new,楼主根据出错的地点,好好找找这个对象有没有被实例化吧
      

  4.   

    在36行commDAO.insertComm(sql);前检查一下commDAO是否为null。
    估计问题出在这里
      

  5.   

    空指针一般是定义了一个对象实例,而这个对像实例的方法返回NULL值,就会返回空指针的异常
      

  6.   

    建议楼主把出错的行标注出来。另外一点,判断语句最好把常量写在前面,如if( con!=null ) => if( null != con )if( con.equals("ABC") ) => if( "ABC".equals(con) )
      

  7.   

    空指针的问题很容易查,哪是第一个出错的地方,上哪找去。一般都是类没有初始化,就被使用里面的方法。或者传入的是一个null的类,结果还是调用了这个类的方法。报错。
      

  8.   

    SQLinsert man_jsmsg (sjh,mdhm,jsmsg) values ('13600000000','018882','015,016')
    问题就从这里开始查!
      

  9.   

    把你的CommDAO commDAO = new CommDAO();这一句
    添加到构造函数public hftv_man(){}中,试试