增加
1 public String addParent(Parent pp) { 2 Connection conn=new ConntionUtil().openConnection(); 3 PreparedStatement pstmt=null; 4 try { 5 if(pp.getUsername()!=null){ 6 String sql="insert into parents(username,nickname,password,registertime,sex,birth,area,headimg,weixin_openid) values(?,?,?,?,?,?,?,?,?)"; 7 pstmt=conn.prepareStatement(sql); 8 pstmt.setString(1,pp.getUsername()); 9 pstmt.setString(2,EmojiUtil.Emoji(pp.getNickname())); 10 pstmt.setString(3,pp.getPassword()); 11 pstmt.setString(4,pp.getRegistertime()); 12 pstmt.setInt(5,pp.getSex()); 13 pstmt.setDate(6,null); 14 pstmt.setString(7,pp.getArea()); 15 pstmt.setString(8,pp.getHeadimg()); 16 pstmt.setString(9,pp.getWeixin_openid()); 17 int i=pstmt.executeUpdate(); 18 if(i>0){ 19 return "0"; 20 }else{ 21 return "1"; 22 } 23 }else{ 24 return "0"; 25 } 26 }catch(SQLException e){ 27 try{ 28 conn.rollback(); 29 }catch(SQLException e1){ 30 e1.printStackTrace(); 31 } 32 e.printStackTrace(); 33 return "-1"; 34 }finally{ 35 try { 36 if(pstmt!=null) pstmt.close(); 37 if(conn!=null) conn.close(); 38 } catch (SQLException e) { 39 e.printStackTrace(); 40 } 41 } 42 43 }
修改
1 public void addData(String strdata,int intdata){ 2 Connection conn=new ConntionUtil().openConnection(); 3 PreparedStatement pstmt=null; 4 try{ 5 String sql="update tablename set strdata=?,intdata=? where id=? "; 6 pstmt=conn.prepareStatement(sql); 7 pstmt.setString(1,strdata); 8 pstmt.setInt(2,intdata); 9 pstmt.setString(3,id); 10 pstmt.executeUpdate(); 11 }catch(SQLException e){ 12 e.printStackTrace(); 13 }finally{ 14 try { 15 if(pstmt!=null)pstmt.close(); 16 if(conn!=null)conn.close(); 17 } catch (SQLException e) { 18 e.printStackTrace(); 19 } 20 } 21 }
查找
1 public boolean haveParent(int userId) { 2 Connection conn=new ConntionUtil().openConnection(); 3 PreparedStatement pstmt=null; 4 ResultSet rs=null; 5 try { 6 String sql="select parentId,parentName from user where user_id=?"; 7 pstmt=conn.prepareStatement(sql); 8 pstmt.setInt(1,userId); 9 rs=pstmt.executeQuery(); 10 while(rs.next()){ 11 if(rs.getString("parentName")==null || rs.getString("parentName")=="" || "".equals(rs.getString("parentName"))){ 12 return false; 13 }else{ 14 return true; 15 } 16 } 17 } catch (SQLException e) { 18 e.printStackTrace(); 19 return false; 20 }finally{ 21 try { 22 if(rs!=null)rs.close(); 23 if(pstmt!=null)pstmt.close(); 24 if(conn!=null)conn.close(); 25 } catch (SQLException e) { 26 e.printStackTrace(); 27 } 28 } 29 return false; 30 }
原文:http://www.cnblogs.com/manyu/p/6255740.html