你好我是一個非常新手在Java中,我試圖寫一個非常簡單的練習,我創建了一個Jclass從DB讀取參數,並將其保存在一個變量「estado」和一個JFrame來顯示這個變量在DB中隨機變化的值。更新JTextFields文本變量的每一個變化
該程序讀出的MySQL數據庫並存儲在像這樣的類的Java變量所需要的數據:
package Paquete_domotica;
import java.io.*;
import java.sql.*;
public class domotica {
public static int estado;
boolean loop = true;
public domotica() throws IOException
{
while(loop)
{
try
{
DriverManager.registerDriver(new org.gjt.mm.mysql.Driver());
Connection conexion = DriverManager.getConnection (
"jdbc:mysql://localhost/XXX","XXXX", "XXXX");
Statement s = conexion.createStatement();
ResultSet rs = s.executeQuery ("select id, nombre, valor from data");
while (rs.next())
{
if (rs.getInt ("id") == 20)
{
estado = rs.getInt ("valor");
}
}
rs.close();
conexion.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
存儲變量被稱爲「國家體制」,這些變量是1或0,並且我正嘗試這些變量的每一個變化使jTextField1的價值在下面的JFrame的變化:
package Paquete_domotica;
import java.awt.event.ActionListener;
import java.io.IOException;
public class JFramedomotica extends javax.swing.JFrame {
int numeroRecibido;
public JFramedomotica() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jTextField1 = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextField1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
jTextField1.setText("SIN DATOS");
jTextField1.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
jTextField1.setEnabled(false);
jTextField1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jTextField1MouseClicked(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(110, 110, 110)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(154, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(138, 138, 138)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(142, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jTextField1MouseClicked(java.awt.event.MouseEvent evt) {
jTextField1.setText(String.valueOf(domotica.estado));
}
public static void main(String args[]) throws IOException {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(JFramedomotica.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(JFramedomotica.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(JFramedomotica.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(JFramedomotica.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JFramedomotica().setVisible(true);
}
});
new domotica();
}
// Variables declaration - do not modify
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
正如你可以看到現在我可以
更新jTextField1 0但有了這段代碼,我不得不點擊鼠標來刷新jTextField1,我不知道如何在每次更改「estado」時更新jTextField1。
感謝您的建議,非常簡單和有用的代碼,setEstado需要一些改變,但我正在研究它,我擔心持續的開啓/關閉連接,你認爲這將是未來的問題嗎?尼克霍爾特(我也感謝他)認爲這將是。 – user3190719
我認爲你應該有一個連接池,不要一直打開和關閉連接。我會推薦與swingWorker一起使用這個解決方案,因爲它更容易,而且它的設計特別適合擺動! – nachokk
現在我正在嘗試學習如何構建池連接,但我一直無法找到一個簡單的示例作爲最後一個解決方案。 – user3190719