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);这条语句报错我却不懂了.为什么这个会说空指针呢?
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);这条语句报错我却不懂了.为什么这个会说空指针呢?
解决方案 »
- JTree删除结点的问题
- 如何设置控件的显示顺序
- 请教一个正则表达式
- 如何求出1-1000的质素 (不能用除法和求余)
- java 成员变量在内存中的什么位置
- 求助java读取excel文件,再写到数据库中遇到了问题!
- 有没有JAVA
- 可否用纯java实现控制com口。成功解决这个问题的大侠,我将会另开几个贴至少送出500分。不够再加。
- 这段网上代码的synchronized加的莫名其妙,还是理解不了.....
- java中没有枚举变量,类似的功能如何简单实现?
- 关于FileInputStream和FileReader,还有FileOutputStream和FileWrite的区别.
- hibernate在配置文件中配置多个<mapping resource/>的问题,请熟悉的知晓一声
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;
}看看输出什么
建议单步跟踪调试
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
我怎么才能解决javax.servlet.ServletException: Cannot get a connection
我别的程序都可以正常访问数据库(插入,读取)
这个程序为什么就不能插入呢?
对了..插入的量有点大..我是用for实现的..
看问题出现在哪,
报错也就是boolean addOk = DBConn.execute(sql);这句报错.
意思好像是说这个里面的getDataSource返回的是null.我不知道为什么返回是null
望大家指导我一下..谢谢先
<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>
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;
}
}
}
如果没有解决那么安装SERVER的JDBC驱动
如果还是没有解决看一下主键是不是自增的
这个是我遇到问题时候的解决方法,希望可以帮助你