JAVA数据库基本操作,增删改查

package mypack; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; public class DbOper { // 查询多行记录 public ArrayList select() { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; ArrayList al = new ArrayList(); try { conn = DbConn.getConn(); pstmt = conn.prepareStatement(”select * from titles”); rs = pstmt.executeQuery(); while (rs.next()) { Titles t = new Titles(); t.setTitleid(rs.getString(1)); t.setTitle(rs.getString(2)); al.add(t); } } catch (SQLException e) { // TODO 自动生成 catch 块 e.printStackTrace(); } finally { try { if (rs != null) rs.close(); if (pstmt != null)pstmt.close(); if (conn != null)conn.close(); } catch (SQLException e) { // TODO 自动生成 catch 块 e.printStackTrace(); } } return al; } //查询单个对象 public Titles selectOne(String titleid){ Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; Titles t = new Titles(); try { conn = DbConn.getConn(); pstmt = conn.prepareStatement(”select * from titles where title_id=?”); pstmt.setString(1,titleid); rs = pstmt.executeQuery(); while (rs.next()) { t.setTitleid(rs.getString(1)); t.setTitle(rs.getString(2)); } } catch (SQLException e) { // TODO 自动生成 catch 块 e.printStackTrace(); } finally { try { if (rs != null) rs.close(); if (pstmt != null)pstmt.close(); if (conn != null)conn.close(); } catch (SQLException e) { // TODO 自动生成 catch 块 e.printStackTrace(); } } return t; } //增加记录 public boolean insert(Titles t){ Connection conn = null; PreparedStatement pstmt=null; boolean b = false; try { conn = DbConn.getConn(); pstmt = conn.prepareStatement(”insert into titles(title_id,title) values (?,?)”); pstmt.setString(1,t.getTitleid()); pstmt.setString(2,t.getTitle()); int n = pstmt.executeUpdate(); if(n==1) b=true; } catch (SQLException e) { // TODO 自动生成 catch 块 e.printStackTrace(); }finally{ try { if(pstmt!=null)pstmt.close(); if(conn!=null)conn.close(); } catch (SQLException e) { // TODO 自动生成 catch 块 e.printStackTrace(); } } return b; } //删除记录 public boolean delete(String titleid){ Connection conn = null; PreparedStatement pstmt=null; boolean b = false; try { conn = DbConn.getConn(); pstmt = conn.prepareStatement(”delete from titles where title_id=?”); pstmt.setString(1,titleid); int n = pstmt.executeUpdate(); if(n==1) b=true; } catch (SQLException e) { // TODO 自动生成 catch 块 e.printStackTrace(); }finally{ try { if(pstmt!=null)pstmt.close(); if(conn!=null)conn.close(); } catch (SQLException e) { // TODO 自动生成 catch 块 e.printStackTrace(); } } return b; } //修改表 public boolean update(Titles t){ Connection conn = null; PreparedStatement pstmt=null; boolean b = false; try { conn = DbConn.getConn(); pstmt = conn.prepareStatement(”update titles set title=? where title_id=?”); pstmt.setString(1,t.getTitle()); pstmt.setString(2,t.getTitleid()); int n = pstmt.executeUpdate(); if(n==1) b=true; } catch (SQLException e) { // TODO 自动生成 catch 块 e.printStackTrace(); }finally{ try { if(pstmt!=null)pstmt.close(); if(conn!=null)conn.close(); } catch (SQLException e) { // TODO 自动生成 catch 块 e.printStackTrace(); } } return b; } }

你可能感兴趣的:(JAVA数据库基本操作,增删改查)