public class StudentDataPersistence {
public void insertStudentInfo(Student student) {
String url = "jdbc:oracle:thin:@localhost:1521:XE";
String username = "system";
String password = "Data03@";
Connection connection = null;
//Statement statement = null;
try {
//Step 1 : Register JDBC driver
Class.forName("oracle.jdbc.driver.OracleDriver");
//Step 2 : Open a connection
System.out.println("Connecting to a selected database...");
connection = DriverManager.getConnection(url, username, password);
if (connection != null) {
System.out.println("Connected to oracle");
}
//Step 3 : Write code to map Java Object to the Student_Info table
System.out.println("Inserting records into the database");
statement = connection.createStatement();
String sql = "insert into Student_Info " +
"VALUES(student.getName(),student.getRoll_no(),student.getAddress(),student.getPhone_no())";
statement.executeUpdate(sql);
System.out.println("Inserted student information into the database");
} catch (SQLException se) {
//handle errors for JDBC
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
//Handle errors for Class.forName
} finally {
System.out.println("Inside the finally block");
//finally block used to close resources
try {
statement.close();
} catch (SQLException se) {
se.printStackTrace();
}
try {
connection.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
System.out.println("!GoodBye");
}
public static void main(String[] args) {
Student student = new Student("Bavin", 1, "Umar Nagar", "89898989809");
StudentDataPersistence obj = new StudentDataPersistence();
obj.insertStudentInfo(student);
}
}
它显示的错误是:
连接到选定的数据库..。连接到oracle,将记录插入数据库java.sql.SQLException: ORA-00904:“学生”.“GETPHONE_NO”:无效标识符
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:189)
at oracle.jdbc.ttc7.TTIoer.processError(TTIoer.java:242)
at oracle.jdbc.ttc7.Oall7.receive(Oall7.java:554)
at oracle.jdbc.ttc7.TTC7Protocol.doOall7(TTC7Protocol.java:1478)
at oracle.jdbc.ttc7.TTC7Protocol.parseExecuteFetch(TTC7Protocol.java:888)
at oracle.jdbc.driver.OracleStatement.executeNonQuery(OracleStatement.java:2076)
at oracle.jdbc.driver.OracleStatement.doExecuteOther(OracleStatement.java:1986)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:2697)
at oracle.jdbc.driver.OracleStatement.executeUpdate(OracleStatement.java:1035)
at org.core.hibernate.reason.StudentDataPersistence.insertStudentInfo(StudentDataPersistence.java:52)
at org.core.hibernate.reason.StudentDataPersistence.main(StudentDataPersistence.java:80)
在最后一个块里面!GoodBye
所有的回答都是错误的(你们中那些用甲骨文来解释的答案)。请在发帖前先看一看。当我发布另一条关于同样内容的帖子时,我得到了正确的答案:
String query = "insert insert Student_Info(name,roll_no,address,phone_no)值“(‘+student.getName()+”,"+student.getRoll_no()+",’+student.getAddress()+‘,’+student.getPhone_no()+‘’);
发布于 2014-04-20 06:43:28
您已经注释掉了语句对象定义。因此,当您使用语句对象时,它是未知的。取消这一行的注释:
//Statement statement;
正如前面@putaro所指出的,您需要引用SQL查询的某些部分。
String sql = "insert into Student_Info " +
"VALUES("+student.getName()+","+student.getRoll_no()+","+student.getAddress()+","+student.getPhone_no()+")";
这是将实际的对象值插入到查询中。引文中的内容将按原样插入。
发布于 2014-04-20 06:52:08
Error ORA-00904意味着甲骨文不知道标识符"GetPhone_NO“。”GetPhone_NO“看起来是在尝试从SQL中将名为”GetPhone_NO“的列插入”GetPhone_NO“表。因此,您应该再次检查SQL和table结构。
发布于 2014-04-20 06:52:42
我看到代码中有两个问题。
插入student_info(名称、roll_no、地址、电话)值(“+ student.getName()+”、“+ student.getRoll_no()+”、"+student.getAddress()+“、”+student.getPhone_no()+“);
更好的是,如果您使用准备语句,如
尝试更改查询如下
插入student_info(姓名、roll_no、地址、电话)值(?、?、?)
然后设置参数值。
https://stackoverflow.com/questions/23179187
复制相似问题