JDBC+swing+MySQL商品管理
---
package com.kao.JDBC; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class JDBC implements AutoCloseable{ private final static String QD="com.mysql.cj.jdbc.Driver"; private final static String LUJIN="jdbc:mysql://localhost:3306/goods_db"; private final static String NAME="root"; private final static String PASS="123456"; static { try { Class.forName(QD); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static Connection conn=null; public static ResultSet re=null; public static void getconn() { try { conn= DriverManager.getConnection(LUJIN, NAME, PASS); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void close() throws Exception { // TODO Auto-generated method stub if(conn!=null) { conn.close(); }if(re!=null) { re.close(); } } public static boolean updete(String sql, Object...obj) throws SQLException { getconn(); PreparedStatement pr=conn.prepareStatement(sql); if(obj!=null) { for(int i=0; i<obj.length;i++) { pr.setObject(i+1, obj[i]); } } return pr.executeLargeUpdate()>0; } public static ResultSet query(String sql, Object...obj) throws SQLException { getconn(); PreparedStatement pr=conn.prepareStatement(sql); if(obj!=null) { for(int i=0; i<obj.length;i++) { pr.setObject(i+1, obj[i]); } } return pr.executeQuery(); } }
package com.kao.JDBC; import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.table.DefaultTableModel; import javax.swing.JLabel; import javax.swing.JOptionPane; import java.awt.Font; import javax.swing.JTextField; import javax.swing.JButton; import javax.swing.JScrollPane; import javax.swing.JTable; import java.awt.event.ActionListener; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Vector; import java.awt.event.ActionEvent; public class MainFrame extends JFrame { private JPanel contentPane; private JTextField textField; private JTable table; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { MainFrame frame = new MainFrame(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public MainFrame() { setTitle("商品管理"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 877, 436); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JLabel lblNewLabel = new JLabel("按名字查询 :"); lblNewLabel.setFont(new Font("宋体", Font.BOLD, 18)); lblNewLabel.setBounds(148, 25, 141, 28); contentPane.add(lblNewLabel); textField = new JTextField(); textField.setBounds(274, 30, 174, 21); contentPane.add(textField); textField.setColumns(10); JButton btnNewButton = new JButton("查询"); btnNewButton.setBounds(495, 29, 97, 23); contentPane.add(btnNewButton); JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(36, 79, 669, 293); contentPane.add(scrollPane); // 列表 // =============================查询死记=========================================== String sql = "SELECT *FROM tb_goods"; try { ResultSet data= JDBC.query(sql); Vector<Vector<String>>list=new Vector<Vector<String>>(); while(data.next()) { Vector<String> v=new Vector<String>(); v.add(data.getString(1)); v.add(data.getString(2)); v.add(data.getString(3)); v.add(data.getString(4)); list.add(v); } System.out.println(list.size()); Vector<String> ti=new Vector<String>(); ti.add("商品编号"); ti.add("商品名称"); ti.add("商品数量"); ti.add("商品价格"); table = new JTable(list,ti); scrollPane.setViewportView(table); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } // ============================================================================ // 这个可和查列表混合写,不过我这样更容易理解 // =============================分开写这查寻获取要放在显示界面的数据的下面写好上面Cv就好==================================================== // =====================查询某个民字记得这个这两句就好 btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String a=textField.getText(); String sql ="SELECT sum(num) FROM tb_goods WHERE goodName LIKE '%"+a+"%'"; // ==================================================================== try { ResultSet data= JDBC.query(sql); Vector<Vector<String>>list=new Vector<Vector<String>>(); while(data.next()) { Vector<String> v=new Vector<String>(); v.add(data.getString(1)); // v.add(data.getString(2)); // v.add(data.getString(3)); // v.add(data.getString(4)); list.add(v); } System.out.println(list.size()); Vector<String> ti=new Vector<String>(); ti.add("商品编号"); // ti.add("商品名称"); // ti.add("商品数量"); // ti.add("商品价格"); table = new JTable(list,ti); scrollPane.setViewportView(table); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); // ====================================================================================== JButton btnNewButton_1 = new JButton("添加"); btnNewButton_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { MainFrameADD aa=new MainFrameADD(); aa.setVisible(true); // } }); btnNewButton_1.setBounds(736, 102, 97, 23); contentPane.add(btnNewButton_1); JButton btnNewButton_2 = new JButton("删除"); btnNewButton_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // ======这句话是获取他的相对位置====就是在列表上重零开始建议记我的==== // int hang=table.getSelectedRow(); // System.out.println(hang); // ==================我的这句话是获取你列表点中选择的行的第0个值======= int hang = Integer.parseInt(table.getValueAt(table.getSelectedRow(), 0).toString()); System.out.println(hang); if(hang==-1) { JOptionPane.showMessageDialog(null, "你还未选中要删除行"); }else { String sql="DELETE FROM tb_goods WHERE goodId="+hang; try { boolean aas =JDBC.updete(sql,null); if(aas==true) { JOptionPane.showMessageDialog(null, "删除成功"); }else { JOptionPane.showMessageDialog(null, "删除失败"); } } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } }); btnNewButton_2.setBounds(736, 190, 97, 23); contentPane.add(btnNewButton_2); } public void get(String sql) { } }
package com.kao.JDBC; import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import javax.swing.JButton; import java.awt.event.ActionListener; import java.sql.SQLException; import java.awt.event.ActionEvent; public class MainFrameADD extends JFrame { private JPanel contentPane; private JTextField textField; private JTextField textField_1; private JTextField textField_2; private JTextField textField_3; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { MainFrameADD frame = new MainFrameADD(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public MainFrameADD() { setTitle("商品添加"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 420, 491); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JLabel lblNewLabel = new JLabel("编号:"); lblNewLabel.setBounds(71, 91, 58, 25); contentPane.add(lblNewLabel); JLabel lblNewLabel_1 = new JLabel("名称:"); lblNewLabel_1.setBounds(71, 151, 58, 25); contentPane.add(lblNewLabel_1); JLabel lblNewLabel_2 = new JLabel("数量:"); lblNewLabel_2.setBounds(71, 212, 58, 25); contentPane.add(lblNewLabel_2); JLabel lblNewLabel_3 = new JLabel("价格:"); lblNewLabel_3.setBounds(71, 273, 58, 25); contentPane.add(lblNewLabel_3); textField = new JTextField(); textField.setBounds(139, 93, 151, 21); contentPane.add(textField); textField.setColumns(10); textField_1 = new JTextField(); textField_1.setColumns(10); textField_1.setBounds(139, 153, 151, 21); contentPane.add(textField_1); textField_2 = new JTextField(); textField_2.setColumns(10); textField_2.setBounds(139, 214, 151, 21); contentPane.add(textField_2); textField_3 = new JTextField(); textField_3.setColumns(10); textField_3.setBounds(139, 275, 151, 21); contentPane.add(textField_3); // ====================================重要=============================================================== JButton btnNewButton = new JButton("确认"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // =========增加第一步获取================ String a=textField.getText(); String b=textField_1.getText(); String c=textField_2.getText(); String d=textField_3.getText(); // ===================判断不为空====================== if(a.equals("")||b.equals("")||c.equals("")||d.equals("")) { JOptionPane.showMessageDialog(null, "你的数据不完整"); }else { // ======================用他别名就好====记得字符要加单引号 不过你你可以简答一点记单引号包双引号包俩加号中间放别名=================== String sql="INSERT INTO tb_goods VALUES('"+a+"','"+b+"','"+c+"','"+d+"')"; try { boolean aa=JDBC.updete(sql, null); if(aa==true){ JOptionPane.showMessageDialog(null, "添加成功"); // ===================这个是添加完自动关闭添加界面=================== MainFrame aaa=new MainFrame(); aaa.setVisible(true); dispose(); // ======================================= }else { JOptionPane.showMessageDialog(null, "添加失败检查数据"); } } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } }); btnNewButton.setBounds(49, 377, 97, 23); contentPane.add(btnNewButton); // ========================================================================================================= JButton btnNewButton_1 = new JButton("取消"); btnNewButton_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { MainFrame aa=new MainFrame(); aa.setVisible(true); dispose(); } }); btnNewButton_1.setBounds(222, 377, 97, 23); contentPane.add(btnNewButton_1); } }
运行起来的样子上面的功能都正常可以用
有用记得点个赞
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通