教師管理數據庫系統:
建立步驟:
1)導入jar包,建立數據庫建立用到的一些基本信息表,與數據庫連接。
2)將一些常用函數導入到DbOperator類中,形成一個抽象工具包!
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();
}
}
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)建立面向對象的 增刪查修等用到的語句!
<1>建立接口
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>實現接口中定義的所有的方法
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;
}
} 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;
}
} 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;
}
} 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);
}
} 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"));
}
} 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"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// TODO Auto-generated method stub
return ts;
}
}
3)構建前臺的網頁對應的jsp(對應的網頁代碼太多不再一一顯示)
jsp文件用於存放一些靜態代碼,也可以用來添加一些動態代碼,與html是不同的! web.xml配置文件,是專門用來配置servlet文件的!
SQLYog 是MySQL 的圖形化頁面,最基礎的調用還是 通過dos窗口(黑框);做應用程序的時候特別是web應用程序的時候要寫好包名,跟架構有關係。