简体   繁体   English

JDBC SQL中的结果集

[英]resultset in JDBC SQL

I have a GUI JDBC SQL project. 我有一个GUI JDBC SQL项目。 I can read the information from my Database well but i don't know what is wrong with my create,update,delete method. 我可以很好地从数据库中读取信息,但是我不知道我的create,update,delete方法有什么问题。 Seem like some methods in resultSet doesn't work correctly. 似乎resultSet中的某些方法无法正常工作。 My code is below. 我的代码如下。

public Person create(Person p){
        try {
            rs.moveToInsertRow();
            rs.updateInt("PersonID", p.getPersonID());
            rs.updateString("firstName", p.getFirstName());
            rs.updateString("middleName", p.getMiddleName());
            rs.updateString("lastName", p.getLastName());
            rs.updateString("email", p.getEmail());
            rs.updateString("phone",p.getPhone());
            rs.insertRow();
            rs.moveToCurrentRow();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return p;
    }// end of create method
    public Person update(Person p){
        try {
            rs.updateString("firstName", p.getFirstName());
            rs.updateString("middleName", p.getMiddleName());
            rs.updateString("lastName", p.getLastName());
            rs.updateString("email", p.getEmail());
            rs.updateString("phone",p.getPhone());
            rs.updateRow();
            rs.moveToCurrentRow();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return p;
    }//end of update method
    public void delete(){
        try {
            rs.moveToCurrentRow();
            rs.deleteRow();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }//end of delete method

thanks for your reading. 感谢您的阅读。

public PersonBean() {
        try { 
            Class.forName(JDBC_DRIVER);
            Connection con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);
            sm = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            rs = sm.executeQuery("Select * From Person");
        } catch (ClassNotFoundException | SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }//end of PersonBean

my connection to SQL is OK because i can read information from SQL but i cant write data to SQL. 我与SQL的连接正常,因为我可以从SQL读取信息,但无法将数据写入SQL。 Here is my error when i try to create a new Person. 当我尝试创建一个新的Person时,这是我的错误。

com.microsoft.sqlserver.jdbc.SQLServerException: The result set is not updatable.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.throwNotUpdatable(SQLServerResultSet.java:436)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.verifyResultSetIsUpdatable(SQLServerResultSet.java:447)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.moveToInsertRow(SQLServerResultSet.java:4350)
    at PersonBean.create(PersonBean.java:29)

A default ResultSet object is not updatable and has a cursor that moves forward only. 默认的ResultSet对象是不可更新的,并且具有仅向前移动的光标。 Thus, you can iterate through it only once and only from the first row to the last row. 因此,您只能从第一行到最后一行迭代一次。 It is possible to produce ResultSet objects that are scrollable and/or updatable. 可以产生可滚动和/或可更新的ResultSet对象。 An updatable result set allows modification to data in a table through the result set. 可更新的结果集允许通过结果集修改表中的数据。 The following code makes a result set that is scrollable and insensitive to updates by others: 以下代码使结果集可滚动并且对其他人的更新不敏感:

 try {
        // Create a statement that will return updatable result sets
        Statement stmt = connection.createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE, 
                    ResultSet.CONCUR_UPDATABLE);

        //Primary key EmployeeID must be specified 
        //so that the result set is updatable
        ResultSet resultSet = stmt.executeQuery(
                    "SELECT EmployeeID, Name, Office FROM employees");
    } catch (SQLException e) {
    }

You want to use following example and check this codes with your codes in project: 您要使用以下示例,并将此代码与项目中的代码一起检查:

//database connector file example (com.mysql.jdbc.Driver)

try {
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    String DB_URL = "jdbc:mysql://localhost:3306/dbname";
    String DB_USER = root;
    String DB_PASS = "";
    Connection con = DriverManager.getConnection(DB_URL, DB_USER, DB_PASS);

    Statement stmt = con.createStatement();
    stmt.executeQuery("Select * From Person");
    ResultSet resultSet = stmt.getResultSet();

    while(resultSet .next()){
       System.out.print(resultSet.getString("fieldName");
       //and other your field to display
    }

    resultSet.close();
    stmt.close()


} catch(Exceptoin e) {
    ....
}

There are many examples of this approach for insert, update and delete. 有许多用于插入,更新和删除此方法的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM