日期:2014-05-17 浏览次数:20746 次
public class UserDao { public void get(String username){ Connection conn=null; try { conn=OracleConnectionUtil.getConnection(); conn.setAutoCommit(false); conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); PreparedStatement pstat = conn.prepareStatement("select * from user2 where username=?"); pstat.setString(1,username); ResultSet rs = pstat.executeQuery(); if(rs.next()){ System.out.println(rs.getString(2)); } rs.close(); pstat.close(); update(username,"2dddddddd");//更新上面查询的字段 conn.commit(); } catch (SQLException e) { e.printStackTrace(); }finally{ if(conn!=null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } public void insert(String username, String password){ Connection conn=null; try { conn=OracleConnectionUtil.getConnection(); conn.setAutoCommit(false); PreparedStatement pstat = conn.prepareStatement("insert into user2 values(?,?)"); pstat.setString(1, username); pstat.setString(2, password); pstat.execute(); pstat.close(); conn.commit(); } catch (SQLException e) { e.printStackTrace(); }finally{ if(conn!=null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } public void update(String username, String password){ Connection conn=null; try { conn=OracleConnectionUtil.getConnection(); conn.setAutoCommit(false); PreparedStatement pstat = conn.prepareStatement("update user2 set password=? where username=?"); pstat.setString(2, username); pstat.setString(1, password); pstat.execute(); pstat.close(); conn.commit(); } catch (SQLException e) { e.printStackTrace(); }finally{ if(conn!=null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } public static void main(String[] args) { UserDao dao = new UserDao(); dao.get("u1"); } }