有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java swing在while循环中等待3秒

我让程序从mysql数据库中获取数据,并在文本字段中显示

我想每3秒钟播放一张唱片

这是我的密码

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 * 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()){
        String value = rs.getString("id");
        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();
    }
} 

我用过睡眠,但程序挂起了(没有好处)

请帮助我 谢谢你


共 (1) 个答案

  1. # 1 楼答案

    如果你想在Swing中做一些需要时间的背景工作,你应该使用^{}Oracle's tutorial,虽然不是很有用……)

    为了安排任务,你可以这样做,就像@Andrew Thompson这次用javax.swing.Timer(有点better tutorial from Oracle)说的那样