今日进度
ji那天学习了javaweb,做了一个小系统部分代码:
package machinepart;
public class Admin {
private int adminid;
private String name;
private String password;
public Admin(String name, String password) {
this.name = name;
this.password = password;
}
public int getAdminid() {
return adminid;
}
public void setAdminid(int adminid) {
this.adminid = adminid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
package machinepart;
public class Mpart {
private int machinepartid;
private String name;
private String data;
private int unfiled_orders;
public Mpart(int machinepartid, String name, String data, int unfiled_orders) {
super();
this.machinepartid = machinepartid;
this.name = name;
this.data = data;
this.unfiled_orders = unfiled_orders;
}
public int getMachinepartid() {
return machinepartid;
}
public void setMachinepartid(int machinepartid) {
this.machinepartid = machinepartid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public int getUnfiled_orders() {
return unfiled_orders;
}
public void setUnfiled_orders(int unfiled_orders) {
this.unfiled_orders = unfiled_orders;
}
}
增删改查:
部分代码:
package m_part.business;
import java.util.List;
import m_part.dao.MpartDao;
import machinepart.Mpart;
public class MpartServices {
private MpartDao mpartDao=new MpartDao();
//保存
public void saveMpart(Mpart mpart) {
mpartDao.saveMpart(mpart);
}
//修改
public void updataMpart(Mpart mpart) {
mpartDao.updataMpart(mpart);
}
//删除
public void deleteMpart(int machinepartid) {
mpartDao.deleteMpart(machinepartid);
}
//根据id查询
public Mpart findMpartById(int machinepartid ) {
return mpartDao.findMpartById(machinepartid);
}
//查询所有
public List<Mpart> findAllStudents(){
return mpartDao.findAllMparts();
}
}
package m_part.dao;
import com.mysql.jdbc.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import m_part.util.DBUtil;
import machinepart.Mpart;
public class MpartDao {
//保存
public void saveMpart(Mpart mpart) {
String sql="insert into m_part(machinepartid,name,data,unfiled_orders) values(?,?,?,?)";
PreparedStatement pstmt=DBUtil.getPreparedStatement(sql);
try {
pstmt.setInt(1,mpart.getMachinepartid());
pstmt.setString(2,mpart.getName());
pstmt.setString(3,mpart.getData());
pstmt.setInt(4,mpart.getUnfiled_orders());
pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBUtil.closeDBResources();
}
}
//修改
public void updataMpart(Mpart mpart) {
String sql="update m_part set name=?,data=?,unfiled_orders=? where machinepartid=?";
PreparedStatement pstmt=DBUtil.getPreparedStatement(sql);
try {
pstmt.setString(1,mpart.getName());
pstmt.setString(2,mpart.getData());
pstmt.setInt(3,mpart.getUnfiled_orders());
pstmt.setInt(4,mpart.getMachinepartid());
pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBUtil.closeDBResources();
}
}
//删除
public void deleteMpart(int machinepartid) {
String sql="delete from m_part where machinepartid=?";
PreparedStatement pstmt=DBUtil.getPreparedStatement(sql);
try {
pstmt.setInt(1,machinepartid);
pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBUtil.closeDBResources();
}
}
//根据id查询
public Mpart findMpartById(int machinepartid ) {
Mpart mpart=null;
String sql="select * from m_part where machinepartid=?";
PreparedStatement pstmt=DBUtil.getPreparedStatement(sql);
try {
pstmt.setInt(1,machinepartid);
ResultSet rs=pstmt.executeQuery();
if(rs.next()) {
String name=rs.getString("name");
String data=rs.getString("data");
int unfiled_orders=rs.getInt("unfiled_orders");
mpart=new Mpart(machinepartid,name,data,unfiled_orders);
}
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBUtil.closeDBResources();
}
return mpart;
}
//查询所有
public List<Mpart> findAllMparts(){
List<Mpart> mpartList=new ArrayList<Mpart>();
String sql="select * from m_part";
PreparedStatement pstmt=DBUtil.getPreparedStatement(sql);
try {
ResultSet rs=pstmt.executeQuery();
while(rs.next()) {
int machinepartid=rs.getInt("machinepartid");
String name=rs.getString("name");
String data=rs.getString("data");
int unfiled_orders=rs.getInt("unfiled_orders");
Mpart mpart=new Mpart(machinepartid,name,data,unfiled_orders);
mpartList.add(mpart);
}
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
DBUtil.closeDBResources();
}
return mpartList;
}
}