首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用JDBC将记录插入数据库时无法修复错误

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

Stack Overflow用户
提问于 2014-04-20 06:27:17
回答 3查看 914关注 0票数 0
代码语言:javascript
运行
复制
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”:无效标识符

代码语言:javascript
运行
复制
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()+‘’);

EN

回答 3

Stack Overflow用户

发布于 2014-04-20 06:43:28

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

代码语言:javascript
运行
复制
//Statement statement;

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

代码语言:javascript
运行
复制
String sql = "insert into Student_Info " +
                    "VALUES("+student.getName()+","+student.getRoll_no()+","+student.getAddress()+","+student.getPhone_no()+")";

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

票数 0
EN

Stack Overflow用户

发布于 2014-04-20 06:52:08

Error ORA-00904意味着甲骨文不知道标识符"GetPhone_NO“。”GetPhone_NO“看起来是在尝试从SQL中将名为”GetPhone_NO“的列插入”GetPhone_NO“表。因此,您应该再次检查SQL和table结构。

票数 0
EN

Stack Overflow用户

发布于 2014-04-20 06:52:42

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

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

插入student_info(名称、roll_no、地址、电话)值(“+ student.getName()+”、“+ student.getRoll_no()+”、"+student.getAddress()+“、”+student.getPhone_no()+“);

更好的是,如果您使用准备语句,如

尝试更改查询如下

插入student_info(姓名、roll_no、地址、电话)值(?、?、?)

然后设置参数值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23179187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档