异常java.sql.SQLException:参数索引超出范围(1>;参数数,即0)
我得到了这个错误:异常java.sql.SQLException:参数索引超出范围(1>;参数数,即0),java,sql,sqlexception,Java,Sql,Sqlexception,我得到了这个错误: 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.createSQLExc
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();
}
}
package com.in.RSSToNews;
导入java.net.URL;
导入java.util.ArrayList;
导入java.util.Date;
导入java.util.Iterator;
导入java.util.List;
导入java.net.URLConnection;
导入java.sql.Connection;
导入java.sql.DriverManager;
导入java.sql.PreparedStatement;
导入java.sql.ResultSet;
导入java.sql.Statement;
导入java.text.ParseException;
导入java.text.simpleDataFormat;
导入com.sun.syndication.feed.synd.SyndEntry;
导入com.sun.syndication.feed.syndd.SyndFeed;
导入com.sun.syndication.io.SyndFeedInput;
导入com.sun.syndication.io.XmlReader;
/**
*RSS/Atom提要类型中的Consumer类。
*
*@author Rbn
*/
公共类消费者{
合成饲料;
/**
*类构造函数
*
*@param url:要使用的url路径
*/
公共消费者(字符串url){
超级();
试一试{
URL feedUrl=新URL(URL);
SyndFeedInput=新的SyndFeedInput();
feed=input.build(新的XmlReader(feedUrl));
}捕获(例外情况除外){
例如printStackTrace();
System.out.println(“错误:+ex.getMessage());
}
}
/**
*打印方法
*向下滚动条目列表并显示提要标题、作者和描述
*/
作废打印(){
//源列表
List entradas=new ArrayList();
entradas=feed.getEntries();
尝试
{
//列表迭代器
迭代器it=entradas.Iterator();
Class.forName(“org.gjt.mm.mysql.Driver”);
Connection conexion=DriverManager.getConnection(“jdbc:mysql://localhost/newsamerica“,”根“,”123”);
如果(!conexion.isClosed())
{
while(it.hasNext())
{
SyndEntry entrada=it.next();
字符串标题=(entrada.getTitle());
字符串链接=(entrada.getLink());
字符串author=(entrada.getAuthor());
字符串描述=(“”+entrada.getDescription());
日期=(entrada.getPublishedDate());
字符串date2=date.toString();
字符串内容=(“”+entrada.getContents());
//description=description.replaceAll(“SyndContentImpl.value=“,”);
//System.out.println(text2);
//System.out.println(“Autor……:+entrada.getAuthor());
//System.out.println(“通知:“+entrada.getContents());
//语句st=conexion.createStatement();
PreparedStatement pstmt=conexion.prepareStatement(“插入通用新闻(标题、链接、作者、描述、日期、内容)值(“?”、“?”、“?”、“?”、“?”、“?”));
pstmt.setString(1,标题);
pstmt.设置管柱(2,连接);
pstmt.setString(3,作者);
pstmt.设置管柱(4,说明);
pstmt.setString(5,日期2);
pstmt.setString(6,内容);
结果集rs=pstmt.executeQuery();
rs.next();
}
}
conexion.close();
}
捕获(例外e)
{
//阿尔冈动量误差。
系统输出打印项次(“例外”+e);
e、 printStackTrace();
}
}
公共静态void main(字符串[]args){
消费者订阅源=新消费者(args[0]);
feed.print();
}
}
糟糕的格式设置。当使用参数时,您不需要输入?”
,而只需输入?
更改行:
PreparedStatement pstmt = conexion.prepareStatement("INSERT INTO general_news(id,title,link,author,description,date,content) VALUES ('?','?','?','?','?','?')");
到
这就是为什么他认为您有0个参数,但指定了第一个参数
还要注意,您正在执行一个
插入
,因此不能使用executeQuery。改为使用。谢谢你,但是现在我有一个错误:Excepcion 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.StatementImpl.checkForDml(StatementImpl.java:412)com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1794)使用executeUpdate
而不是executeQuery
。在general\u news
table:)中是否有名为title
的列,如果您希望粘贴CREATE table
语句。谢谢smink,我用MyBatis搜索了大约3个小时试图找到我的错误。。。在一篇5年前的帖子中,你得到了一个答案:)“?”当我需要的时候,你会给我的字符串?很高兴能帮忙。StackOverflow习惯于接受对解决问题贡献最大的答案。通过单击答案投票附近的复选标记,使其变为绿色。
PreparedStatement pstmt = conexion.prepareStatement("INSERT INTO general_news(id,title,link,author,description,date,content) VALUES (?,?,?,?,?,?)");