2010-06-04 8 views
5

Ich erhalte diese Fehlermeldung:Ausnahme java.sql.SQLException: Parameter Index außerhalb des zulässigen Bereichs (1> Anzahl der Parameter, die 0 ist)

Exception java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0). 
java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0). 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055) 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956) 
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926) 
at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3288) 
at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3272) 
at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:4108) 
at com.inmobia.RSSToNews.Consumer.print(Consumer.java:92) 
at com.inmobia.RSSToNews.Consumer.main(Consumer.java:122) 

Wenn ich versuche, diese Klasse auszuführen:

package com.in.RSSToNews; 
import java.net.URL; 
import java.util.ArrayList; 
import java.util.Date; 
import java.util.Iterator; 
import java.util.List; 
import java.net.URLConnection; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.Statement; 
import java.text.ParseException; 
import java.text.SimpleDateFormat; 

import com.sun.syndication.feed.synd.SyndEntry; 
import com.sun.syndication.feed.synd.SyndFeed; 
import com.sun.syndication.io.SyndFeedInput; 
import com.sun.syndication.io.XmlReader; 

/** 
* Consumer class from RSS/Atom feed type. 
* 
* @author Rbn 
*/ 

public class Consumer { 





SyndFeed feed; 

/** 
* Class constructor 
* 
* @param url: url path to consume 
*/ 
public Consumer(String url) { 
    super(); 
    try { 
     URL feedUrl = new URL(url); 

     SyndFeedInput input = new SyndFeedInput(); 
     feed = input.build(new XmlReader(feedUrl)); 

    } catch (Exception ex) { 
     ex.printStackTrace(); 
     System.out.println("ERROR: "+ex.getMessage()); 
    } 
} 

/** 
* print method 
* Scroll down the list of entries and displays the feed title, author and description 
*/ 
void print() { 

    //feeds list 
    List<SyndEntry> entradas = new ArrayList<SyndEntry>(); 
    entradas = feed.getEntries(); 
    try 
    { 


     //list iterator 
    Iterator<SyndEntry> it = entradas.iterator(); 
    Class.forName("org.gjt.mm.mysql.Driver"); 
    Connection conexion = DriverManager.getConnection("jdbc:mysql://localhost/newsamerica", "root", "123"); 

     if (!conexion.isClosed()) 
     { 
        while (it.hasNext()) 
         { 
          SyndEntry entrada = it.next(); 
          String title=(entrada.getTitle()); 
          String link=(entrada.getLink()); 
          String author=(entrada.getAuthor()); 
          String description=(""+entrada.getDescription()); 
          Date date=(entrada.getPublishedDate()); 
          String date2= date.toString(); 

          String content=(""+entrada.getContents()); 

          //description=description.replaceAll("SyndContentImpl.value=", ""); 
          //System.out.println(text2); 
          //System.out.println("Autor.......: " + entrada.getAuthor()); 
          // System.out.println("Noticia.......: " + entrada.getContents()); 
          // Statement st = conexion.createStatement(); 
          PreparedStatement pstmt = conexion.prepareStatement("INSERT INTO general_news(title,link,author,description,date,content) VALUES ('?','?','?','?','?','?')"); 
          pstmt.setString(1,title); 
          pstmt.setString(2,link); 
          pstmt.setString(3,author); 
          pstmt.setString(4,description); 
          pstmt.setString(5,date2); 
          pstmt.setString(6,content); 
          ResultSet rs = pstmt.executeQuery(); 
          rs.next(); 
         } 



     } 


     conexion.close(); 
} 

    catch (Exception e) 
    { 
     // Error en algun momento. 
     System.out.println("Excepcion "+e); 
     e.printStackTrace(); 
    } 
} 


public static void main(String[] args) { 

    Consumer feed = new Consumer (args[0]); 
    feed.print(); 
} 

}

+1

Ich bin froh zu helfen. Bei StackOverflow ist es üblich, die Antwort zu akzeptieren, die am meisten zur Lösung Ihres Problems beigetragen hat. Sie tun dies, indem Sie auf das Häkchen in der Nähe der Antwortstimmen klicken, so dass es durchgehend grün wird. –

Antwort

12

Horrible Formatierung. Wenn Sie Parameter verwenden, geben Sie nicht '?' sondern einfach ? ein.

Ändern Sie die Zeile:

PreparedStatement pstmt = conexion.prepareStatement("INSERT INTO general_news(id,title,link,author,description,date,content) VALUES ('?','?','?','?','?','?')"); 

zu

PreparedStatement pstmt = conexion.prepareStatement("INSERT INTO general_news(id,title,link,author,description,date,content) VALUES (?,?,?,?,?,?)"); 

Aus diesem Grund ist er denkt, dass Sie 0 Parameter haben, aber die ersten waren, angeben.

Beachten Sie auch, dass Sie eine INSERT ausführen, und als solche darf keine executeQuery verwenden. Verwenden Sie stattdessen executeUpdate().

+1

Danke Mann, aber jetzt habe ich diesen Fehler: Excepcion java.sql.SQLException: kann keine Datenmanipulationsanweisungen mit executeQuery() ausgeben. java.sql.SQLException: Mit executeQuery() können keine Datenmanipulationsanweisungen ausgegeben werden. \t bei com.mysql.jdbc.SQLError.createSQLException (SQLError.java:1055) \t bei com.mysql.jdbc.SQLError.createSQLException (SQLError.java:956) \t bei com.mysql.jdbc.SQLError.createSQLException (SQLError.java:926) \t bei com.mysql.jdbc.StatementImpl.checkForDml (StatementImpl.java:412) \t bei com.mysql.jdbc.PreparedStatement.executeQuery (PreparedStatement.java:1794) – Rbn

+2

Verwenden 'executeUpdate' und nicht 'executeQuery'. –

+0

Und haben Sie eine Spalte namens 'title' in' general_news' table :) Wenn Sie möchten, fügen Sie die 'CREATE TABLE' Anweisung ein. –

Verwandte Themen