插入查询在JAVA中不起作用 [英] Insert query not working in JAVA
本文介绍了插入查询在JAVA中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在我的数据库中插入新值。但它不断抛出错误java.sql.SQLException:参数索引超出范围(1>参数个数,为0)。
我的代码是:
Hi,
I am trying to insert new values in my database. but it keeps throwing a error "java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0)."
My code is:
String sql;
//String temp = jComboBox1.getSelectedItem();
sql = "Insert into mytable (s_id,fname,lname,enrolled) values(?,?,?,?)";
ps.setString(1,t_id.getText());
ps.setString(2,t_fname.getText());
ps.setString(3,t_lname.getText());
ps.setString(4,(String) jComboBox1.getSelectedItem());
ps = con.prepareStatement(sql);
ps.execute();
JOptionPane.showMessageDialog(null, "Record Inserted");
update_table();
}
catch(SQLException er){
System.out.println(er);
JOptionPane.showMessageDialog(null,er);
}
请指导。我无法找到问题
Please guide. I'm unable to locate the problem
推荐答案
参见http://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html [ ^ ]。在添加参数之前,您需要使用SQL语句创建 ps
。
See http://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html[^]. You need to use the SQL statement to create ps
before you add the parameters.
Hi
以下链接可能有帮助你解决了这个问题。
http://stackoverflow.com/questions/2975797/exception-java-sql-sqlexception-parameter-index-out-of-range-1-number-of-par [ ^ ]
请检查s_id是否为主键并自动增加。
如果是,那么你不需要通过插入查询来提供数据..
试试这个..
谢谢
Hi The below link might help you for sort out the issue.
http://stackoverflow.com/questions/2975797/exception-java-sql-sqlexception-parameter-index-out-of-range-1-number-of-par[^]
Please check whether the s_id is Primary key and auto increment or not.
If it is,then you don't need to give that data through insert query..
Try this..
Thanks
ps。 setString(1,t_id.getText());
ps.setString(2,t_fnam e.getText());
ps.setString(3,t_lname.getText());
ps.setString(4,(String)jComboBox1.getSelectedItem() );
--------------更改为-------- ---------
ps.setString(0,t_id.getText());
ps.setString(1,t_fname.getText()) ;
ps.setString(2,t_lname.getText());
ps.setString(3,(String)jComboBox1.getSelectedItem());
试试
ps.setString(1,t_id.getText());
ps.setString(2,t_fname.getText());
ps.setString(3,t_lname.getText());
ps.setString(4,(String) jComboBox1.getSelectedItem());
--------------change to-----------------
ps.setString(0,t_id.getText());
ps.setString(1,t_fname.getText());
ps.setString(2,t_lname.getText());
ps.setString(3,(String) jComboBox1.getSelectedItem());
try
这篇关于插入查询在JAVA中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文