简体   繁体   中英

How to make id with text auto increment in java?

I want to select data from mysql and input into JComboBox as an auto increment.

   DefaultComboBoxModel mo = new DefaultComboBoxModel();
   try {
       rs = stmt.executeQuery("select pid from tbpatient");
       while(rs.next()) {
           mo.addElement(rs.getString(1));
       }
   } catch(SQLException e) {
       e.printStackTrace();
   }
   jComboBox1.setModel(mo);

}

But I want my text show "P001" and auto increment.

What you could do is store the key as two columns. A char prefix and an auto-incrementing int, both of which are grouped for the primary key.

CREATE TABLE myItems (
    id INT NOT NULL AUTO_INCREMENT,
    prefix CHAR(30) NOT NULL,
    PRIMARY KEY (id, prefix),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM