简体   繁体   中英

swing retrieve data from mysql db to textfield

i have column in mysql table have 100 record ,i want show values from table inside textfield ( every 3 second show record from 0 - 99). this is my code :

Connection conn = null;
    Statement st = null;
    ResultSet rs = null;
    String dbUrl = "jdbc:mysql://localhost:3306/jointdb";
    String dbUsr = "root";
    String dbPass = "a12345";
    try{
    String sql= "select expert1 from eridb";
    Class.forName("com.mysql.jdbc.Driver");
    conn = DriverManager.getConnection (dbUrl,dbUsr,dbPass);
    st = conn.createStatement();
    rs = st.executeQuery(sql);
   // textField1.setText("enter text here");
    while(rs.next()){
        //Get values
        String value = rs.getString("expert1");
        textField1.setText(value);        
    }

}catch(Exception e){
    e.printStackTrace();
}finally{
    try {
        rs.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    try {
        st.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    try {
        conn.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

Now, i want show value of record every 3 second from index 0-99 record

Note: Data come to database every 3 second thanks

Use Thread.sleep(milliseconds)

while(rs.next()){
    String value = rs.getString("expert1");
    textField1.setText(value);        
    try {
          Thread.sleep(3000);
    } catch(Exception e) {}
}

You can use Thread for parallel process.

Please read How To Use Swing Timer and The Event Dispatch Thread .

Use then javax.swing.Timer to re-read the data from database and set the content on the JTextField the way you need it.

Timer timer = new Timer(3000, new ActionListener() {

    @Override
     public void actionPerformed(ActionEvent e) {
         .... // retrieve data and prepare the textField content
         textField.setContent(...);
     }          
});

timer.start();

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