Java JTextField限制字符
我在连接到MySQL数据库的JFrame上有一个以预订ID命名的JTextField。 每当我在数据库中添加新乘客时,在我在其他文本字段中输入乘客姓名和其他详细信息之前,JTextField应根据自动增量在JTextField中自动为我生成一个新的预订ID 我的代码:Java JTextField限制字符,java,mysql,swing,jtextfield,Java,Mysql,Swing,Jtextfield,我在连接到MySQL数据库的JFrame上有一个以预订ID命名的JTextField。 每当我在数据库中添加新乘客时,在我在其他文本字段中输入乘客姓名和其他详细信息之前,JTextField应根据自动增量在JTextField中自动为我生成一个新的预订ID 我的代码: resId = new JTextField(); try{ rs = stt.executeQuery("select ReservID as last_id from passenger");
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。当我运行表单时,它应该在预订ID文本字段中显示我004,我如何实现这一点?请帮忙。。
谢谢
下面图像链接中的texfield
首先使用数据库查询找出最大id->
从mytable中选择最大id
将最大id存储在变量->数据库中的int max\u id=max\u id\u中代码>
将max_id
增加一->max_id++代码>
将max_id值添加到JTextField->myJTextField.setText(“+max_id”)代码>
编辑:根据您的代码(只需在reserved之前添加max,并在if条件中添加rs.next(),reserved必须是数据库中的整数)
从数据库中获取最后一个预订id。。然后将其显示在Jtexfield上,但在将其设置为textfield之前先增加id。如何实现这一点?假设该id不可编辑用户,我可能会尝试不显示它,特别是如果您对表使用某种自动键控机制,但这只是我
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");
}