【问题标题】:Exception java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0) [duplicate]异常java.sql.SQLException:参数索引超出范围(1>参数数量,即0)[重复]
【发布时间】:2026-01-15 13:55:02
【问题描述】:

我收到此错误:

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();
}
}

【问题讨论】:

  • 很高兴为您提供帮助。在 *,习惯于接受对解决您的问题贡献最大的答案。您可以通过单击答案投票附近的复选标记来做到这一点,使其变为纯绿色。

标签: java sql sqlexception


【解决方案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,因此不得使用 executeQuery。请改用executeUpdate()

【讨论】:

  • 谢谢伙计,但现在我有这个错误:异常 java.sql.SQLException:无法使用 executeQuery() 发出数据操作语句。 java.sql.SQLException:无法使用 executeQuery() 发出数据操作语句。在 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055) 在 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956) 在 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926 ) 在 com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1794) 的 com.mysql.jdbc.StatementImpl.checkForDml(StatementImpl.java:412)
  • 使用executeUpdate 而不是executeQuery
  • 你在general_news 表中有一个名为title 的列吗:) 如果你想粘贴CREATE TABLE 语句。
  • 谢谢 smink,我用 MyBatis 搜索了大约 3 个小时,试图找到我的错误......在一篇 5 年前的帖子中,你有一个答案:) '?当我需要时为我的字符串? SMH..