aU sUd
aU sUd

Reputation: 35

resultset in JDBC SQL

I have a GUI JDBC SQL project. I can read the information from my Database well but i don't know what is wrong with my create,update,delete method. Seem like some methods in resultSet doesn't work correctly. 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. Here is my error when i try to create a new 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)

Upvotes: 1

Views: 1283

Answers (2)

AGdev
AGdev

Reputation: 616

A default ResultSet object is not updatable and has a cursor that moves forward only. 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. 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) {
    }

Upvotes: 2

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.

Upvotes: 0

Related Questions