从oracle 10g数据库获取数据时,java中出现无效列索引错误。我想不出是怎么回事
代码段: 单击按钮时,将调用actionevent从oracle 10g数据库获取数据时,java中出现无效列索引错误。我想不出是怎么回事,java,oracle,jdbc,Java,Oracle,Jdbc,代码段: 单击按钮时,将调用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="s
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,从DB中获取关于该客户的所有其他数据
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());
}
}
以下是主类中嵌套类函数的代码段:
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 = ?");
如果将
?
括在单引号中,则不会将其识别为占位符 整理bind变量将解决您的直接问题
您应该明确指定要选择的列,这样您只会得到您需要的(稍后可能会有人添加BLOB列),并且您将以正确的顺序获得它们(在另一个DB实例上运行之前,可能会有人更改表创建脚本,尽管您是按名称查找列,但只有在使用位置索引时,不同的顺序才会产生影响)
另一个答案同上:绑定变量(即无引号)
另外,“select*from”从来都不是一个好主意,请咨询您的DBA
显然,您的代码就是一个例子,但是您应该确保在完成任何资源(连接、语句、结果集)后立即释放它们