怎么java抓取数据并存到数据库中?

arsenal白 2015-12-02 03:36:15
我自己写了一个报错不知道怎么改了,请帮我看看是什么问题

控制台:
严重: Servlet.service() for servlet [RentalServlet] in context with path [/rentalDemo1202] threw exception
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at cn.itcast.servlet.BaseServlet.service(BaseServlet.java:61)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at cn.itcast.servlet.BaseServlet.service(BaseServlet.java:51)
... 21 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?)' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2617)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2778)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2819)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1842)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1764)
at cn.zx.rental.dao.RentalDao.addItems(RentalDao.java:26)
at cn.zx.rental.service.RentalService.addItem(RentalService.java:174)
at cn.zx.rental.service.RentalService.crawler(RentalService.java:31)
at cn.zx.rental.web.servlet.RentalServlet.crawler(RentalServlet.java:29)
... 26 more
...全文
285 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
arsenal白 2015-12-02
  • 打赏
  • 举报
回复
引用 6 楼 Icehand哥的回复:
5楼正解啊小伙子,错误提示中已经说得很清楚了。出错了,先认真看看异常,自己实在解决不了再把问题抛出来,这样节约你的时间。
嗯嗯,我当时打印了sql看了一下,发现自己的sql执行的都是带?的,但是不知道为啥,,还是jdbc掌握的不好,,有待加强,谢谢了,
arsenal白 2015-12-02
  • 打赏
  • 举报
回复
引用 5 楼 约定291天后的回复:
 public void addDatas(Item i,Data d) throws Exception {
        Connection con = getConnection();
        String sql = "insert into data(did,dprice,iid) values(?,?,?)";
        PreparedStatement pstmt = con.prepareStatement(sql);
        pstmt.setString(1, d.getDid());
        pstmt.setString(2, d.getDprice());
        pstmt.setString(3, i.getIid());
         
        pstmt.executeUpdate(sql);
        System.out.println(d.getDid()+""+d.getDprice()+":"+i.getIid());
        pstmt.close();
        con.close();
    }
pstmt.executeUpdate(sql); 你等于直接在数据库里执行insert into data(did,dprice,iid) values(?,?,?),是不能执行成功的,prepareStatement是继承自Statement。 pstmt.executeUpdate()即可。
真的是这样啊,,没有注意到,太感谢了
Icehand哥 2015-12-02
  • 打赏
  • 举报
回复
5楼正解啊小伙子,错误提示中已经说得很清楚了。出错了,先认真看看异常,自己实在解决不了再把问题抛出来,这样节约你的时间。
加权不平权 2015-12-02
  • 打赏
  • 举报
回复
 public void addDatas(Item i,Data d) throws Exception {
        Connection con = getConnection();
        String sql = "insert into data(did,dprice,iid) values(?,?,?)";
        PreparedStatement pstmt = con.prepareStatement(sql);
        pstmt.setString(1, d.getDid());
        pstmt.setString(2, d.getDprice());
        pstmt.setString(3, i.getIid());
         
        pstmt.executeUpdate(sql);
        System.out.println(d.getDid()+""+d.getDprice()+":"+i.getIid());
        pstmt.close();
        con.close();
    }
pstmt.executeUpdate(sql); 你等于直接在数据库里执行insert into data(did,dprice,iid) values(?,?,?),是不能执行成功的,prepareStatement是继承自Statement。 pstmt.executeUpdate()即可。
ilmlife 2015-12-02
  • 打赏
  • 举报
回复
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?)' at line 1 at cn.zx.rental.dao.RentalDao.addItems(RentalDao.java:26) 自己打个断点瞅瞅是不是传过来的参数特殊字符什么的
arsenal白 2015-12-02
  • 打赏
  • 举报
回复
domain Data.java

package cn.zx.rental.domain;

public class Data {
	private String did;
	private String dprice;
	public String getDid() {
		return did;
	}
	public void setDid(String did) {
		this.did = did;
	}
	public String getDprice() {
		return dprice;
	}
	public void setDprice(String dprice) {
		this.dprice = dprice;
	}
	public Data(String did, String dprice) {
		super();
		this.did = did;
		this.dprice = dprice;
	}
	public Data() {
		super();
		// TODO Auto-generated constructor stub
	}
	@Override
	public String toString() {
		return "Data [did=" + did + ", dprice=" + dprice + "]";
	}
	
	
}

Item.java

package cn.zx.rental.domain;

public class Item {
	private String iid;
	private String iurl;
	public String getIid() {
		return iid;
	}
	public void setIid(String iid) {
		this.iid = iid;
	}
	public String getIurl() {
		return iurl;
	}
	public void setIurl(String iurl) {
		this.iurl = iurl;
	}
	public Item(String iid, String iurl) {
		super();
		this.iid = iid;
		this.iurl = iurl;
	}
	public Item() {
		super();
		// TODO Auto-generated constructor stub
	}
	@Override
	public String toString() {
		return "Item [iid=" + iid + ", iurl=" + iurl + "]";
	}
	
	
}

Rule.java

package cn.zx.rental.domain;

/**
 * 规则类
 */
public class Rule
{
	/**
	 * 链接
	 */
	private String url;

	/**
	 * 参数集合
	 */
	private String params;
	/**
	 * 参数对应的值
	 */
	private String values;

	/**
	 * 对返回的HTML,第一次过滤所用的标签,请先设置type
	 */
	private String resultTagName;

	/**
	 * CLASS / ID / SELECTION
	 * 设置resultTagName的类型,默认为ID 
	 */
	private int type = ID ;
	
	/**
	 *GET / POST
	 * 请求的类型,默认GET
	 */
	private int requestMoethod = GET ; 
	private String tagType;
	
	public final static int GET = 0 ;
	public final static int POST = 1 ;
	

	public final static int CLASS = 0;
	public final static int ID = 1;
	public final static int SELECTION = 2;

	public Rule()
	{
	}

	
	public Rule(String url, String params, String values,
			String resultTagName, int type, int requestMoethod, String tagType)
	{
		super();
		this.url = url;
		this.params = params;
		this.values = values;
		this.resultTagName = resultTagName;
		this.type = type;
		this.requestMoethod = requestMoethod;
		this.tagType = tagType;
	}

	
	



	public String getUrl()
	{
		return url;
	}

	public void setUrl(String url)
	{
		this.url = url;
	}

	public String getParams()
	{
		return params;
	}

	public void setParams(String params)
	{
		this.params = params;
	}

	public String getValues()
	{
		return values;
	}

	public void setValues(String values)
	{
		this.values = values;
	}

	public String getResultTagName()
	{
		return resultTagName;
	}

	public void setResultTagName(String resultTagName)
	{
		this.resultTagName = resultTagName;
	}

	public int getType()
	{
		return type;
	}

	public void setType(int type)
	{
		this.type = type;
	}

	public int getRequestMoethod()
	{
		return requestMoethod;
	}

	public void setRequestMoethod(int requestMoethod)
	{
		this.requestMoethod = requestMoethod;
	}


	public String getTagType() {
		return tagType;
	}


	public void setTagType(String tagType) {
		this.tagType = tagType;
	}
}



package cn.zx.rental.domain;

public class RuleException extends RuntimeException
{

	private static final long serialVersionUID = 1L;

	public RuleException()
	{
		super();
		// TODO Auto-generated constructor stub
	}

	public RuleException(String message, Throwable cause)
	{
		super(message, cause);
		// TODO Auto-generated constructor stub
	}

	public RuleException(String message)
	{
		super(message);
		// TODO Auto-generated constructor stub
	}

	public RuleException(Throwable cause)
	{
		super(cause);
		// TODO Auto-generated constructor stub
	}

}


package cn.zx.rental.domain;

public class TextTypeData {
	private String text;
	private String id;
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	public String getId() {
		return id;
	}
	public void setId(String string) {
		this.id = string;
	}
	@Override
	public String toString() {
		return "TextTypeData [text=" + text + ", id=" + id + "]";
	}
		
}

arsenal白 2015-12-02
  • 打赏
  • 举报
回复
dao:

package cn.zx.rental.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import cn.zx.rental.domain.Data;
import cn.zx.rental.domain.Item;


public class RentalDao {
	
	public static Connection getConnection() throws Exception {
		Class.forName("com.mysql.jdbc.Driver");
		String url = "jdbc:mysql://localhost:3306/rental";
		return DriverManager.getConnection(url, "root", "zhangxin");
	}
	
	public void addItems(Item i) throws Exception {
		Connection con = getConnection();
		String sql = "insert into item(iid,iurl) values(?,?)";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, i.getIid());
		pstmt.setString(2, i.getIurl());
		
		pstmt.executeUpdate(sql);
		System.out.println(i.getIid()+":"+i.getIurl()+"additems");
		pstmt.close();
		con.close();
	}
	
	public void addDatas(Item i,Data d) throws Exception {
		Connection con = getConnection();
		String sql = "insert into data(did,dprice,iid) values(?,?,?)";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, d.getDid());
		pstmt.setString(2, d.getDprice());
		pstmt.setString(3, i.getIid());
		
		pstmt.executeUpdate(sql);
		System.out.println(d.getDid()+""+d.getDprice()+":"+i.getIid());
		pstmt.close();
		con.close();
	}
	/*public void addItems(Item i){
		String sql = "insert into item(iid,iurl) values(?,?)";
		Object[] params = {
				i.getIid(),i.getIurl()
		};
		try {
			qr.update(sql,params);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public void addDatas(Item i,TextTypeData d){
		String sql = "insert into data(did,dprice,iid) values(?,?,?)";
		Object[] params = {
			d.getId(),d.getText(),i.getIid()
		};
		try {
			qr.update(sql,params);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}*/
}

arsenal白 2015-12-02
  • 打赏
  • 举报
回复

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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