JSP+Servlet+JavaBean+DAO------學生體質資訊管理

來源:互聯網
上載者:User

標籤:介面   工具   error:   訪問   color   學生   exce   unicode   manager   

1.描述學生資訊的資料類:Student

package exam1;public class Student {    private int id;    private String name;    private String sex;    private int age;    private float weight;    private float hight;    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getSex() {        return sex;    }    public void setSex(String sex) {        this.sex = sex;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    public float getWeight() {        return weight;    }    public void setWeight(float weight) {        this.weight = weight;    }    public float getHight() {        return hight;    }    public void setHight(float hight) {        this.hight = hight;    }    }

2.資料庫連接和關閉的工具:JavaBean

package exam1;import java.sql.*;public class DbConnect {    private static String driverName="com.mysql.jdbc.Driver";    private static String userName="root";    private static String userPwd="123456";    private static String dbName="student";    static Connection getDBconnection(){        String url1="jdbc:mysql://localhost:3306/"+dbName;        String url2="?user="+userName+"&password="+userPwd;        String url3="&useUnicode=true&characterEncoding=UTF-8";        String url=url1+url2+url3;        try{            Class.forName(driverName);            Connection con=DriverManager.getConnection(url);            return con;        }catch(Exception e){            e.printStackTrace();        }        return null;    }    public static void closeDB(Connection con,PreparedStatement pstmt,ResultSet rs){        try{            if(rs!=null)rs.close();            if(pstmt!=null)pstmt.close();            if(con!=null)con.close();        }catch(SQLException e){            e.printStackTrace();        }    }}

3.實現資料庫訪問和商務邏輯的結合體DAO

package exam1;import java.sql.*;import java.util.ArrayList;import java.util.List;public  class StudentDAO implements IStudentDAO {    protected static final String FIELDS_INSERT="id,name,sex,age,weight,hight";    protected static String INSERT_SQL="insert into stu_info("+FIELDS_INSERT+")"+"values(?,?,?,?,?,?)";    protected static String SELECT_SQL="select"+FIELDS_INSERT+"from stu_info where id=?";    protected static String UPDATE_SQL="update stu_info set"+"id=?,name=?,sex=?,age=?,weight=?,hight=? where id=?";    protected static String DELETE_SQL="delete from stu_info where id=?";         public Student create(Student stu)throws Exception{         Connection con=null;         PreparedStatement preStmt=null;         ResultSet rs=null;         try{             con=DbConnect.getDBconnection();             preStmt=con.prepareStatement(INSERT_SQL);             preStmt.setInt(1, stu.getId());             preStmt.setString(2, stu.getName());             preStmt.setString(3, stu.getSex());             preStmt.setInt(4, stu.getAge());             preStmt.setFloat(5, stu.getWeight());             preStmt.setFloat(6, stu.getHight());             preStmt.executeUpdate();         }catch(Exception e){         }finally{             DbConnect.closeDB(con, preStmt, rs);         }         return stu;     }    @Override    public void remove(Student stu) throws Exception {        // TODO Auto-generated method stub            }    @Override    public Student find(Student stu) throws Exception {     Connection con=null;        PreparedStatement preStmt=null;        ResultSet rs=null;        Student stu2=null;        try{            con=DbConnect.getDBconnection();            preStmt=con.prepareStatement(SELECT_SQL);            preStmt.setInt(1, stu.getId());            rs=preStmt.executeQuery();            if(rs.next()){                stu2=new Student();                stu2.setId(rs.getInt(1));                stu2.setName(rs.getString(2));               stu2.setSex(rs.getString(3));               stu2.setAge(rs.getInt(4));               stu2.setWeight(rs.getFloat(5));               stu2.setHight(rs.getFloat(6));            }        }catch(Exception e){        }finally{            DbConnect.closeDB(con, preStmt, rs);        }        return stu2;    }    @Override    public List<Student> findAll() throws Exception {        Connection con=null;            PreparedStatement prepStmt=null;            ResultSet rs=null;            List<Student> student=new ArrayList<Student>();            con=DbConnect.getDBconnection();            prepStmt=con.prepareStatement("select * from stu_info");           while(rs.next()){                    Student stu2=new Student();                    stu2.setId(rs.getInt(1));                    stu2.setName(rs.getString(2));                   stu2.setSex(rs.getString(3));                   stu2.setAge(rs.getInt(4));                   stu2.setWeight(rs.getFloat(5));                   stu2.setHight(rs.getFloat(6));                   student.add(stu2);                }                DbConnect.closeDB(con, prepStmt, rs);            return student;    }    @Override    public void update(Student stu) throws Exception {        Connection con=null;        PreparedStatement preStmt=null;        ResultSet rs=null;        try{            con=DbConnect.getDBconnection();            preStmt=con.prepareStatement(UPDATE_SQL);            preStmt.setInt(1, stu.getId());            preStmt.setString(2, stu.getName());            preStmt.setString(3, stu.getSex());            preStmt.setInt(4, stu.getAge());            preStmt.setFloat(5, stu.getWeight());            preStmt.setFloat(6, stu.getHight());            int rowCount=preStmt.executeUpdate();            if(rowCount==0){                throw new Exception("Update Error:Student Id:"+stu.getId());            }        }catch(Exception e){        }finally{            DbConnect.closeDB(con, preStmt, rs);        }            }}

4.實現商務邏輯處理的介面

package exam1;import java.util.List;public interface IStudentDAO {    public abstract Student create(Student stu)throws Exception;    public abstract void remove(Student stu)throws Exception;    public abstract Student find(Student stu)throws Exception;    public abstract List<Student>findAll()throws Exception;    public abstract void update(Student stu)throws Exception;}

 

JSP+Servlet+JavaBean+DAO------學生體質資訊管理

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.