Model
实体类
public class Boys {
private int id;
private String boyName;
private int userCP;
@Override
public String toString() {
return "Boys{" +
"id=" + id +
", boyName='" + boyName + '\'' +
", userCP=" + userCP +
'}';
}
public Boys(int id, String boyName, int userCP) {
this.id = id;
this.boyName = boyName;
this.userCP = userCP;
}
public Boys() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBoyName() {
return boyName;
}
public void setBoyName(String boyName) {
this.boyName = boyName;
}
public int getUserCP() {
return userCP;
}
public void setUserCP(int userCP) {
this.userCP = userCP;
}
}
DAO层
import com.miyon.entity.Boys;
import com.miyon.utils.JDBCUtilsByDruid;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class BoysDao {
public void addBoys(Boys boys) throws Exception{
Connection conn = JDBCUtilsByDruid.getConnection();
String sql = "insert into boys values (null,?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1,boys.getBoyName());
ps.setInt(2,boys.getUserCP());
ps.execute();
}
public void updateBoys(Boys boys) throws Exception{
Connection conn = JDBCUtilsByDruid.getConnection();
String sql = " update boys set boyName=?, userCp=? where id=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1,boys.getBoyName());
ps.setInt(2,boys.getUserCP());
ps.setInt(3,boys.getId());
ps.execute();
}
public List<Boys> queryAll(List<Map<String,Object>> params) throws Exception{
List<Boys> result = new ArrayList<>();
Connection conn = JDBCUtilsByDruid.getConnection();
StringBuilder sb = new StringBuilder();
sb.append("select * from boys where 1=1");
if(params!=null && params.size()!=0){
for (int i = 0; i < params.size(); i++) {
Map<String,Object> map = params.get(i);
sb.append(" and "+map.get("name")+ " "+map.get("relation")+" "+map.get("value")+" ");
}
PreparedStatement ps = conn.prepareStatement(sb.toString());
ResultSet set = ps.executeQuery();
Boys boys = null;
while (set.next()){
boys = new Boys();
boys.setId(set.getInt("id"));
boys.setBoyName(set.getString("boyName"));
boys.setUserCP(set.getInt("userCp"));
result.add(boys);
}
System.out.println(sb);
return result;
}
return null;
}
public void deleteBoys(Boys boys) throws Exception{
Connection conn = JDBCUtilsByDruid.getConnection();
String sql = " update boys set userCp = ? where id=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1,boys.getUserCP());
ps.setInt(2,boys.getId());
ps.execute();
}
public List<Boys> queryBoys() throws Exception{
Connection conn = JDBCUtilsByDruid.getConnection();
String sql = "select * from boys ";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet set = ps.executeQuery();
List<Boys> list = new ArrayList<>();
Boys boys = null;
while (set.next()){
boys = new Boys();
boys.setId(set.getInt("id"));
boys.setBoyName(set.getString("boyName"));
boys.setUserCP(set.getInt("userCp"));
list.add(boys);
}
return list;
}
public Boys querySingle(int id) throws Exception{
Connection conn = JDBCUtilsByDruid.getConnection();
String sql = " select * from boys where id =? ";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1,id);
ResultSet set = ps.executeQuery();
Boys boys =null;
while (set.next()){
boys = new Boys();
boys.setId(set.getInt("id"));
boys.setBoyName(set.getString("boyName"));
boys.setUserCP(set.getInt("userCp"));
}
return boys;
}
}
Control(Action)
import com.miyon.dao.impl.BoysDao;
import com.miyon.entity.Boys;
import java.util.List;
import java.util.Map;
public class BoysAction {
public void add(Boys boys) throws Exception {
BoysDao boysDao = new BoysDao();
boysDao.addBoys(boys);
}
public void update(Boys boys) throws Exception {
BoysDao boysDao = new BoysDao();
boysDao.updateBoys(boys);
}
public void delete(Boys boys) throws Exception {
BoysDao boysDao = new BoysDao();
boysDao.deleteBoys(boys);
}
public Boys querySingle(int id) throws Exception {
BoysDao boysDao = new BoysDao();
return boysDao.querySingle(id);
}
public List<Boys> queryAllByCondition(List<Map<String,Object>> params) throws Exception {
BoysDao boysDao = new BoysDao();
return boysDao.queryAll(params);
}
public List<Boys> queryAll() throws Exception {
BoysDao boysDao = new BoysDao();
return boysDao.queryBoys();
}
}
View(逻辑跳转)
登录界面
import com.miyon.dao.impl.AdminDaoImpl;
import com.miyon.entity.Admin;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyVetoException;
import java.util.ResourceBundle;
public class LoginFrame {
public static void main(String[] args) {
LoginFrame loginFrame = new LoginFrame();
loginFrame.LoginGui();
}
Font d = new Font("楷体", Font.BOLD, 36);
Font f = new Font("楷体", Font.BOLD, 18);
JFrame logingui = new JFrame("仓库管理系统");
JLabel adminlogin = new JLabel("管理员登录");
JLabel username = new JLabel("用户名:");
JLabel password = new JLabel("密 码:");
JTextField name = new JTextField();
JTextField pwd = new JPasswordField();
JButton login = new JButton("登录");
JButton reset = new JButton("重置");
Admin admin = new Admin();
public void LoginGui(){
logingui.setBounds(600,300,700,500);
logingui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
logingui.setLayout(null);
adminlogin.setBounds(240,50,220,50);
adminlogin.setFont(d);
username.setBounds(110,150,120,50);
username.setFont(f);
password.setBounds(110,210,120,50);
password.setFont(f);
name.setBounds(250,150,300,50);
name.setFont(f);
pwd.setBounds(250,210,300,50);
pwd.setFont(f);
login.setBounds(230,350,150,60);
login.setFont(f);
reset.setBounds(415,350,150,60);
reset.setFont(f);
logingui.add(adminlogin);
logingui.add(username);
logingui.add(password);
logingui.add(name);
logingui.add(pwd);
logingui.add(login);
logingui.add(reset);
logingui.setVisible(true);
logingui.setResizable(false);
logingui.setLocationRelativeTo(null);
login.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name_text = name.getText();
String pwd_text = pwd.getText();
admin.setUsername(name_text);
admin.setPassword(pwd_text);
AdminDaoImpl ad = new AdminDaoImpl();
int i = ad.login(name_text, pwd_text);
if(i==1){
JOptionPane.showMessageDialog(null,"登录成功!");
logingui.setVisible(false);
new MainFrame();
}
if(i==0){
JOptionPane.showMessageDialog(null,"登录失败,检查用户名或密码");
}
}
});
reset.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
resetValueActionPerformed(arg0);
}
});
}
public void resetValueActionPerformed(ActionEvent arg0) {
this.resetValue();
}
private void resetValue(){
this.name.setText("");
this.pwd.setText("");
}
public static class MainFrame extends JFrame {
public MainFrame() {
initComponents();
}
private void okButtonActionPerformed(ActionEvent e) {
}
private void addGoodsActionPerformed(ActionEvent e) throws PropertyVetoException {
desktopPane1.add(new AddGoodsInnerFrame());
}
private void exitSystemActionPerformed(ActionEvent e) {
int n = JOptionPane.showConfirmDialog(null,"确定离开?");
if(n==0){
System.exit( 0);
return;
}
}
public static void main(String[] args) {
new MainFrame().setVisible(true);
}
private void desktopPane1PropertyChange(PropertyChangeEvent e) {
}
private void initComponents() {
ResourceBundle bundle = ResourceBundle.getBundle("com.miyon.jform");
dialogPane = new JPanel();
contentPanel = new JPanel();
menuBar1 = new JMenuBar();
menu1 = new JMenu();
menuItem1 = new JMenuItem();
menuItem2 = new JMenuItem();
menuItem3 = new JMenuItem();
menu2 = new JMenu();
menuItem4 = new JMenuItem();
menuItem5 = new JMenuItem();
menuItem6 = new JMenuItem();
menu3 = new JMenu();
menuItem7 = new JMenuItem();
panel1 = new JPanel();
desktopPane1 = new JDesktopPane();
setTitle(bundle.getString("this.title"));
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setResizable(false);
setName("\u8d27\u4ed3\u7ba1\u7406\u7cfb\u7edf\u4e3b\u9875\u9762");
setVisible(true);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
{
dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
dialogPane.setPreferredSize(new Dimension(1024, 580));
dialogPane.setBorder ( new javax . swing. border .CompoundBorder ( new javax . swing. border .TitledBorder ( new EmptyBorder ( 0, 0 ,0 , 0) , "JF\u006frmD\u0065sig\u006eer \u0045val\u0075ati\u006fn" , javax. swing .border . TitledBorder
. CENTER ,javax . swing. border .TitledBorder . BOTTOM, new Font ( "Dia\u006cog", Font. BOLD ,12 ) , Color .red ) ,dialogPane. getBorder () ) )
; dialogPane. addPropertyChangeListener( new java. beans .PropertyChangeListener ( ){ @Override public void propertyChange (PropertyChangeEvent e
) { if( "\u0062ord\u0065r" .equals ( e. getPropertyName () ) )throw new RuntimeException( ) ;} } )
;
dialogPane.setLayout(new BorderLayout());
{
contentPanel.setPreferredSize(new Dimension(1000, 50));
contentPanel.setMaximumSize(null);
contentPanel.setMinimumSize(null);
contentPanel.setName(bundle.getString("this.title"));
contentPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));
{
menuBar1.setPreferredSize(new Dimension(300, 24));
{
menu1.setText(bundle.getString("menu1.text"));
menu1.setPreferredSize(new Dimension(100, 21));
menuItem1.setText("\u6dfb\u52a0\u8d27\u54c1");
menuItem1.setPreferredSize(new Dimension(100, 21));
menuItem1.addActionListener(e -> {
try {
addGoodsActionPerformed(e);
} catch (PropertyVetoException propertyVetoException) {
propertyVetoException.printStackTrace();
}
});
menu1.add(menuItem1);
menuItem2.setText("\u66f4\u65b0\u8d27\u54c1");
menu1.add(menuItem2);
menuItem3.setText("\u67e5\u8be2\u8d27\u54c1");
menu1.add(menuItem3);
}
menuBar1.add(menu1);
{
menu2.setText("\u4ed3\u5e93\u7ba1\u7406");
menu2.setPreferredSize(new Dimension(100, 21));
menuItem4.setText("\u6dfb\u52a0\u4ed3\u5e93");
menuItem4.setPreferredSize(new Dimension(100, 21));
menu2.add(menuItem4);
menuItem5.setText("\u66f4\u65b0\u4ed3\u5e93");
menu2.add(menuItem5);
menuItem6.setText("\u67e5\u627e\u4ed3\u5e93");
menu2.add(menuItem6);
}
menuBar1.add(menu2);
{
menu3.setText("\u5b89\u5168\u9000\u51fa");
menu3.setPreferredSize(new Dimension(100, 21));
menuItem7.setText("\u9000\u51fa");
menuItem7.setPreferredSize(new Dimension(100, 21));
menuItem7.addActionListener(e -> exitSystemActionPerformed(e));
menu3.add(menuItem7);
}
menuBar1.add(menu3);
}
contentPanel.add(menuBar1);
}
dialogPane.add(contentPanel, BorderLayout.NORTH);
{
panel1.setPreferredSize(new Dimension(1000, 500));
panel1.setLayout(new BorderLayout());
{
desktopPane1.setPreferredSize(new Dimension(1000, 450));
desktopPane1.setBorder(new EmptyBorder(5, 5, 5, 5));
desktopPane1.addPropertyChangeListener(e -> desktopPane1PropertyChange(e));
}
panel1.add(desktopPane1, BorderLayout.NORTH);
}
dialogPane.add(panel1, BorderLayout.CENTER);
}
contentPane.add(dialogPane, BorderLayout.NORTH);
pack();
setLocationRelativeTo(getOwner());
}
private JPanel dialogPane;
private JPanel contentPanel;
private JMenuBar menuBar1;
private JMenu menu1;
private JMenuItem menuItem1;
private JMenuItem menuItem2;
private JMenuItem menuItem3;
private JMenu menu2;
private JMenuItem menuItem4;
private JMenuItem menuItem5;
private JMenuItem menuItem6;
private JMenu menu3;
private JMenuItem menuItem7;
private JPanel panel1;
private JDesktopPane desktopPane1;
}
}
utils
创建连接、关闭连接、其他工具…
public class InputUtil {
public static boolean isEmpty(String str){
if(str==null || "".equals(str.trim())){
return true;
}else{
return false;
}
}
public static boolean isNotEmpty(String str){
if(str!=null && !"".equals(str.trim())){
return true;
}else{
return false;
}
}
}
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
public class JDBCUtilsByDruid {
static DataSource ds;
static {
try {
Properties properties = new Properties();
properties.load(new FileInputStream("warehousemanagement\\src\\druid.properties"));
ds = DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws Exception{
return ds.getConnection();
}
public static void close(ResultSet set, PreparedStatement statement, Connection connection) throws Exception {
if (set != null) {
set.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
}
}
|