问题描述
我有以下用于将值插入数据库的Java代码
I have the following java code for inserting values to the database
当我尝试下面的代码时,
When i try the code below it works
st.executeUpdate("INSERT INTO USERT " + "VALUES ('1', 'Simpson', 'Mr', 'Springfield', '2001')");
但是当我尝试下面的代码时,出现错误
But when i try the code below i get an error
st.executeUpdate("INSERT INTO USERT (`USERID`, `FIRSTNAME`,`LASTNAME`,`EMAIL`,`PHONE`) VALUES ('2', 'james', 'john', 'myemail', 'myphone')");
此处在堆栈溢出时提供的大多数答案都涉及分号;
的字符放错位置.即链接1 ,,似乎对我没有帮助
Most of the answers provided here at stack overflow refer to a character misplacement of a semicolon ;
. i.e. link 1 , link 2, link 3 which does not seem to help me
这是我班上的完整代码
package dbproject;
import java.sql.*;
public class jdbcconnection {
public static void main(String[] args) {
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
java.sql.Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","data1","mypass");
Statement st=con.createStatement();
st.executeUpdate("INSERT INTO USERT (`USERID`, `FIRSTNAME`,`LASTNAME`,`EMAIL`,`PHONE`) VALUES ('2', 'james', 'john', 'myemail', 'myphone')");
//st.executeUpdate("INSERT INTO USERT " + "VALUES ('1', 'Simpson', 'Mr', 'Springfield', '2001')");
con.close();
}
catch(Exception e){
System.out.println(e);
}
}
}
我的执行查询可能有什么问题,还有什么可能导致错误?
What could be wrong with my execute query and what else could lead to the error?
推荐答案
MySQL和Oracle在标识符定义方面有一些细微差别.在MySQL中,未加引号的标识符可以以数字开头,并且在加引号的标识符中允许使用双引号.但是,Oracle标识符中均不允许使用这些方法.在MySQL中,引号是backtick
(`).如果设置了SQL模式ANSI_QUOTES,则也可以使用双引号将标识符引起来.在Oracle中,标识符用双引号引起来.
MySQL and Oracle have some minor differences in their definition of an identifier. In MySQL, an unquoted identifier may begin with a digit, and double quotation marks are allowed in a quoted identifier; however, neither of these is allowed in an Oracle identifier. In MySQL, the quote character is the backtick
(`). If the SQL mode ANSI_QUOTES is set, double quotes can also be used to quote the identifiers. In Oracle, identifiers are quoted using double quotation marks.
https://docs.oracle.com/cd/E12151_01/doc.150/e12155/oracle_mysql_compared.htm#i1026354
尝试;
st.executeUpdate("INSERT INTO USERT (USERID, FIRSTNAME,LASTNAME,EMAIL,PHONE) VALUES ('2', 'james', 'john', 'myemail', 'myphone')");
这篇关于java.sql.SQLSyntaxErrorException:ORA-00911:无效字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!