使用JDBC将记录插入数据库时​​无法修复错误

时间:2014-04-20 06:27:17

标签: java jdbc

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:“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 =“insert into Student_Info(name,roll_no,address,phone_no)VALUES('”+ student.getName()+“',”+ student.getRoll_no()+“,'”+ student.getAddress( )+ “”, ' ”+ student.getPhone_no()+“')”;

3 个答案:

答案 0 :(得分:0)

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

//Statement statement;

正如@putaro早先指出的那样,你需要引用SQL查询的某些部分。

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

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我发现代码中存在两个问题。

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

    “INSERT INTO student_info(name,roll_no,address,phone)VALUES(”+   student.getName()+“,”+   student.getRoll_no()+ “ ”+ student.getAddress()+“, ”+ student.getPhone_no()+“)”;

    如果你使用像

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

    尝试更改

    之类的查询

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

    然后设置参数值。