【问题标题】:Unable to fix the error while inserting records into database using JDBC使用 JDBC 将记录插入数据库时​​无法修复错误
【发布时间】:2014-04-20 06:27:17
【问题描述】:
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);
    }

}

它显示的错误:

正在连接到选定的数据库... 连接到甲骨文 将记录插入数据库 java.sql.SQLException: ORA-00904: "STUDENT"."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)

在 finally 块内 !再见

所有的答案(你们中那些用 oracle 查询来说明它的人)都是错误的。 请在发布之前查看它。 当我发布另一个关于相同的主题时,我得到了正确的:

String query = "插入Student_Info(name,roll_no,address,phone_no) VALUES('"+student.getName()+"',"+student.getRoll_no()+",'"+student.getAddress( )+"','"+student.getPhone_no()+"')";

【问题讨论】:

    标签: java jdbc


    【解决方案1】:

    您已注释掉您的 Statement 对象定义。因此,当您使用它时,语句对象是未知的。 取消注释这一行:

    //Statement statement;
    

    正如@putaro 之前指出的,您需要引用 SQL 查询的某些部分。

    String sql = "insert into Student_Info " +
                        "VALUES("+student.getName()+","+student.getRoll_no()+","+student.getAddress()+","+student.getPhone_no()+")";
    

    这是在查询中插入实际的对象值。引用中的内容将按原样插入。

    【讨论】:

      【解决方案2】:

      错误 ORA-00904 表示 Oracle 不知道标识符“STUDENT”。“GETPHONE_NO”看起来您正试图从 SQL 中将一些值插入名为“GetPhone_NO”的列到表“Student”。所以你应该再次检查你的 SQL 和表结构

      【讨论】:

        【解决方案3】:

        我看到代码中有两个问题。

        1. 目前您的代码在进行查询时未使用学生对象。所有 student.getName() 等调用都被视为纯字符串,而不是返回适当值的方法调用。
        2. 其次,最好按以下形式编写查询。由于表的结构,它将避免愚蠢的错误。

        "INSERT INTO student_info(name,roll_no,address,phone) VALUES("+ 学生.getName()+","+ student.getRoll_no()+","+student.getAddress()+","+student.getPhone_no()+")";

        如果你使用像

        这样的准备好的语句,那就更好了

        尝试更改查询,如

        “INSERT INTO student_info(name,roll_no,address,phone) VALUES(?,?,?,?)”

        然后设置参数值。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-08-15
          • 1970-01-01
          • 2017-09-22
          • 2013-08-25
          • 2021-03-11
          • 2014-01-29
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多