【问题标题】:JTextField Limit CharacterJTextField 限制字符
【发布时间】:2015-04-11 07:45:53
【问题描述】:

我在连接到 MySQL 数据库的 JFrame 上有一个名为 Reservation ID 的 JTextField。 每当我在数据库中添加新乘客时,在我在其他文本字段中输入乘客姓名和其他详细信息之前,JTextField 应该根据自动增量在 JTextField 中为我自动生成一个新的预订 ID。

我的代码:

    resId = new JTextField();
    try{
        rs = stt.executeQuery("select ReservID as last_id from passenger");
        int lastid = rs.getInt("last_id");
        lastid++;
        resId.setText(String.valueOf(last_id));

    }catch(Exception e){
        JOptionPane.showMessageDialog(null, "cannot retrieve");
    }
    resId.setBounds(432, 178, 126, 22);
    frame.getContentPane().add(resId);
    resId.setColumns(10);

我的列 RevervID 的最后一行的值为 003。当我运行表单时,它应该在 Reservation ID 文本字段中显示我 004 我该如何实现?请帮忙.. 谢谢

下面图片链接中的texfield http://i.imgbox.com/wcf9KicU.png

【问题讨论】:

  • 从数据库中获取最后一个预订 id。然后将其显示在您的 Jtexfield 上,但在将其设置为文本字段之前先增加 id。
  • 如何实现?
  • 假设用户的 ID 是不可编辑的,我可能不想显示它,特别是如果你对表格使用某种自动键控机制,但这只是我

标签: java mysql swing jtextfield


【解决方案1】:

先用数据库查询找出最大id->Select max(id) from mytable

将最大id存储在一个变量中-> int max_id= max_id_from_database;

max_id 增加一-> max_id++;

将 max_id 值添加到您的 JTextField-> myJTextField.setText("" + max_id);

已编辑:根据您的代码(只需在 ReservID 之前添加 max 并在 if 条件中添加 rs.next() 并且 ReservID 在您的数据库中必须是整数)。

 resId = new JTextField();
    try{
        rs = stt.executeQuery("select max(ReservID) as last_id from passenger");
        if(rs.next()){
        int lastid = rs.getInt("last_id");
        lastid++;
        resId.setText(String.valueOf(last_id));
        }

    }catch(Exception e){
        JOptionPane.showMessageDialog(null, "cannot retrieve");
    }

【讨论】:

    猜你喜欢
    • 2012-05-22
    • 2011-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-29
    • 1970-01-01
    • 2014-10-14
    • 2014-01-25
    相关资源
    最近更新 更多