我很困惑使用日期可能有人指出我正确的方向为下面的代码,它抛出以下异常:
org.h2.jdbc.JdbcSQLException:参数值'11'无效 “parameterIndex”[90008-193]
import java.util.logging.Level;
import java.util.logging.Logger;
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalDate;
import java.time.ZoneId;
public class Database {
public static void main(String[] args) throws SQLException {
Connection conn = null;
Statement st = null;
String URL = "jdbc:h2:~/registDB";
String USER = "admin";
String PASSWORD = "password";
ZoneId z = ZoneId.systemDefault() ;
LocalDate currentDate = LocalDate.now(z);
LocalDate expiration = currentDate.plusDays(inputFld.getText()); //JTextField.getText()
java.sql.Date expirationDate = java.sql.Date.valueOf(expiration);
try {
Class.forName("org.h2.Driver").newInstance();
conn = DriverManager.getConnection(URL,USER,PASS);
String sql = "INSERT INTO data
(fullName,regNum,itemName,note,zHemjee,fee,
time,date,totalPay,expirationDate)"
+ "VALUES"
+ "(?,?,?,?,?,?,?,?,?,?)";
pst = conn.prepareStatement(sql);
// 1st index left not being modified on purpose which is ID auto_increment-ed
pst.setString(2, getFullName()); // Get methods are
// JTextField.getText() casted into proper data types
// except the 11th row which is throwing SQLException
pst.setString(3, getRegNum());
pst.setString(4, getItemName());
pst.setString(5, getNote());
pst.setInt(6, getzHemjee());
pst.setInt(7, getFee());
pst.setInt(8, getTime());
pst.setDate(9, java.sql.Date.valueOf(LocalDate.now()));
pst.setDouble(10, getTotalPay());
pst.setDate(11, expirationDate);
pst.executeUpdate();
pst.close();
conn.close();
} catch (ClassNotFoundException | SQLException ex) {
Logger.getLogger(database.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
以下是我的SQL语句,用它的列类型创建一个表:
CREATE TABLE IF NOT EXISTS data "
+"(id INT NOT NULL AUTO_INCREMENT," //int
+ " fullname varchar(30)," //String
+ " regNum varchar(10)," //String
+ " itemName varchar(30)," //String
+ " note varchar(30)," //String
+ " zHemjee int," //int
+ " fee number," //int
+ " time INT," //int
+ " date DATE," //Date
+ " totalPay BIGINT," //int
+ " expirationDate DATE);"); //Date
答案 0 :(得分:1)
问题在于您是否输入了错误的索引。 你应该从1开始而不是2 只有10'?'你有第11个不存在的参数 这段代码应该有效:
String sql = "INSERT INTO data
(ovogNer,regNum,itemName,note,zHemjee,hvv,
hugatsaa,date,totalPay,expirationDate)"
+ "VALUES"
+ "(?,?,?,?,?,?,?,?,?,?)";
pst = conn.prepareStatement(sql);
pst.setString(1, getFullName());
pst.setString(2, getRegNum());
pst.setString(3, getItemName());
pst.setString(4, getNote());
pst.setInt(5, getzHemjee());
pst.setInt(6, getFee());
pst.setInt(7, getTime());
pst.setDate(8, java.sql.Date.valueOf(LocalDate.now()));
pst.setDouble(9, getTotalPay());
pst.setDate(10, expirationDate);
pst.executeUpdate();
你也很奇怪" +"设置整数时。
答案 1 :(得分:1)
Bojan Petkovic的回答是正确的。话虽如此,每当我有两个或三个以上的参数时,我总是使用索引变量来防止错误。像这样:
int index = 1;
String sql = "INSERT INTO data
(ovogNer,regNum,itemName,note,zHemjee,hvv,
hugatsaa,date,totalPay,expirationDate)"
+ "VALUES"
+ "(?,?,?,?,?,?,?,?,?,?)";
pst = conn.prepareStatement(sql);
pst.setString(index++, getFullName());
pst.setString(index++, getRegNum());
pst.setString(index++, getItemName());
pst.setString(index++, getNote());
pst.setInt(index++, getzHemjee());
pst.setInt(index++, getFee());
pst.setInt(index++, getTime());
pst.setDate(index++, java.sql.Date.valueOf(LocalDate.now()));
pst.setDouble(index++, getTotalPay());
pst.setDate(index++, expirationDate);
pst.executeUpdate();
当然,这种方法取决于你把所有东西都按照正确的顺序(你做了)。另一个选择是使内部常量适用于数字。当你达到这一点时,你就到了一个真正需要使用框架进行SQL访问的地方。 :)
[编辑]或至少代码是"正确"在某种意义上,代码按照描述的方式工作。