import java.io.IOException;
import java.sql.SQLException;
import com.foren.family.db.DBConn;
import org.apache.commons.httpclient.HttpClient;
public class GetHtml 
{
public static void main(String args[]) throws Exception
{
String[] aa=null;
String content="";
HttpClient client = new HttpClient();

GetWeb a=new GetWeb();

client.getHostConfiguration().setHost("www.12582.com", 80);

a.readHtml( client,"/cqnxt/priceMarketingAction.do?eachOrAll=all&serviceName=jghq&globalLocationid=1168");

a.readHtml(client, "/cqnxt/turnPageAction.do?index=2&keyWord2=&area=null");

GetValue s=new GetValue();
GetPageCount s1=new GetPageCount();
int page=s1.getPageCount();
content=s.getValue();

for(int i=1;i<=page;i++)
{
//a.readHtml( client,"/cqnxt/priceMarketingAction.do?eachOrAll=all&serviceName=jghq&globalLocationid=1168");
a.readHtml(client, "/cqnxt/turnPageAction.do?index="+i+"&keyWord2=&area=null");
GetValue S=new GetValue();
content=content+S.getValue();
}
aa=content.split(",");
System.out.println("名称:");
//int j=1;



String name=aa[0];
String type=aa[1];
Float zzz=new Float(aa[2]);
float price=zzz.floatValue();
String address=aa[3];
String intime=aa[4];
for(int w=0;w<aa.length;w++)

{
if(w%5==0)
{
name=aa[w];
}
if(w%5==1)
{
type=aa[w];
}
if(w%5==2)
{
Float cc=new Float(aa[w]);
price=cc.floatValue();
}
if(w%5==3)
{
address=aa[w];
}
if(w%5==4)
{
intime=aa[w];
}

//System.out.print(aa[w]+"---");
if((w+1)%5==0){
String sql="insert into procd(name,type,price,address,intime) values('"+name+"','"+type+"','"+price+"','"+address+"','"+intime+"')";
try {

boolean addOk = DBConn.execute(sql);
if(addOk)
{
System.out.println("成功插入一条记录!");
}
//System.out.println(name);
//System.out.println(type);
//System.out.println(price);
//System.out.println(address);
//System.out.println(intime); 
} catch (SQLException e) {

e.printStackTrace();
}
}
}

}}报错:
java.lang.NullPointerException
at org.apache.commons.dbutils.QueryRunner.prepareConnection(QueryRunner.java:189)
at org.apache.commons.dbutils.QueryRunner.update(QueryRunner.java:475)
at org.apache.commons.dbutils.QueryRunner.update(QueryRunner.java:443)
at com.foren.family.db.DBConn.execute(DBConn.java:464)
at com.foren.family.db.GetHtml.main(GetHtml.java:76)
DBConn页面没问题,是一个通用的类.下面是里面的类
public static boolean execute(String sql) throws Exception {
boolean result = false;
try{
QueryRunner run = new QueryRunner(getDataSource());
run.update(sql);
result = true;
}catch(Exception e){
result = false;
e.printStackTrace();
}
return result;
}
至于boolean addOk = DBConn.execute(sql);这条语句报错我却不懂了.为什么这个会说空指针呢?

解决方案 »

  1.   

    public static boolean execute(String sql) throws Exception {
    boolean result = false;
    try{
    if(getDataSource()==null){
      System.out.println("datasource is null");
    }else if(sql == null){
      System.out.println("sql is null");
    }QueryRunner run = new QueryRunner(getDataSource());
    run.update(sql);
    result = true;
    }catch(Exception e){
    result = false;
    e.printStackTrace();
    }
    return result;
    }看看输出什么
      

  2.   

    把你的sql打印出来,直接在数据库里执行一下,看看是否正确
    建议单步跟踪调试
      

  3.   

    估计getDataSource返回是空的,检查一下
      

  4.   

    masse(当午).我用你的方法报出这样的结果,为什么datasource is null?
    at com.foren.family.db.GetHtml.main(GetHtml.java:77)
    java.lang.NullPointerException
    at org.apache.commons.dbutils.QueryRunner.prepareConnection(QueryRunner.java:189)
    at org.apache.commons.dbutils.QueryRunner.update(QueryRunner.java:475)
    datasource is null
      

  5.   

    我别的程序用DBConn里面的这个方法都能正确插入数据的,就这个程序有问题
      

  6.   

    没有人关注了吗?
    我怎么才能解决javax.servlet.ServletException: Cannot get a connection
    我别的程序都可以正常访问数据库(插入,读取)
    这个程序为什么就不能插入呢?
    对了..插入的量有点大..我是用for实现的..
      

  7.   

    如果不是用eclipse,可以依次在你每个涉及变量变化和取回数据的语句后面都加上一个输出变量值和数据的语句,必要处还要加return 语句防止看不到结果。一行行过滤就知道哪里问题了。
      

  8.   

    设置 断点、输出sql 
     看问题出现在哪,
      

  9.   

    我是用是eclipse
    报错也就是boolean addOk = DBConn.execute(sql);这句报错.
    意思好像是说这个里面的getDataSource返回的是null.我不知道为什么返回是null
    望大家指导我一下..谢谢先
      

  10.   

    你们帮我看一下这个server.xml有问题没有.老是报connection为null <Context path="" docBase="E:\jsp" reloadable="true" debug="0" crossContext="true">
    <Resource name="jdbc/bn" auth="Container" type="javax.sql.DataSource"/>
    <ResourceParams name="jdbc/bn">
    <parameter>
    <name>factory</name>
    <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
    </parameter>
    <parameter>
    <name>driverClassName</name>
    <value>sun.jdbc.odbc.JdbcOdbcDriver</value>
    </parameter>
    <parameter>
    <name>url</name>
    <value>jdbc:odbc:foren</value>
    </parameter>
    <parameter>
    <name>username</name>
    <value>foren</value>
    </parameter>
    <parameter>
    <name>password</name>
    <value>foren</value>
    </parameter>
    <parameter>
    <name>maxActive</name>
    <value>20</value>
    </parameter>
    <parameter>
    <name>maxIdle</name>
    <value>100</value>
    </parameter>
    <parameter>
    <name>maxWait</name>
    <value>3000</value>
    </parameter>
    <parameter>
    <name>removeAbandonedTimeout</name>
    <value>60</value>
    </parameter>
    <parameter>
    <name>logAbandoned</name>
    <value>true</value>
    </parameter>
    </ResourceParams>

      </Context>
      

  11.   

    下面是我用断点找到的调用数据源报的错
    javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initialimport javax.sql.DataSource;
    import java.sql.Connection;
    import java.sql.SQLException;
    import javax.naming.Context;
    import javax.naming.NamingException;
    public class DatabaseConnection 
    {
    public static synchronized Connection getConnection() throws Exception
    {

    try
    {
    Context initCtx=new javax.naming.InitialContext();
    Context envCtx=(Context)initCtx.lookup("java:comp/env");
    DataSource ds=(DataSource)envCtx.lookup("jdbc/bn");;
    return ds.getConnection();
    }
    catch(SQLException e)
    {
    throw e;
    }
    catch(NamingException e)
    {
    throw e;
    }

    }
    }
      

  12.   

    首先打上SP4的补丁,
    如果没有解决那么安装SERVER的JDBC驱动
    如果还是没有解决看一下主键是不是自增的
    这个是我遇到问题时候的解决方法,希望可以帮助你