복붙노트

[SQL] 예외 java.sql.SQLException의 (0, 1> 파라미터의 수)의 범위 중 정수 인덱스 [중복]

SQL

예외 java.sql.SQLException의 (0, 1> 파라미터의 수)의 범위 중 정수 인덱스 [중복]

이 오류를 받고 있어요 :

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)

나는이 클래스를 실행하기 위해 노력하고있어 경우 :

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. ==============================

    1.끔찍한 서식. 매개 변수를 사용하는 경우 당신은 넣지 마십시오 '?' 단순히?.

    끔찍한 서식. 매개 변수를 사용하는 경우 당신은 넣지 마십시오 '?' 단순히?.

    줄을 변경 :

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

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

    그는 당신이 0 매개 변수가 있지만 첫째을 지정하고 생각하는 이유입니다.

    또한 당신이 INSERT를 실행하는 참고 및 MUST으로하는 executeQuery를 사용하지. 사용 executeUpdate의 () 대신.

  2. from https://stackoverflow.com/questions/2975797/exception-java-sql-sqlexception-parameter-index-out-of-range-1-number-of-par by cc-by-sa and MIT license