java.sql.SQLSyntaxErrorException。ORA-00947:没有足够的值

问题描述 投票:0回答:1
System.out.println("Enter the code: ");
Code=input.next();                       
System.out.println("Enter the title: ");
Title=input.next();   
System.out.println("Enter the semster: ");
Semester=input.next(); 
System.out.println("Enter the year: ");
Year=input.next();           
System.out.println("Enter the grade: ");
Grade=input.next(); 

String insertStatement ="insert into Courses values('"+Code+"'+'"+Title+"'+'"+Semester+"'+'"+Year+"'+'"+Grade+"')";
System.out.println(insertStatement);
s.execute(insertStatement); 
continue;

当我插入值并运行代码时,它显示我

insert into Courses values('GET'+'CS'+'Fall'+'2016'+'C+')

错误

java.sql.SQLSyntaxErrorException: ORA-00947: not enough values

谁能给我解释一下,谢谢

java mysql sql database oracle
1个回答
0
投票

更新你的SQL语句,使用这样的列名。

String insertStatement = "INSERT INTO Courses (codeColName, titleColName, semesterColName, yearColName, gradeColName)"
                + "VALUES ('" + code + "', '" + title + "', '" + semester + "', '" + year + "', '" + grade + "')";

,最好的做法是使用 PreparedStatement 以避免SQL注入


0
投票

正确准备查询,你的双引号乱了。

你需要这样的东西。

"insert into Courses values('"+Code+"'"+"'"+Title+"'"+"'"+Semester+"'"+"'"+Year+"'"+"'"+Grade+"')"

但是是的,这很容易引起SQL注入,所以最好使用PreparedStatement。

© www.soinside.com 2019 - 2024. All rights reserved.