[Java] 宿舍 →→→→→进入此内容的聊天室

来自 , 2019-08-17, 写在 Java, 查看 105 次.
URL http://www.code666.cn/view/076023ed
  1. package com.dao;
  2.  
  3. import com.db.DBHelper;
  4. import com.bean.BuildingBean;
  5.  
  6. import java.util.*;
  7. import java.sql.*;
  8.  
  9. public class BuildingDao {
  10.        
  11.         //获取列表
  12.         public List<BuildingBean> GetList(String strwhere,String strorder){
  13.                 String sql="select * from Building";
  14.                 if(!(isInvalid(strwhere)))
  15.                 {
  16.                         sql+=" where "+strwhere;
  17.                 }
  18.                 if(!(isInvalid(strorder)))
  19.                 {
  20.                         sql+=" order by "+strorder;
  21.                 }
  22.                 Statement stat = null;
  23.                 ResultSet rs = null;
  24.                 Connection conn = new DBHelper().getConn();
  25.                 List<BuildingBean> list=new ArrayList<BuildingBean>();
  26.                 try{
  27.                         stat = conn.createStatement();
  28.                         rs = stat.executeQuery(sql);
  29.                         while(rs.next()){
  30.                                 BuildingBean cnbean=new BuildingBean();
  31.                                 cnbean.setBuilding_ID(rs.getInt("Building_ID"));
  32.                                 cnbean.setBuilding_Name(rs.getString("Building_Name"));
  33.                                 cnbean.setBuilding_Introduction(rs.getString("Building_Introduction"));
  34.                                 list.add(cnbean);
  35.                         }
  36.                 } catch (SQLException e) {
  37.                         e.printStackTrace();
  38.                 } finally {
  39.                         try {
  40.                                 if (conn != null)
  41.                                         conn.close();
  42.                                 if (stat != null)
  43.                                         stat.close();
  44.                                 if (rs != null)
  45.                                         rs.close();
  46.                         } catch (SQLException e) {
  47.                                 e.printStackTrace();
  48.                         }
  49.                 }
  50.                 return list;
  51.         }
  52.        
  53.         //获取指定ID的实体Bean
  54.         public BuildingBean GetBean(int id){
  55.                 String sql="select * from Building where Building_ID="+id;
  56.                 Statement stat = null;
  57.                 ResultSet rs = null;
  58.                 Connection conn = new DBHelper().getConn();
  59.                 BuildingBean cnbean=new BuildingBean();
  60.                 try{
  61.                         stat = conn.createStatement();
  62.                         rs = stat.executeQuery(sql);
  63.                         while(rs.next()){
  64.                                 cnbean.setBuilding_ID(rs.getInt("Building_ID"));
  65.                                 cnbean.setBuilding_Name(rs.getString("Building_Name"));
  66.                                 cnbean.setBuilding_Introduction(rs.getString("Building_Introduction"));
  67.                                
  68.                         }
  69.                 } catch (SQLException e) {
  70.                         e.printStackTrace();
  71.                 } finally {
  72.                         try {
  73.                                 if (conn != null)
  74.                                         conn.close();
  75.                                 if (stat != null)
  76.                                         stat.close();
  77.                                 if (rs != null)
  78.                                         rs.close();
  79.                         } catch (SQLException e) {
  80.                                 e.printStackTrace();
  81.                         }
  82.                 }
  83.                 return cnbean;
  84.         }
  85.        
  86.         //添加
  87.         public void Add(BuildingBean cnbean){
  88.                 String sql="insert into Building (";
  89.                 sql+="Building_Name,Building_Introduction";
  90.                 sql+=") values(";
  91.                 sql+="'"+cnbean.getBuilding_Name()+"','"+cnbean.getBuilding_Introduction()+"'";
  92.                 sql+=")";
  93.                 Statement stat = null;
  94.                 ResultSet rs = null;
  95.                 Connection conn = new DBHelper().getConn();
  96.                 try{
  97.                         stat = conn.createStatement();
  98.                         stat.executeUpdate(sql);
  99.                 } catch (SQLException e) {
  100.                         e.printStackTrace();
  101.                 } finally {
  102.                         try {
  103.                                 if (conn != null)
  104.                                         conn.close();
  105.                                 if (stat != null)
  106.                                         stat.close();
  107.                                 if (rs != null)
  108.                                         rs.close();
  109.                         } catch (SQLException e) {
  110.                                 e.printStackTrace();
  111.                         }
  112.                 }
  113.         }
  114.         //修改
  115.         public void Update(BuildingBean cnbean){
  116.                 String sql="update Building set ";
  117.                 sql+="Building_Name='"+cnbean.getBuilding_Name()+"',";
  118.                 sql+="Building_Introduction='"+cnbean.getBuilding_Introduction()+"'";
  119.                
  120.                 sql+=" where Building_ID='"+cnbean.getBuilding_ID()+"'";
  121.                 Statement stat = null;
  122.                 ResultSet rs = null;
  123.                 Connection conn = new DBHelper().getConn();
  124.                 try{
  125.                         stat = conn.createStatement();
  126.                         stat.executeUpdate(sql);
  127.                 } catch (SQLException e) {
  128.                         e.printStackTrace();
  129.                 } finally {
  130.                         try {
  131.                                 if (conn != null)
  132.                                         conn.close();
  133.                                 if (stat != null)
  134.                                         stat.close();
  135.                                 if (rs != null)
  136.                                         rs.close();
  137.                         } catch (SQLException e) {
  138.                                 e.printStackTrace();
  139.                         }
  140.                 }
  141.         }
  142.         //删除
  143.         public void Delete(String strwhere){
  144.                 String sql="delete Building where ";
  145.                 sql+=strwhere;
  146.                 Statement stat = null;
  147.                 ResultSet rs = null;
  148.                 Connection conn = new DBHelper().getConn();
  149.                 try{
  150.                         stat = conn.createStatement();
  151.                         stat.executeUpdate(sql);
  152.                 } catch (SQLException e) {
  153.                         e.printStackTrace();
  154.                 } finally {
  155.                         try {
  156.                                 if (conn != null)
  157.                                         conn.close();
  158.                                 if (stat != null)
  159.                                         stat.close();
  160.                                 if (rs != null)
  161.                                         rs.close();
  162.                         } catch (SQLException e) {
  163.                                 e.printStackTrace();
  164.                         }
  165.                 }
  166.         }
  167.  
  168.        
  169.         //判断是否空值
  170.         private boolean isInvalid(String value) {
  171.                 return (value == null || value.length() == 0);
  172.         }
  173.        
  174.         //测试
  175.         public static void main(String[] args) {
  176.                 System.out.println("");
  177.         }
  178.        
  179. }
  180.  
  181.  

回复 "宿舍"

这儿你可以回复上面这条便签

captcha