學習開發環境:Myeclipse 7.0 Mysql 6.0 tomcat 6.0
資料庫名:school 表名:學籍 列名:學號、姓名、性別、出生年月、家庭地址。
老是用到的兩個類:dao.Student類(負責與資料庫互動)和po.Student(javabean)
Code:
- package dao;
-
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.util.ArrayList;
-
- import po.Student;
-
- //訪問資料庫
- public class StudentDao {
- private Connection conn = null;
-
- public void initConnection(){ //初始化資料庫連接
- try{
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- String DbConn = "jdbc:mysql://localhost:3306/school";
- String DbPass = "123";
- conn = java.sql.DriverManager.getConnection(DbConn, "root", DbPass);
- }catch(Exception ex){ex.printStackTrace();}
- }
-
- public ArrayList queryStus(){ //查詢所有的學生
- ArrayList stus = new ArrayList();
- String sql = "SELECT 學號,姓名,性別,出生年月,家庭地址 FROM 學籍 ";
- try{
- this.initConnection();//意味著每次串連都是重新串連資料庫,同時在該函數中也會關閉所有的串連。
- ResultSet rs = conn.createStatement().executeQuery(sql);
- while(rs.next()){
- Student stu = new Student();
- stu.setStuId(rs.getString("學號"));
- stu.setStuName(rs.getString("姓名"));
- stu.setStuSex(rs.getString("性別"));
- stu.setStuBir(rs.getString("出生年月"));
- stu.setStuAdd(rs.getString("家庭地址"));
- stus.add(stu);
- }
- }catch(Exception ex){ex.printStackTrace();}
- finally{
- this.closeConnection(); //查詢的最後總會關掉串連,以免佔用資源
- }
- return stus;
- }
-
- public ArrayList queryBySex(String sex){ //查詢所有的學生
- ArrayList stus = new ArrayList();
- String sql = "SELECT 學號,姓名,性別,出生年月,家庭地址 FROM 學籍 where 性別='"+sex+"'";
-
- try{
- this.initConnection();//意味著每次串連都是重新串連資料庫,同時在該函數中也會關閉所有的串連。
- ResultSet rs = conn.createStatement().executeQuery(sql);
- while(rs.next()){
- Student stu = new Student();
- stu.setStuId(rs.getString("學號"));
- stu.setStuName(rs.getString("姓名"));
- stu.setStuSex(rs.getString("性別"));
- stu.setStuBir(rs.getString("出生年月"));
- stu.setStuAdd(rs.getString("家庭地址"));
- stus.add(stu);
- }
- }catch(Exception ex){ex.printStackTrace();}
- finally{
- this.closeConnection(); //查詢的最後總會關掉串連,以免佔用資源
- }
- return stus;
- }
-
- public ArrayList queryBySA(String sex,String add){ //查詢所有的學生
- ArrayList stus = new ArrayList();
- String sql = "SELECT 學號,姓名,性別,出生年月,家庭地址 FROM 學籍 where 性別='"+sex+"' and 家庭地址='"+add+"'";
-
- try{
- this.initConnection();//意味著每次串連都是重新串連資料庫,同時在該函數中也會關閉所有的串連。
- ResultSet rs = conn.createStatement().executeQuery(sql);
- while(rs.next()){
- Student stu = new Student();
- stu.setStuId(rs.getString("學號"));
- stu.setStuName(rs.getString("姓名"));
- stu.setStuSex(rs.getString("性別"));
- stu.setStuBir(rs.getString("出生年月"));
- stu.setStuAdd(rs.getString("家庭地址"));
- stus.add(stu);
- }
- }catch(Exception ex){ex.printStackTrace();}
- finally{
- this.closeConnection(); //查詢的最後總會關掉串連,以免佔用資源
- }
- return stus;
- }
-
-
- public void closeConnection(){
- try{
- if(conn!=null){
- conn.close();
- conn = null;
- }
- }catch(Exception ex){ex.printStackTrace();}
- }
- }
Code:
- package po;
- //封裝一個學生資料
- public class Student {
- private String stuId;
- private String stuName;
- private String stuSex;
- private String stuBir;
- private String stuAdd;
- public String getStuAdd() {
- return stuAdd;
- }
- public void setStuAdd(String stuAdd) {
- this.stuAdd = stuAdd;
- }
- public String getStuBir() {
- return stuBir;
- }
- public void setStuBir(String stuBir) {
- this.stuBir = stuBir;
- }
- public String getStuId() {
- return stuId;
- }
- public void setStuId(String stuId) {
- this.stuId = stuId;
- }
- public String getStuName() {
- return stuName;
- }
- public void setStuName(String stuName) {
- this.stuName = stuName;
- }
- public String getStuSex() {
- return stuSex;
- }
- public void setStuSex(String stuSex) {
- this.stuSex = stuSex;
- }
- }