[英]swing retrieve data from mysql db to textfield
我在mysql表中的列有100條記錄,我想從textfield中的表中顯示值(每3秒顯示記錄,從0-99)。 這是我的代碼:
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();
}
}
現在,我想從索引0-99記錄每3秒顯示一次記錄的值
注意:數據每3秒進入數據庫一次,謝謝
使用Thread.sleep(毫秒)
while(rs.next()){
String value = rs.getString("expert1");
textField1.setText(value);
try {
Thread.sleep(3000);
} catch(Exception e) {}
}
您可以使用線程進行並行處理。
請閱讀如何使用Swing計時器和事件調度線程 。
然后使用javax.swing.Timer從數據庫中重新讀取數據,並按照需要的方式在JTextField上設置內容。
Timer timer = new Timer(3000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
.... // retrieve data and prepare the textField content
textField.setContent(...);
}
});
timer.start();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.