package com.neusoft.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DbUtils { private DbUtils(){} private static Connection conn; public static Connection getConnection() { try { DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); //2.连接数据库,获取数据库连接对象 String url="jdbc:oracle:thin:@zhangyahui:1521:oracle"; String user="scott"; String password="0000"; conn=DriverManager.getConnection(url,user,password); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } public static void closeConnection() { try { if(conn!=null) { conn.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void closeStatement(Statement stmt) { try { if(stmt!=null) { stmt.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void closeResultSet(ResultSet rs) { //4.关闭数据库 try { if(rs!=null) { rs.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
package com.neusoft.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DbUtils { private DbUtils(){} private static Connection conn; public static Connection getConnection() { try { DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); //2.连接数据库,获取数据库连接对象 String url="jdbc:oracle:thin:@zhangyahui:1521:oracle"; String user="scott"; String password="0000"; conn=DriverManager.getConnection(url,user,password); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } public static void closeConnection() { try { if(conn!=null) { conn.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void closeStatement(Statement stmt) { try { if(stmt!=null) { stmt.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void closeResultSet(ResultSet rs) { //4.关闭数据库 try { if(rs!=null) { rs.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } package com.neusoft.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; //实现类 public class DeptDaoImpl implements DeptDaoInterface { @Override public void deleteDept(int deptno) { // TODO Auto-generated method stub PreparedStatement pstmt=null; Dept dept=new Dept(); try{ Connection conn=DbUtils.getConnection(); String sql="delete from forjava where ID=?"; pstmt=conn.prepareStatement(sql); pstmt.setInt(1, deptno); //pstmt=conn.prepareStatement(sql); pstmt.executeUpdate(); }catch(SQLException ex) { ex.printStackTrace(); }finally { DbUtils.closeStatement(pstmt); DbUtils.closeConnection(); } } @Override public List<Dept> getAllDepts() { PreparedStatement pstmt=null; ResultSet rs=null; List<Dept> depts=new ArrayList<Dept>(); try{ Connection conn=DbUtils.getConnection(); String sql="select * from forjava"; pstmt=conn.prepareStatement(sql); rs= pstmt.executeQuery(); while(rs.next()) { Dept dept=new Dept(); dept.setID(rs.getInt("ID")); dept.setName(rs.getString("NAME")); System.out.println(rs.getInt("ID")+" "+rs.getString("NAME")); depts.add(dept); } }catch(SQLException ex) { ex.printStackTrace(); }finally { DbUtils.closeResultSet(rs); DbUtils.closeStatement(pstmt); DbUtils.closeConnection(); } return depts; } @Override public Dept getDeptByDeptNo(int deptno) { // TODO Auto-generated method stub return null; } @Override public void insertDept(Dept dept) { // TODO Auto-generated method stub PreparedStatement pstmt=null; try{ Connection conn=DbUtils.getConnection(); String sql="insert into forjava(ID,name) values(?,?)"; pstmt=conn.prepareStatement(sql); pstmt.setInt(1, dept.getID()); pstmt.setString(2, dept.getName()); pstmt.executeUpdate(); }catch(SQLException ex) { ex.printStackTrace(); }finally { DbUtils.closeStatement(pstmt); DbUtils.closeConnection(); } } @Override public void updateDept(Dept dept) { // TODO Auto-generated method stub } }
package com.neusoft.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DbUtils { private DbUtils(){} private static Connection conn; public static Connection getConnection() { try { DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); //2.连接数据库,获取数据库连接对象 String url="jdbc:oracle:thin:@zhangyahui:1521:oracle"; String user="scott"; String password="0000"; conn=DriverManager.getConnection(url,user,password); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } public static void closeConnection() { try { if(conn!=null) { conn.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void closeStatement(Statement stmt) { try { if(stmt!=null) { stmt.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void closeResultSet(ResultSet rs) { //4.关闭数据库 try { if(rs!=null) { rs.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } package com.neusoft.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DbUtils { private DbUtils(){} private static Connection conn; public static Connection getConnection() { try { DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); //2.连接数据库,获取数据库连接对象 String url="jdbc:oracle:thin:@zhangyahui:1521:oracle"; String user="scott"; String password="0000"; conn=DriverManager.getConnection(url,user,password); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } public static void closeConnection() { try { if(conn!=null) { conn.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void closeStatement(Statement stmt) { try { if(stmt!=null) { stmt.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void closeResultSet(ResultSet rs) { //4.关闭数据库 try { if(rs!=null) { rs.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } package com.neusoft.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; //实现类 public class DeptDaoImpl implements DeptDaoInterface { @Override public void deleteDept(int deptno) { // TODO Auto-generated method stub PreparedStatement pstmt=null; Dept dept=new Dept(); try{ Connection conn=DbUtils.getConnection(); String sql="delete from forjava where ID=?"; pstmt=conn.prepareStatement(sql); pstmt.setInt(1, deptno); //pstmt=conn.prepareStatement(sql); pstmt.executeUpdate(); }catch(SQLException ex) { ex.printStackTrace(); }finally { DbUtils.closeStatement(pstmt); DbUtils.closeConnection(); } } @Override public List<Dept> getAllDepts() { PreparedStatement pstmt=null; ResultSet rs=null; List<Dept> depts=new ArrayList<Dept>(); try{ Connection conn=DbUtils.getConnection(); String sql="select * from forjava"; pstmt=conn.prepareStatement(sql); rs= pstmt.executeQuery(); while(rs.next()) { Dept dept=new Dept(); dept.setID(rs.getInt("ID")); dept.setName(rs.getString("NAME")); System.out.println(rs.getInt("ID")+" "+rs.getString("NAME")); depts.add(dept); } }catch(SQLException ex) { ex.printStackTrace(); }finally { DbUtils.closeResultSet(rs); DbUtils.closeStatement(pstmt); DbUtils.closeConnection(); } return depts; } @Override public Dept getDeptByDeptNo(int deptno) { // TODO Auto-generated method stub return null; } @Override public void insertDept(Dept dept) { // TODO Auto-generated method stub PreparedStatement pstmt=null; try{ Connection conn=DbUtils.getConnection(); String sql="insert into forjava(ID,name) values(?,?)"; pstmt=conn.prepareStatement(sql); pstmt.setInt(1, dept.getID()); pstmt.setString(2, dept.getName()); pstmt.executeUpdate(); }catch(SQLException ex) { ex.printStackTrace(); }finally { DbUtils.closeStatement(pstmt); DbUtils.closeConnection(); } } @Override public void updateDept(Dept dept) { // TODO Auto-generated method stub } } import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; //实现类 public class DeptDaoImpl implements DeptDaoInterface { @Override public void deleteDept(int deptno) { // TODO Auto-generated method stub PreparedStatement pstmt=null; Dept dept=new Dept(); try{ Connection conn=DbUtils.getConnection(); String sql="delete from forjava where ID=?"; pstmt=conn.prepareStatement(sql); pstmt.setInt(1, deptno); //pstmt=conn.prepareStatement(sql); pstmt.executeUpdate(); }catch(SQLException ex) { ex.printStackTrace(); }finally { DbUtils.closeStatement(pstmt); DbUtils.closeConnection(); } } @Override public List<Dept> getAllDepts() { PreparedStatement pstmt=null; ResultSet rs=null; List<Dept> depts=new ArrayList<Dept>(); try{ Connection conn=DbUtils.getConnection(); String sql="select * from forjava"; pstmt=conn.prepareStatement(sql); rs= pstmt.executeQuery(); while(rs.next()) { Dept dept=new Dept(); dept.setID(rs.getInt("ID")); dept.setName(rs.getString("NAME")); System.out.println(rs.getInt("ID")+" "+rs.getString("NAME")); depts.add(dept); } }catch(SQLException ex) { ex.printStackTrace(); }finally { DbUtils.closeResultSet(rs); DbUtils.closeStatement(pstmt); DbUtils.closeConnection(); } return depts; } @Override public Dept getDeptByDeptNo(int deptno) { // TODO Auto-generated method stub return null; } @Override public void insertDept(Dept dept) { // TODO Auto-generated method stub PreparedStatement pstmt=null; try{ Connection conn=DbUtils.getConnection(); String sql="insert into forjava(ID,name) values(?,?)"; pstmt=conn.prepareStatement(sql); pstmt.setInt(1, dept.getID()); pstmt.setString(2, dept.getName()); pstmt.executeUpdate(); }catch(SQLException ex) { ex.printStackTrace(); }finally { DbUtils.closeStatement(pstmt); DbUtils.closeConnection(); } } @Override public void updateDept(Dept dept) { // TODO Auto-generated method stub } } package com.neusoft.dao; import java.util.List; //接口 public interface DeptDaoInterface { List<Dept> getAllDepts(); void insertDept(Dept dept); void updateDept(Dept dept); void deleteDept(int deptno); Dept getDeptByDeptNo(int deptno); }
package com.neusoft.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DbUtils { private DbUtils(){} private static Connection conn; public static Connection getConnection() { try { DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); //2.连接数据库,获取数据库连接对象 String url="jdbc:oracle:thin:@zhangyahui:1521:oracle"; String user="scott"; String password="0000"; conn=DriverManager.getConnection(url,user,password); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } public static void closeConnection() { try { if(conn!=null) { conn.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void closeStatement(Statement stmt) { try { if(stmt!=null) { stmt.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void closeResultSet(ResultSet rs) { //4.关闭数据库 try { if(rs!=null) { rs.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } package com.neusoft.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DbUtils { private DbUtils(){} private static Connection conn; public static Connection getConnection() { try { DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); //2.连接数据库,获取数据库连接对象 String url="jdbc:oracle:thin:@zhangyahui:1521:oracle"; String user="scott"; String password="0000"; conn=DriverManager.getConnection(url,user,password); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } public static void closeConnection() { try { if(conn!=null) { conn.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void closeStatement(Statement stmt) { try { if(stmt!=null) { stmt.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void closeResultSet(ResultSet rs) { //4.关闭数据库 try { if(rs!=null) { rs.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } package com.neusoft.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; //实现类 public class DeptDaoImpl implements DeptDaoInterface { @Override public void deleteDept(int deptno) { // TODO Auto-generated method stub PreparedStatement pstmt=null; Dept dept=new Dept(); try{ Connection conn=DbUtils.getConnection(); String sql="delete from forjava where ID=?"; pstmt=conn.prepareStatement(sql); pstmt.setInt(1, deptno); //pstmt=conn.prepareStatement(sql); pstmt.executeUpdate(); }catch(SQLException ex) { ex.printStackTrace(); }finally { DbUtils.closeStatement(pstmt); DbUtils.closeConnection(); } } @Override public List<Dept> getAllDepts() { PreparedStatement pstmt=null; ResultSet rs=null; List<Dept> depts=new ArrayList<Dept>(); try{ Connection conn=DbUtils.getConnection(); String sql="select * from forjava"; pstmt=conn.prepareStatement(sql); rs= pstmt.executeQuery(); while(rs.next()) { Dept dept=new Dept(); dept.setID(rs.getInt("ID")); dept.setName(rs.getString("NAME")); System.out.println(rs.getInt("ID")+" "+rs.getString("NAME")); depts.add(dept); } }catch(SQLException ex) { ex.printStackTrace(); }finally { DbUtils.closeResultSet(rs); DbUtils.closeStatement(pstmt); DbUtils.closeConnection(); } return depts; } @Override public Dept getDeptByDeptNo(int deptno) { // TODO Auto-generated method stub return null; } @Override public void insertDept(Dept dept) { // TODO Auto-generated method stub PreparedStatement pstmt=null; try{ Connection conn=DbUtils.getConnection(); String sql="insert into forjava(ID,name) values(?,?)"; pstmt=conn.prepareStatement(sql); pstmt.setInt(1, dept.getID()); pstmt.setString(2, dept.getName()); pstmt.executeUpdate(); }catch(SQLException ex) { ex.printStackTrace(); }finally { DbUtils.closeStatement(pstmt); DbUtils.closeConnection(); } } @Override public void updateDept(Dept dept) { // TODO Auto-generated method stub } } package com.neusoft.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DbUtils { private DbUtils(){} private static Connection conn; public static Connection getConnection() { try { DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); //2.连接数据库,获取数据库连接对象 String url="jdbc:oracle:thin:@zhangyahui:1521:oracle"; String user="scott"; String password="0000"; conn=DriverManager.getConnection(url,user,password); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } public static void closeConnection() { try { if(conn!=null) { conn.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void closeStatement(Statement stmt) { try { if(stmt!=null) { stmt.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void closeResultSet(ResultSet rs) { //4.关闭数据库 try { if(rs!=null) { rs.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } package com.neusoft.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DbUtils { private DbUtils(){} private static Connection conn; public static Connection getConnection() { try { DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); //2.连接数据库,获取数据库连接对象 String url="jdbc:oracle:thin:@zhangyahui:1521:oracle"; String user="scott"; String password="0000"; conn=DriverManager.getConnection(url,user,password); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } public static void closeConnection() { try { if(conn!=null) { conn.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void closeStatement(Statement stmt) { try { if(stmt!=null) { stmt.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void closeResultSet(ResultSet rs) { //4.关闭数据库 try { if(rs!=null) { rs.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } package com.neusoft.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; //实现类 public class DeptDaoImpl implements DeptDaoInterface { @Override public void deleteDept(int deptno) { // TODO Auto-generated method stub PreparedStatement pstmt=null; Dept dept=new Dept(); try{ Connection conn=DbUtils.getConnection(); String sql="delete from forjava where ID=?"; pstmt=conn.prepareStatement(sql); pstmt.setInt(1, deptno); //pstmt=conn.prepareStatement(sql); pstmt.executeUpdate(); }catch(SQLException ex) { ex.printStackTrace(); }finally { DbUtils.closeStatement(pstmt); DbUtils.closeConnection(); } } @Override public List<Dept> getAllDepts() { PreparedStatement pstmt=null; ResultSet rs=null; List<Dept> depts=new ArrayList<Dept>(); try{ Connection conn=DbUtils.getConnection(); String sql="select * from forjava"; pstmt=conn.prepareStatement(sql); rs= pstmt.executeQuery(); while(rs.next()) { Dept dept=new Dept(); dept.setID(rs.getInt("ID")); dept.setName(rs.getString("NAME")); System.out.println(rs.getInt("ID")+" "+rs.getString("NAME")); depts.add(dept); } }catch(SQLException ex) { ex.printStackTrace(); }finally { DbUtils.closeResultSet(rs); DbUtils.closeStatement(pstmt); DbUtils.closeConnection(); } return depts; } @Override public Dept getDeptByDeptNo(int deptno) { // TODO Auto-generated method stub return null; } @Override public void insertDept(Dept dept) { // TODO Auto-generated method stub PreparedStatement pstmt=null; try{ Connection conn=DbUtils.getConnection(); String sql="insert into forjava(ID,name) values(?,?)"; pstmt=conn.prepareStatement(sql); pstmt.setInt(1, dept.getID()); pstmt.setString(2, dept.getName()); pstmt.executeUpdate(); }catch(SQLException ex) { ex.printStackTrace(); }finally { DbUtils.closeStatement(pstmt); DbUtils.closeConnection(); } } @Override public void updateDept(Dept dept) { // TODO Auto-generated method stub } } import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; //实现类 public class DeptDaoImpl implements DeptDaoInterface { @Override public void deleteDept(int deptno) { // TODO Auto-generated method stub PreparedStatement pstmt=null; Dept dept=new Dept(); try{ Connection conn=DbUtils.getConnection(); String sql="delete from forjava where ID=?"; pstmt=conn.prepareStatement(sql); pstmt.setInt(1, deptno); //pstmt=conn.prepareStatement(sql); pstmt.executeUpdate(); }catch(SQLException ex) { ex.printStackTrace(); }finally { DbUtils.closeStatement(pstmt); DbUtils.closeConnection(); } } @Override public List<Dept> getAllDepts() { PreparedStatement pstmt=null; ResultSet rs=null; List<Dept> depts=new ArrayList<Dept>(); try{ Connection conn=DbUtils.getConnection(); String sql="select * from forjava"; pstmt=conn.prepareStatement(sql); rs= pstmt.executeQuery(); while(rs.next()) { Dept dept=new Dept(); dept.setID(rs.getInt("ID")); dept.setName(rs.getString("NAME")); System.out.println(rs.getInt("ID")+" "+rs.getString("NAME")); depts.add(dept); } }catch(SQLException ex) { ex.printStackTrace(); }finally { DbUtils.closeResultSet(rs); DbUtils.closeStatement(pstmt); DbUtils.closeConnection(); } return depts; } @Override public Dept getDeptByDeptNo(int deptno) { // TODO Auto-generated method stub return null; } @Override public void insertDept(Dept dept) { // TODO Auto-generated method stub PreparedStatement pstmt=null; try{ Connection conn=DbUtils.getConnection(); String sql="insert into forjava(ID,name) values(?,?)"; pstmt=conn.prepareStatement(sql); pstmt.setInt(1, dept.getID()); pstmt.setString(2, dept.getName()); pstmt.executeUpdate(); }catch(SQLException ex) { ex.printStackTrace(); }finally { DbUtils.closeStatement(pstmt); DbUtils.closeConnection(); } } @Override public void updateDept(Dept dept) { // TODO Auto-generated method stub } } package com.neusoft.dao; import java.util.List; //接口 public interface DeptDaoInterface { List<Dept> getAllDepts(); void insertDept(Dept dept); void updateDept(Dept dept); void deleteDept(int deptno); Dept getDeptByDeptNo(int deptno); } import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; //实现类 public class DeptDaoImpl implements DeptDaoInterface { @Override public void deleteDept(int deptno) { // TODO Auto-generated method stub PreparedStatement pstmt=null; Dept dept=new Dept(); try{ Connection conn=DbUtils.getConnection(); String sql="delete from forjava where ID=?"; pstmt=conn.prepareStatement(sql); pstmt.setInt(1, deptno); //pstmt=conn.prepareStatement(sql); pstmt.executeUpdate(); }catch(SQLException ex) { ex.printStackTrace(); }finally { DbUtils.closeStatement(pstmt); DbUtils.closeConnection(); } } @Override public List<Dept> getAllDepts() { PreparedStatement pstmt=null; ResultSet rs=null; List<Dept> depts=new ArrayList<Dept>(); try{ Connection conn=DbUtils.getConnection(); String sql="select * from forjava"; pstmt=conn.prepareStatement(sql); rs= pstmt.executeQuery(); while(rs.next()) { Dept dept=new Dept(); dept.setID(rs.getInt("ID")); dept.setName(rs.getString("NAME")); System.out.println(rs.getInt("ID")+" "+rs.getString("NAME")); depts.add(dept); } }catch(SQLException ex) { ex.printStackTrace(); }finally { DbUtils.closeResultSet(rs); DbUtils.closeStatement(pstmt); DbUtils.closeConnection(); } return depts; } @Override public Dept getDeptByDeptNo(int deptno) { // TODO Auto-generated method stub return null; } @Override public void insertDept(Dept dept) { // TODO Auto-generated method stub PreparedStatement pstmt=null; try{ Connection conn=DbUtils.getConnection(); String sql="insert into forjava(ID,name) values(?,?)"; pstmt=conn.prepareStatement(sql); pstmt.setInt(1, dept.getID()); pstmt.setString(2, dept.getName()); pstmt.executeUpdate(); }catch(SQLException ex) { ex.printStackTrace(); }finally { DbUtils.closeStatement(pstmt); DbUtils.closeConnection(); } } @Override public void updateDept(Dept dept) { // TODO Auto-generated method stub } } import java.util.List; //接口 public interface DeptDaoInterface { List<Dept> getAllDepts(); void insertDept(Dept dept); void updateDept(Dept dept); void deleteDept(int deptno); Dept getDeptByDeptNo(int deptno); } package com.neusoft.dao; //package com.neusoft.dao; //实体类 public class Dept { private int ID; private String name; public Dept(int de,String dna){ this.ID=de; this.name=dna; } public Dept() { } public int getID() { return ID; } public void setID(int iD) { ID = iD; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; //实现类 public class DeptDaoImpl implements DeptDaoInterface {
@Override public void deleteDept(int deptno) { // TODO Auto-generated method stub PreparedStatement pstmt=null; Dept dept=new Dept(); try{ Connection conn=DbUtils.getConnection(); String sql="delete from forjava where ID=?"; pstmt=conn.prepareStatement(sql); pstmt.setInt(1, deptno); //pstmt=conn.prepareStatement(sql); pstmt.executeUpdate(); }catch(SQLException ex) { ex.printStackTrace(); }finally { DbUtils.closeStatement(pstmt); DbUtils.closeConnection(); }
}
@Override public List<Dept> getAllDepts() { PreparedStatement pstmt=null; ResultSet rs=null; List<Dept> depts=new ArrayList<Dept>(); try{ Connection conn=DbUtils.getConnection(); String sql="select * from forjava"; pstmt=conn.prepareStatement(sql); rs= pstmt.executeQuery(); while(rs.next()) { Dept dept=new Dept(); dept.setID(rs.getInt("ID")); dept.setName(rs.getString("NAME")); System.out.println(rs.getInt("ID")+" "+rs.getString("NAME")); depts.add(dept); } }catch(SQLException ex) { ex.printStackTrace(); }finally { DbUtils.closeResultSet(rs); DbUtils.closeStatement(pstmt); DbUtils.closeConnection(); } return depts; }
@Override public Dept getDeptByDeptNo(int deptno) { // TODO Auto-generated method stub return null; }
@Override public void insertDept(Dept dept) { // TODO Auto-generated method stub PreparedStatement pstmt=null; try{ Connection conn=DbUtils.getConnection(); String sql="insert into forjava(ID,name) values(?,?)"; pstmt=conn.prepareStatement(sql); pstmt.setInt(1, dept.getID()); pstmt.setString(2, dept.getName()); pstmt.executeUpdate(); }catch(SQLException ex) { ex.printStackTrace(); }finally { DbUtils.closeStatement(pstmt); DbUtils.closeConnection(); } }
@Override public void updateDept(Dept dept) { // TODO Auto-generated method stub
}
}
import java.util.List; //接口 public interface DeptDaoInterface { List<Dept> getAllDepts(); void insertDept(Dept dept); void updateDept(Dept dept); void deleteDept(int deptno); Dept getDeptByDeptNo(int deptno); }
//package com.neusoft.dao; //实体类 public class Dept { private int ID; private String name; public Dept(int de,String dna){ this.ID=de; this.name=dna;
} public Dept() { } public int getID() { return ID; } public void setID(int iD) { ID = iD; } public String getName() { return name; } public void setName(String name) { this.name = name; }
}