@Erqi 2021-08-02 19:49 采纳率: 0%
浏览 25
已结题

使用jframe操作数据库数据,一定要先创建对象类,使用集合操作吗

做一个简易的用户管理系统,实现增删改查功能

两个jframe间的数据传输怎么实现?
没有对象类

img

jtable呈现代码

all.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String sql = "select num as 序号  ,date as 日期 ,name as 姓名,phone as 手机号,sex as 性别  ,age as 年龄 ,history as 病例    from vip";
                DUBtil db = new DUBtil();
                try {
                    db.getConnection();
                    ResultSet rs = db.executeQuery(sql, null);
                    ResultSetMetaData rsmd = rs.getMetaData();
                    // 获取列数
                    int colCount = rsmd.getColumnCount();
                    // 存放列名
                    Vector<String> title = new Vector<String>();
                    // 列名
                    
                    for (int i = 1; i <= colCount; i++) {
                        title.add(rsmd.getColumnLabel(i));
                        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
//                        fitTableColumns(table);
                        
                    }
        
                    // 表格数据
                    Vector<Vector<String>> data = new Vector<Vector<String>>();
                    int rowCount = 0;
                    while (rs.next()) {
                        rowCount++;
                        // 行数据
                        Vector<String> rowdata = new Vector<String>();
                        for (int i = 1; i <= colCount; i++) {
                            rowdata.add(rs.getString(i));
                        }
                        data.add(rowdata);
                    }
                    if (rowCount == 0) {
                        model.setDataVector(null, title);
                    } else {
                        model.setDataVector(data, title);
                        
                    }
        
        
                } catch (Exception ee) {
                    System.out.println(ee.toString());
                    JOptionPane.showMessageDialog(JF, "【系统异常!】", "失败",0);
                } finally {
                    db.closeAll();
                }
            }
        });

新建功能的代码该怎么完成?

btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
//                int row = main.table.getSelectedRow();
////                String oid = (String)dtm.getValueAt(row, 0);
//                String name = (String)namet.getValueAt(row, 1);
//                String phone = (String)dtm.getValueAt(row, 2);
//                String sex = (String)dtm.getValueAt(row, 3);
//                String age = (String)dtm.getValueAt(row, 3);
//                String history = (String)dtm.getValueAt(row, 3);
//                String str = sd.Insert(oid, oname, ocourse, odegree);
//                JOptionPane.showMessageDialog(null, str);

            }
        });
  • 写回答

1条回答 默认 最新

  • @Erqi 2021-08-03 16:44
    关注

    已经完成了

        public void actionPerformed(ActionEvent e) {
                    DUBtil db=new DUBtil();
                    Connection conn=null;
                    try {
                        conn=db.getConnection();
                        Statement stmt=conn.createStatement();
                        String name = namet.getText() ;
                        System.out.print(name);
                        String phone =phonet.getText();
                        System.out.print(phone);
                        String sex="男";
                        System.out.print(sex);                
                        String age =aget.getText() ;
                        System.out.print(age);
                        String history =historyt.getText();
                        System.out.print(history);
                        String pricet =price.getText();
                        System.out.print(pricet);
                        LocalDate date = LocalDate.now();
                        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");  
                        System.out.println(date.format(formatter)); 
                        String time=date.format(formatter);
                        
                        String str="insert into vip(date, name, phone, sex, age, price, history ) values ('"+time+"','"+name+"','"+phone+"','"+sex+"','"+age+"','"+pricet+"','"+history+"')";
                      
                        db.executeUpdate(str,null);
                        JOptionPane.showMessageDialog(JF2, "【添加成功!!!】", "成功",1);
                        
                    }
                     catch (Exception ee) {
                        JOptionPane.showMessageDialog(JF2, "【系统异常!】", "失败",0);
                    } finally {
                        db.closeAll();
                        dispose();
                    }
                    
                }
    
    
    评论

报告相同问题?

问题事件

  • 系统已结题 8月10日
  • 修改了问题 8月2日
  • 创建了问题 8月2日

悬赏问题

  • ¥66 换电脑后应用程序报错
  • ¥50 array数据同步问题
  • ¥15 pic16F877a单片机的外部触发中断程序仿真失效
  • ¥15 Matlab插值拟合差分微分规划图论
  • ¥15 keil5 target not created
  • ¥15 C/C++数据与算法请教
  • ¥15 怎么找志同道合的伙伴
  • ¥20 如何让程序ab.eXe自已删除干净硬盘里的本文件自己的ab.eXe文件
  • ¥50 爬虫预算充足,跪巨佬
  • ¥15 滑块验证码拖动问题悬赏