gpt4 book ai didi

java - 我正在尝试插入 sql,但它一直给我错误

转载 作者:行者123 更新时间:2023-12-02 01:31:36 25 4
gpt4 key购买 nike

我想通过java插入sql,但它给了我错误

public void addStudent(String name, int SID, String gender, String major, String nationality, int mobileNo,
String birthDate, int deptNo) {

try {
PreparedStatement stmt = conn.prepareStatement("insert into students values("+ SID+ ", '"+name+"','"
+ gender+"',"+mobileNo + ",TO_Date('"+birthDate+"', 'DD/MM/YYYY'), '"+ major+"', '"+nationality+"', null, "+deptNo+");");
stmt.execute();
} catch (SQLException e) {

e.printStackTrace();
return;

}
}

java.sql.SQLSyntaxErrorException: ORA-00933: SQL command not properly ended

最佳答案

使用PreparedStatement如下所示:

       try {
//passing placeholder
PreparedStatement stmt = conn.prepareStatement("insert into students values(?,?,?,?,?,?,?,?,?)");
//giving valuesto each placeholder
stmt.setInt(1,SID);
stmt.setString(2,name);
stmt.setString(3,gender);
stmt.setInt(4,mobileNo );
stmt.setDate(5,birthDate);
stmt.setString(6,major);
stmt.setString(7,nationality);
stmt.setString(8,null);
stmt.setInt(9,deptNo );
//executing update,insert value in table
stmt.executeUpdate();
} catch (SQLException e) {

e.printStackTrace();
return;

}

关于java - 我正在尝试插入 sql,但它一直给我错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55980740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com