本系统采用Java +jsp+servlet+mysql+eclipse实现,jdbc编程,具有简单的增删改查等操作,适合初学者满足基本的需求。
1、实体类GuaHao和User类。
package entity;
public class User {
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
private String username;
private String password;
}
package entity;
import java.sql.Date;
public class Guahao {
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getNo() {
return no;
}
public void setNo(Integer no) {
this.no = no;
}
public String getCondation() {
return condation;
}
public void setCondation(String condation) {
this.condation = condation;
}
public String gethName() {
return hName;
}
public void sethName(String hName) {
this.hName = hName;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
private Integer no;
private String condation;
private String hName;
private Date createTime;
}
2.Jdbc连接数据
public class DBConnection {
final static String DRIVER="com.mysql.jdbc.Driver";
final static String URL="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8";
final static String USER="root";
final static String PASSWORD="123456";
public static Connection getConnection(){
try{
Class.forName(DRIVER);
Connection connection=DriverManager.getConnection(URL,USER,PASSWORD);
return connection;
}catch(Exception e){
e.printStackTrace();
return null;
}
}
public static void closeConnection(Connection c){
try{
c.close();
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args){
System.out.println("绿色"+DBConnection.getConnection());
}
}
3、Dao层
package mapper;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import entity.Guahao;
import utils.DBConnection;
public class GuhaoMapper {
public List<Guahao> get_ListInfo(){
List<Guahao> list = new ArrayList<Guahao>();
Connection conn=DBConnection.getConnection();
String sql = "select * from guahao";
PreparedStatement stm = null;
ResultSet rs = null;
try {
stm = conn.prepareStatement(sql);
rs = stm.executeQuery();
while(rs.next()){
Guahao gh = new Guahao();
gh.setId(rs.getInt("id"));
gh.setNo(rs.getInt("no"));
gh.setCondation(rs.getString("condation"));
gh.sethName(rs.getString("hName"));
gh.setCreateTime(rs.getDate("createTime"));
list.add(gh);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
DBConnection.closeConnection(conn);
}
return list;
}
public boolean insert(Guahao guahao){
Connection conn=DBConnection.getConnection();
String sql = "insert into guahao(no,condation,hName,createTime) values(?,?,?,?)";
try{
PreparedStatement pst=conn.prepareStatement(sql);
pst.setInt(1, guahao.getNo());
pst.setString(2, guahao.getCondation());
pst.setString(3, guahao.gethName());
pst.setDate(4,guahao.getCreateTime());
pst.execute();
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}finally{
DBConnection.closeConnection(conn);
}
}
public boolean update(Guahao guahao,int id) {
Connection conn=null;
PreparedStatement stm=null;
ResultSet rs=null;
try{
conn=DBConnection.getConnection();
String sql="update guahao set no=?,condation=?,hName=?,createTime=? where id=?";
stm=conn.prepareStatement(sql);
stm.setInt(1,guahao.getNo());
stm.setString(2, guahao.getCondation());
stm.setString(3, guahao.gethName());
stm.setDate(4, guahao.getCreateTime());
stm.setInt(5,id);
stm.execute();
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}finally{
DBConnection.closeConnection(conn);
}
}
public List<Guahao> findByNo(int no){
Connection conn=null;
List<Guahao> list =new ArrayList<Guahao>();
ResultSet rs=null;
PreparedStatement stm=null;
try{
conn=DBConnection.getConnection();
String sql="select * from guahao where no=?";
stm=conn.prepareStatement(sql);
stm.setInt(1, no);
rs=stm.executeQuery();
while(rs.next()) {
Guahao gh =new Guahao();
gh.setId(rs.getInt("id"));
gh.setNo(rs.getInt("no"));
gh.setCondation(rs.getString("condation"));
gh.sethName(rs.getString("hName"));
gh.setCreateTime(rs.getDate("createTime"));
list.add(gh);
}
return list;
}catch(Exception e){
e.printStackTrace();
return null;
}finally{
DBConnection.closeConnection(conn);
}
}
public Guahao queryById(int id){
Connection conn=null;
PreparedStatement stm=null;
ResultSet rs=null;
try{
conn=DBConnection.getConnection();
String sql="select * from guahao where id=?";
stm=conn.prepareStatement(sql);
stm.setInt(1,id);
rs=stm.executeQuery();
if(rs.next()){
Guahao gh =new Guahao();
gh.setId(rs.getInt("id"));
gh.setNo(rs.getInt("no"));
gh.setCondation(rs.getString("condation"));
gh.sethName(rs.getString("hName"));
gh.setCreateTime(rs.getDate("createTime"));
return gh;
}else{
return null;
}
}catch(Exception e){
e.printStackTrace();
return null;
}finally{
DBConnection.closeConnection(conn);
}
}
public boolean delete( int id) {
Connection conn=null;
try{
conn=DBConnection.getConnection();
String sql="delete from guahao where id=?";
PreparedStatement pst=conn.prepareStatement(sql);
pst.setInt(1,id);
pst.execute();
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}finally{
DBConnection.closeConnection(conn);
}
}
}
4、效果图
以上是本系统的重要代码,希望给大家一个参考。如若需要源码或者制定类似的中小型管理系统,请加QQ1728608455.,欢迎咨询