java.sql.SQLException: 没有为参数 1 指定值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16723771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:47:37  来源:igfitidea点击:

java.sql.SQLException: No value specified for parameter 1

javamysqljdbc

提问by novat0

I have looked at other stackoverflow threads to get an answer for this problem. Mostly what I see is that it is caused by typos, although I cannot see any in this method. This method is called from another method in the same class and when run returns the error :

我查看了其他 stackoverflow 线程以获得此问题的答案。大多数我看到的是它是由拼写错误引起的,尽管我在这种方法中看不到任何内容。这个方法是从同一个类中的另一个方法调用的,运行时返回错误:

java.sql.SQLException: No value specified for parameter 1

java.sql.SQLException: 没有为参数 1 指定值

The code for my class is here:

我的课的代码在这里:

public void WriteTag(String tagPrefix, String tagName, String tagContent)
{
    try {
        String query = String.format("INSERT INTO %s(%s,%s) VALUES(?,?)", 
                tagPrefix, TAGNAME_COLUMN, TAGCONTENT_COLUMN);
        PreparedStatement sqlStatement = connection.prepareStatement(query);
        sqlStatement.setString(1, tagName);
        sqlStatement.setString(2, tagContent);
        //sqlStatement.executeUpdate();
    } catch(Exception e) {HandleException(e);}
}

I'm not really sure what is wrong here. The constants are properly defined elsewhere in the code. Does anyone see what I am doing wrong?

我不确定这里有什么问题。常量在代码的其他地方被正确定义。有没有人看到我做错了什么?

回答by Maciej Cygan

Try this

试试这个

String query = "Insert into foo (foo1,foo2) Values (?,?)";
PreparedStatement pst = connection.prepareStatement(query);
pst.setString(1, whatever);
pst.setString(2, whatever);
pst.executeUpdate();  // this is actually important in order to get data inserted into database. 

So technically hardcode the tablenames that are in the databse. and it should work.

所以在技术上硬编码数据库中的表名。它应该工作。