Jsp+servlet+javabean+dao------Students ' physique information management

Source: Internet
Author: User
Tags rowcount

1. Data class describing student information: Student

 Packageexam1; Public classStudent {Private intID; PrivateString name; PrivateString sex; Private intAge ; Private floatweight; Private floathight;  Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString Getsex () {returnsex; }     Public voidsetsex (String sex) { This. Sex =sex; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }     Public floatgetweight () {returnweight; }     Public voidSetweight (floatweight) {         This. Weight =weight; }     Public floatgethight () {returnhight; }     Public voidSethight (floathight) {         This. hight =hight; }    }

2. Database connection and shutdown tools: JavaBean

 Packageexam1;ImportJava.sql.*; Public classDbconnect {Private StaticString drivername= "Com.mysql.jdbc.Driver"; Private StaticString username= "Root"; Private StaticString userpwd= "123456"; Private StaticString dbname= "Student"; StaticConnection 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); returncon; }Catch(Exception e) {e.printstacktrace (); }        return NULL; }     Public Static voidclosedb (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. Implementing a combination of database access and business logic DAO

 Packageexam1;ImportJava.sql.*;Importjava.util.ArrayList;Importjava.util.List; Public  classStudentdaoImplementsIstudentdao {protected Static FinalString fields_insert= "Id,name,sex,age,weight,hight"; protected StaticString insert_sql= "INSERT into Stu_info (" +fields_insert+ ")" + "VALUES (?,?,?,?,?,?)"; protected StaticString select_sql= "Select" +fields_insert+ "from Stu_info where id=?"; protected StaticString update_sql= "UPDATE stu_info Set" + "id=?,name=?,sex=?,age=?,weight=?,hight=? where id=? "; protected StaticString delete_sql= "DELETE from Stu_info where id=?";  PublicStudent Create (Student stu)throwsexception{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); }         returnStu; } @Override Public voidRemove (Student stu)throwsException {//TODO auto-generated Method Stub} @Override PublicStudent Find (Student stu)throwsException {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=NewStudent (); 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); }        returnSTU2; } @Override PublicList<student> FindAll ()throwsException {Connection con=NULL; PreparedStatement prepstmt=NULL; ResultSet RS=NULL; List<Student> student=NewArraylist<student>(); Con=dbconnect.getdbconnection (); Prepstmt=con.preparestatement ("SELECT * from Stu_info");  while(Rs.next ()) {Student STU2=NewStudent (); 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); returnstudent; } @Override Public voidUpdate (Student Stu)throwsException {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 ()); intRowcount=prestmt.executeupdate (); if(rowcount==0){                Throw NewException ("Update error:student Id:" +Stu.getid ()); }        }Catch(Exception e) {}finally{dbconnect.closedb (Con, prestmt, RS); }            }}

4. Interfaces to implement business logic processing

 Packageexam1;Importjava.util.List; Public InterfaceIstudentdao { Public AbstractStudent Create (Student stu)throwsException;  Public Abstract voidRemove (Student stu)throwsException;  Public AbstractStudent Find (Student stu)throwsException;  Public AbstractList<student>findall ()throwsException;  Public Abstract voidUpdate (Student Stu)throwsException;}

Jsp+servlet+javabean+dao------Students ' physique information management

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.