Teacher Management Database system:
Establish steps:
1 Import jar package, set up database to build some basic information table, connect with database.
2 to import some common functions into the Dboperator class to form an abstract tool pack.
Package cn.edu.hpu.util;
public class Dboperator {public static final String dbdriver = "Com.mysql.jdbc.Driver";
public static final String Dbdurl = "Jdbc:mysql://localhost:3306/tennis";
public static final String dbuser = "root";
public static final String Dbpass = "root";
static {try {class.forname (dbdriver);
catch (ClassNotFoundException e) {e.printstacktrace ();
The public static Connection getconnection () {Connection coon = null;
try {coon = drivermanager.getconnection (Dbdurl,dbuser,dbpass);
catch (SQLException e) {e.printstacktrace ();
Return coon; public static void Close (ResultSet rs, Statement St, Connection conn) {try {if (RS
!= null) {rs.close ();
} if (St!= null) {st.close ();
} IF (conn!= null) {conn.close ();
}}catch (Exception ex) {ex.printstacktrace ();
} public static void Close (Statement St, Connection Coon) {close (Null,st,coon);
}
}
3) To establish object-oriented additions, deletions, Chashiu and other statements.
<1> Establish interface
Package cn.edu.hpu.service;
Import java.util.List;
Import Cn.edu.hpu.model.TeacherStaff;
Public interface Teacherstaffmanager {public
boolean add (Teacherstaff ts);
public boolean del (int id);
public boolean update (Teacherstaff ts);
Public list<teacherstaff> getteacherstaffs ();
Public Teacherstaff getteacherstaffbyname (String name);
Public Teacherstaff Getteacherstaffbyid (int id);
}
<2> implement all the methods defined in the interface
Package cn.edu.hpu.service;
Import java.io.Closeable;
Import java.sql.Connection;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
Import java.util.ArrayList;
Import java.util.List;
Import Cn.edu.hpu.model.TeacherStaff;
Import Cn.edu.hpu.util.DbOperator; public class Teacherstaffmanagerimpl implements teacherstaffmanager{@Override public boolean add (Teacherstaff ts
) {Boolean flag=false;
Connection con = null;
PreparedStatement PST = NULL;
String sql = "INSERT into Teacherstaff values (?,?,?,?,?,?)";
con = dboperator.getconnection ();
try {PST = con.preparestatement (SQL);
Pst.setint (1, Ts.getid ());
Pst.setstring (2, Ts.getname ());
Pst.setstring (3, Ts.getsex ());
Pst.setstring (4, Ts.getbirthdate ());
Pst.setstring (5, Ts.getlocation ());
Pst.setstring (6, ts.getposition ()); int row = Pst.executeupdate ();
if (row>0) {flag = true;
The catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();
}//TODO auto-generated method stub return flag;
@Override public boolean del (int id) {Boolean flag = false;
Connection con = null;
PreparedStatement PST = NULL;
String sql = "Delete from Teacherstaff where id=?";
con = dboperator.getconnection ();
try {PST = con.preparestatement (SQL);
Pst.setint (1, id);
int row = Pst.executeupdate ();
if (row>0) {flag=true;
The catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();
}//TODO auto-generated method stub return flag; @Override public boolean update (teachErstaff ts) {Boolean flag = false;
Connection con = null;
PreparedStatement PST = NULL; String sql = "Update teacherstaff set name=?,sex=?,birthdate=?,location=?,position=?"
where id=? ";
con = dboperator.getconnection ();
try {PST = con.preparestatement (SQL);
Pst.setstring (1, Ts.getname ());
Pst.setstring (2, Ts.getsex ());
Pst.setstring (3, Ts.getbirthdate ());
Pst.setstring (4, Ts.getlocation ());
Pst.setstring (5, Ts.getposition ());
Pst.setint (6, Ts.getid ());
int row = Pst.executeupdate ();
if (Row > 0) {flag = true;
The catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();
}//TODO auto-generated method stub return flag; @Override public list<teacherstaff> getteacherstaffs () {List List = new Arraylist<teacherstaff> ();
Statement st = null;
Connection con = null;
ResultSet rs = null;
String sql = "SELECT * from Teacherstaff";
con = dboperator.getconnection ();
try {st = con.createstatement ();
rs = st.executequery (SQL);
while (Rs.next ()) {Teacherstaff ts = new Teacherstaff ();
Ts.setid (Rs.getint ("id"));
Ts.setname (rs.getstring ("name"));
Ts.setsex (rs.getstring ("Sex"));
Ts.setbirthdate (rs.getstring ("birthdate"));
Ts.setlocation (rs.getstring ("location"));
Ts.setposition (rs.getstring ("position"));
List.add (TS);
The catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();
//TODO auto-generated method stub return list; } @Override Public Teacherstaff GetteaCherstaffbyname (String name) {Connection con = null;
Statement st = null;
ResultSet rs = null;
Teacherstaff ts = new Teacherstaff ();
String sql = "SELECT * from Teacherstaff where name= '" +name+ "";
con = dboperator.getconnection ();
try {st = con.createstatement ();
rs = st.executequery (SQL);
if (Rs.next ()) {Ts.setid (Rs.getint ("id"));
Ts.setname (rs.getstring ("name"));
Ts.setsex (rs.getstring ("Sex"));
Ts.setbirthdate (rs.getstring ("birthdate"));
Ts.setlocation (rs.getstring ("location"));
Ts.setposition (rs.getstring ("position"));
The catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();
}//TODO auto-generated method stub return TS;
@Override public teacherstaff Getteacherstaffbyid (int id) { Connection con = null;
Statement st = null;
ResultSet rs = null;
Teacherstaff ts = new Teacherstaff ();
String sql = "SELECT * from Teacherstaff where id=" +ID;
con = dboperator.getconnection ();
try {st = con.createstatement ();
rs = st.executequery (SQL);
if (Rs.next ()) {Ts.setid (Rs.getint ("id"));
Ts.setname (rs.getstring ("name"));
Ts.setsex (rs.getstring ("Sex"));
Ts.setbirthdate (rs.getstring ("birthdate"));
Ts.setlocation (rs.getstring ("location"));
Ts.setposition (rs.getstring ("position"));
The catch (SQLException e) {//TODO auto-generated catch block E.printstacktrace ();
}//TODO auto-generated method stub return TS;
}
}
3) Building a front page corresponding to the JSP (the corresponding page code too much no longer one by one display)
JSP file to store some static code, can also be used to add some dynamic code, and HTML is different. Web.xml configuration file, which is specifically used to configure servlet files.
SQLyog is a graphical page of MySQL, the most basic call or through a DOS window (black box), when the application, especially the Web application to write the package name, and architecture.