struts經常調用的兩個類

來源:互聯網
上載者:User

  學習開發環境:Myeclipse 7.0   Mysql 6.0   tomcat 6.0
  資料庫名:school  表名:學籍    列名:學號、姓名、性別、出生年月、家庭地址。
 老是用到的兩個類:dao.Student類(負責與資料庫互動)和po.Student(javabean)

Code:
  1. package dao;   
  2.   
  3. import java.sql.Connection;   
  4. import java.sql.ResultSet;   
  5. import java.util.ArrayList;   
  6.   
  7. import po.Student;   
  8.   
  9. //訪問資料庫   
  10. public class StudentDao {   
  11.     private Connection conn = null;   
  12.        
  13.     public void initConnection(){   //初始化資料庫連接   
  14.         try{   
  15.             Class.forName("com.mysql.jdbc.Driver").newInstance();          
  16.             String DbConn = "jdbc:mysql://localhost:3306/school";   
  17.             String DbPass = "123";   
  18.             conn = java.sql.DriverManager.getConnection(DbConn, "root", DbPass);   
  19.         }catch(Exception ex){ex.printStackTrace();}   
  20.     }   
  21.        
  22.     public ArrayList queryStus(){    //查詢所有的學生   
  23.         ArrayList stus = new ArrayList();   
  24.         String sql = "SELECT 學號,姓名,性別,出生年月,家庭地址 FROM 學籍 ";   
  25.         try{   
  26.             this.initConnection();//意味著每次串連都是重新串連資料庫,同時在該函數中也會關閉所有的串連。   
  27.             ResultSet rs = conn.createStatement().executeQuery(sql);   
  28.             while(rs.next()){   
  29.                 Student stu = new Student();   
  30.                 stu.setStuId(rs.getString("學號"));   
  31.                 stu.setStuName(rs.getString("姓名"));   
  32.                 stu.setStuSex(rs.getString("性別"));   
  33.                 stu.setStuBir(rs.getString("出生年月"));   
  34.                 stu.setStuAdd(rs.getString("家庭地址"));   
  35.                 stus.add(stu);             
  36.             }   
  37.         }catch(Exception ex){ex.printStackTrace();}   
  38.         finally{   
  39.             this.closeConnection();   //查詢的最後總會關掉串連,以免佔用資源   
  40.         }   
  41.         return stus;   
  42.     }   
  43.        
  44.     public ArrayList queryBySex(String sex){    //查詢所有的學生   
  45.         ArrayList stus = new ArrayList();   
  46.         String sql = "SELECT 學號,姓名,性別,出生年月,家庭地址 FROM 學籍 where 性別='"+sex+"'";   
  47.            
  48.         try{   
  49.             this.initConnection();//意味著每次串連都是重新串連資料庫,同時在該函數中也會關閉所有的串連。   
  50.             ResultSet rs = conn.createStatement().executeQuery(sql);   
  51.             while(rs.next()){   
  52.                 Student stu = new Student();   
  53.                 stu.setStuId(rs.getString("學號"));   
  54.                 stu.setStuName(rs.getString("姓名"));   
  55.                 stu.setStuSex(rs.getString("性別"));   
  56.                 stu.setStuBir(rs.getString("出生年月"));   
  57.                 stu.setStuAdd(rs.getString("家庭地址"));   
  58.                 stus.add(stu);             
  59.             }   
  60.         }catch(Exception ex){ex.printStackTrace();}   
  61.         finally{   
  62.             this.closeConnection();   //查詢的最後總會關掉串連,以免佔用資源   
  63.         }   
  64.         return stus;   
  65.     }   
  66.        
  67.     public ArrayList queryBySA(String sex,String add){    //查詢所有的學生   
  68.         ArrayList stus = new ArrayList();   
  69.         String sql = "SELECT 學號,姓名,性別,出生年月,家庭地址 FROM 學籍 where 性別='"+sex+"' and 家庭地址='"+add+"'";   
  70.            
  71.         try{   
  72.             this.initConnection();//意味著每次串連都是重新串連資料庫,同時在該函數中也會關閉所有的串連。   
  73.             ResultSet rs = conn.createStatement().executeQuery(sql);   
  74.             while(rs.next()){   
  75.                 Student stu = new Student();   
  76.                 stu.setStuId(rs.getString("學號"));   
  77.                 stu.setStuName(rs.getString("姓名"));   
  78.                 stu.setStuSex(rs.getString("性別"));   
  79.                 stu.setStuBir(rs.getString("出生年月"));   
  80.                 stu.setStuAdd(rs.getString("家庭地址"));   
  81.                 stus.add(stu);             
  82.             }   
  83.         }catch(Exception ex){ex.printStackTrace();}   
  84.         finally{   
  85.             this.closeConnection();   //查詢的最後總會關掉串連,以免佔用資源   
  86.         }   
  87.         return stus;   
  88.     }   
  89.        
  90.        
  91.     public void closeConnection(){   
  92.         try{   
  93.             if(conn!=null){   
  94.                 conn.close();   
  95.                 conn = null;   
  96.             }   
  97.         }catch(Exception ex){ex.printStackTrace();}   
  98.     }   
  99. }   
Code:
  1. package po;   
  2. //封裝一個學生資料   
  3. public class Student {   
  4.     private String stuId;   
  5.     private String stuName;   
  6.     private String stuSex;   
  7.     private String stuBir;   
  8.     private String stuAdd;   
  9.     public String getStuAdd() {   
  10.         return stuAdd;   
  11.     }   
  12.     public void setStuAdd(String stuAdd) {   
  13.         this.stuAdd = stuAdd;   
  14.     }   
  15.     public String getStuBir() {   
  16.         return stuBir;   
  17.     }   
  18.     public void setStuBir(String stuBir) {   
  19.         this.stuBir = stuBir;   
  20.     }   
  21.     public String getStuId() {   
  22.         return stuId;   
  23.     }   
  24.     public void setStuId(String stuId) {   
  25.         this.stuId = stuId;   
  26.     }   
  27.     public String getStuName() {   
  28.         return stuName;   
  29.     }   
  30.     public void setStuName(String stuName) {   
  31.         this.stuName = stuName;   
  32.     }   
  33.     public String getStuSex() {   
  34.         return stuSex;   
  35.     }   
  36.     public void setStuSex(String stuSex) {   
  37.         this.stuSex = stuSex;   
  38.     }   
  39. }   

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.