我有一個問題與此功能java.sql.SQLException:參數索引超出範圍(1>參數數量,爲0)。
String القيمة=jTextField3.getText();
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/cars","root","123456");
pstmt=conn.prepareStatement("select القيمة from eradat WHERE DATE_FORMAT(التاريخ, \"%m-%Y\") = \"01-2015\";");
pstmt.setString(1,القيمة);
rs=pstmt.executeQuery();
while(rs.next()){
jTextField3.setText(rs.getString("القيمة"));
}
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
finally{
try{
conn.close();
pstmt.close();
rs.close();
}
catch(Exception e){
}
}
我獲得錯誤消息:值java.sql.SQLException:參數索引超出範圍(1>參數的數量,這是0)。
您的SQL沒有任何參數標記(''?),所以沒有參數,因此「參數的數量」爲0 – Andreas
如果你總是從你的結果集中檢索'rs.getString(「القيمة」)',你不需要這個'pstmt.setString(1,القيمة)'行。 – learningloop