Oracle上的JDBC更新无法提交

时间:2014-04-02 04:51:35

标签: oracle jdbc sql-update

我有JDBC Dao对象,并使用PreparedStatements在我的数据库中的表中执行UPDATE行。

我有其他方法,如SELECT和INSERT成功(插入提交工作)。 但是更新,只是不提交更改(根本不起作用)。虽然相同的UPDATE语句可以直接从Oracle SQLServer运行。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class StaffDAO {
    private Connection conn;

    public StaffDAO() {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
        } catch (ClassNotFoundException e) {
            System.out.println("Oracle Driver not found");
            System.exit(0);
        }
        try {
            conn = DriverManager.getConnection(
                    "jdbc:oracle:thin:@db01.xxxdev.com:1521:training",
                    "training", "training");
        } catch (SQLException e) {
            System.out.println("Driver manager failed");
        }
    }

    public ResultSet getAllResultSet() {
        String sql = "select * from ben_staff";
        Statement stmt = null;
        ResultSet rs = null;
        try {
            stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            rs = stmt.executeQuery(sql);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return rs;

    }

    public Staff viewEmployee(String id) throws Exception {
        Staff st = new Staff();
        String sql = "SELECT * from BEN_STAFF where BEN_STAFF.id =\'" + id
                + "\'";
        // String psql = "SELECT * FROM BEN_STAFF WHERE ID = ?";

        Statement statement = null;
        // PreparedStatement pstatement = null;
        try {
            statement = conn.createStatement();
            // pstatement = conn.prepareStatement(psql);
            // pstatement.setString(1, id);
        } catch (SQLException e) {
            System.out.println("Create Statement failed");
            System.exit(1);
        }
        ResultSet rs = null;
        try {
            rs = statement.executeQuery(sql);
            // rs = pstatement.executeQuery();

            while (rs.next()) {
                st.setId(rs.getString("ID"));
                st.setLastName(rs.getString("LASTNAME"));
                st.setFirstName(rs.getString("FIRSTNAME"));
                st.setMi(rs.getString("MI"));
                st.setAddress(rs.getString("ADDRESS"));
                st.setCity(rs.getString("CITY"));
                st.setState(rs.getString("STATE"));
                st.setTelephone(rs.getString("TELEPHONE"));
                st.setEmail(rs.getString("EMAIL"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                statement.close();
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return st;
    }

    public boolean insert(String id, String last, String first, String mi,
            String address, String city, String state, String telephone,
            String email) {
        PreparedStatement pstmt = null;
        String psql = "insert into ben_staff (id, lastname, firstname, mi, address, city, state, telephone, email)"
                + "values (?,?,?,?,?,?,?,?,?)";

        try {
            pstmt = conn.prepareStatement(psql);
            // pstmt.setString(1,st.getId());
            // pstmt.setString(2, st.getLastName());
            // pstmt.setString(3, st.getFirstName());
            pstmt.setString(1, id);
            pstmt.setString(2, last);
            pstmt.setString(3, first);
            pstmt.setString(4, mi);
            pstmt.setString(5, address);
            pstmt.setString(6, city);
            pstmt.setString(7, state);
            pstmt.setString(8, telephone);
            pstmt.setString(9, email);
            pstmt.executeUpdate();
            conn.commit();
        } catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        } finally {
            try {
                pstmt.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return true;
    }

    public boolean update(String id, String last, String first, String mi,
            String address, String city, String state, String telephone,
            String email) {
        PreparedStatement pstmt = null;
        String psql = "update ben_staff set lastname=?, firstname=?, mi=?, address=?, city=?, state=?,"
                + " telephone=?, email=? where id=?";

        try {
            pstmt = conn.prepareStatement(psql);
            pstmt.setString(1, last);
            pstmt.setString(2, first);
            pstmt.setString(3, mi);
            pstmt.setString(4, address);
            pstmt.setString(5, city);
            pstmt.setString(6, state);
            pstmt.setString(7, telephone);
            pstmt.setString(8, email);
            pstmt.setString(9, id);
            pstmt.executeUpdate();
            conn.commit();
        } catch (SQLException ex) {
            ex.printStackTrace();
            try{
                conn.rollback();
            } catch (SQLException exx){
                System.out.println("Update Rollback Failed");
            }
            return false;
        } finally {
            try {
                pstmt.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return true;
    }

    public void close() {
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}

1 个答案:

答案 0 :(得分:1)

pstmt.executeUpdate();返回什么?这会告诉你正在更新多少行。像

这样的东西
int numRows = pstmt.executeUpdate();
System.out.println( "Update modified " + numRows + " rows." );

我的猜测是你的更新实际上并没有修改任何行。这意味着传入的id不正确。请记住,默认情况下,SQL Server中的字符串比较不区分大小写,而在Oracle中默认区分大小写。