[英]invalid column index error in java while fetching data from oracle 10g database. Cant figure out whats wrong
代码段:单击按钮后,将调用actionevent
public void actionPerformed(ActionEvent e)
{
Function f = new Function();
函数是一个嵌套类,我曾用它来建立与数据库的连接。 最后还提供了函数类的代码片段。
ResultSet rs = null;
String Cid ="cust_id";
String Pno="cust_phone";
String cat="cust_cat";
String start_date="st_date";
String Adv_amt="adv";
String Adv_end="end_date";
String Address="addr";
t2是我用来获取客户名称的文本字段名称。 我想将此客户名称用作PK,以便从数据库中获取有关该客户的所有其他数据。
rs=f.find(t2.getText());
try{
if(rs.next())
{
t1.setText(rs.getString("cust_id"));
t3.setText(rs.getString("cust_phone"));
t4.setText(rs.getString("cust_cat"));
t5.setText(rs.getString("st_date"));
t6.setText(rs.getString("adv"));
t7.setText(rs.getString("end_date"));
t8.setText(rs.getString("addr"));
}
else
JOptionPane.showMessageDialog(null,"No data for this name");
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null,ex.getMessage());
}
}
这是嵌套在主类内部的Function类的代码片段:
class Function{
Connection con=null;
ResultSet rs= null;
PreparedStatement ps = null;
public ResultSet find(String s)
{
try
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
con = DriverManager.getConnection("jdbc:oracle:thin:@Localhost:1521:xe","system","qwerty");
ps= con.prepareStatement("Select * from gkkdb where cust_name='?'");
ps.setString(1,s);
rs= ps.executeQuery();
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null, ex.getMessage());
}
return rs;
}
}
请帮助解决问题。
不要放置参数占位符?
用单引号引起来。
这个:
ps = con.prepareStatement("Select * from gkkdb where cust_name='?'");
应该
ps = con.prepareStatement("Select * from gkkdb where cust_name = ?");
?
如果将其用单引号引起来,则不会被识别为占位符。
整理出绑定变量将解决您的直接问题。
您应该明确指定要选择的列,这样一来,您将只获得所需的列(以后可能会添加BLOB列),并且会以正确的顺序获得它们(有人可以在继续运行之前更改表创建脚本)另一个数据库实例,尽管您正在按名称查找列,但是只有在使用位置索引时,不同的顺序才会影响)。
同上另一个答案:绑定变量(即不加引号)
另外,“选择* from”绝不是一个好主意,请咨询您的DBA。
显然,例如您的代码,但是您应该确保在处理完所有资源后立即释放它们(连接,语句,结果集)。 使用Java 7 try-with-resources。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.