Java-web 学习感触一方法接口实现
在通过对Java-web的开发方式有了一定的了解后,并不一如当初那样感到迷茫,在最开始的时候,没能跟上老师的步伐,故对Java-web的开发存在很大的迷茫和一定的误解。
现将自己对于Java-web的开发学习的理解和感触记录如下,仅仅是一种作为入门新手最直观的感触,并不适合作为出学参考,如有不足,请指正。
在编写项目时同样的可以将一些后端通用的数据库操作先声明为接口,再单独逐个接口的实现,这样的操作可以使得再后续的servlet中直接调用这些接口的方法实现即可,而不必在servlet中在编写类似的程序,如此一来,可以有效的实现代码的复用;
示例如下:
声明:
1 package dao; 2 3 import java.util.List; 4 import entity.People; 5 6 7 public interface PeopleDao { 8 9 public boolean add(People people); 10 public List<People>select(); 11 public boolean delete(String HZXM); 12 public List<People>selectall(String HZXM,String XB,String MZ,String SJYCD); 13 boolean update(String HZXM,String SFZHM, String XB, String MZ, String SJYCD); 14 15 }
实现:
1 package dao; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8 import java.util.ArrayList; 9 import java.util.List; 10 11 import entity.People; 12 13 public class PeopleDaoImpI implements PeopleDao{ 14 Connection conn=(Connection)DBUtil.getConn(); 15 16 @Override 17 public boolean add(People people) { 18 19 // TODO Auto-generated method stub 20 boolean flag=false; 21 try { 22 String sql="insert into huseventhpeoplecheck values('"+people.getTIME()+"','"+people.getHB()+"','"+people.getZFLX()+"','"+people.getXZFMJ()+"','"+people.getXZFJS()+"','"+people.getHZXM()+"','"+people.getSFZHM()+"','"+people.getXB()+"','"+people.getMZ()+"','"+people.getSJYCD()+"')"; 23 PreparedStatement pstmt = conn.prepareStatement(sql); 24 int i=pstmt.executeUpdate(); 25 pstmt.close(); 26 conn.close(); 27 if(i>0) { 28 flag=true; 29 } 30 }catch(SQLException e) { 31 System.out.println("人口信息登记失败!!!"); 32 e.printStackTrace(); 33 } 34 return flag; 35 } 36 37 @Override 38 public List<People> select() { 39 // TODO Auto-generated method stub 40 List<People> list = new ArrayList<People>(); 41 try { 42 String sql="select * from huseventhpeoplecheck ORDER BY time DESC"; 43 PreparedStatement pstmt = conn.prepareStatement(sql); 44 ResultSet rs = pstmt.executeQuery(); 45 while(rs.next()) { 46 People people = new People(); 47 people.setHB(rs.getString("HB")); 48 people.setZFLX(rs.getString("ZFLX")); 49 people.setXZFMJ(rs.getString("XZFMJ")); 50 people.setXZFJS(rs.getString("XZFJS")); 51 people.setHZXM(rs.getString("HZXM")); 52 people.setXB(rs.getString("XB")); 53 people.setMZ(rs.getString("MZ")); 54 people.setSJYCD(rs.getString("SJYCD")); 55 people.setTIME(rs.getString("TIME")); 56 list.add(people); 57 } 58 rs.close(); 59 pstmt.close(); 60 conn.close(); 61 62 }catch(SQLException e){ 63 e.printStackTrace(); 64 } 65 return list; 66 } 67 68 69 70 @Override 71 public boolean delete(String HZXM) { 72 // TODO Auto-generated method stub 73 boolean flag=false; 74 try { 75 String sql="delete from huseventhpeoplecheck where HZXM='"+HZXM+"'"; 76 PreparedStatement pstmt = conn.prepareStatement(sql); 77 int i=pstmt.executeUpdate(); 78 pstmt.close(); 79 conn.close(); 80 if(i>0) 81 flag=true; 82 }catch(SQLException e){ 83 System.out.println("人口信息删除失败!!!"); 84 e.printStackTrace(); 85 } 86 return flag; 87 } 88 89 90 @Override 91 public List<People> selectall(String HZXM, String XB, String MZ, String SJYCD) { 92 List<People> list = new ArrayList<People>(); 93 try { 94 String sql="select * from huseventhpeoplecheck where HZXM=? or XB=? or MZ=? or SJYCD=?"; 95 PreparedStatement pstmt = conn.prepareStatement(sql); 96 pstmt.setString(1, HZXM); 97 pstmt.setString(2, XB); 98 pstmt.setString(3, MZ); 99 pstmt.setString(4, SJYCD); 100 ResultSet rs = pstmt.executeQuery(); 101 while(rs.next()) { 102 People people = new People(); 103 people.setHB(rs.getString("HB")); 104 people.setZFLX(rs.getString("ZFLX")); 105 people.setXZFMJ(rs.getString("XZFMJ")); 106 people.setXZFJS(rs.getString("XZFJS")); 107 people.setHZXM(rs.getString("HZXM")); 108 people.setSFZHM(rs.getString("SFZHM")); 109 people.setXB(rs.getString("XB")); 110 people.setMZ(rs.getString("MZ")); 111 people.setSJYCD(rs.getString("SJYCD")); 112 people.setTIME(rs.getString("TIME")); 113 list.add(people); 114 } 115 rs.close(); 116 pstmt.close(); 117 conn.close(); 118 119 }catch(SQLException e){ 120 e.printStackTrace(); 121 } 122 return list; 123 } 124 125 @Override 126 public boolean update(String HZXM, String SFZHM, String XB, String MZ, String SJYCD) { 127 // TODO Auto-generated method stub 128 boolean flag=false; 129 try { 130 String sql="update huseventhpeoplecheck set HZXM='"+HZXM+"',SFZHM='"+SFZHM+"',XB='"+XB+"',MZ='"+MZ+"',SJYCD='"+SJYCD+"' where HZXM='"+HZXM+"'"; 131 PreparedStatement pstmt = conn.prepareStatement(sql); 132 int i = pstmt.executeUpdate(); 133 pstmt.close(); 134 conn.close(); 135 if(i>0)flag = true; 136 }catch(SQLException e) { 137 e.printStackTrace(); 138 } 139 return flag; 140 } 141 }
调用:
1 PeopleDao pd = new PeopleDaoImpI(); 2 try { 3 pd.add(p); 4 response.sendRedirect("renkoudengji.jsp"); 5 System.out.println("人口添加成功!!!"); 6 }catch(Exception e){ 7 System.out.println("人口信息添加失败!!!"); 8 e.printStackTrace(); 9 } 10