如何从数据库中检索数据到jcombobox?

时间:2017-03-06 10:53:46

标签: java database swing jcombobox

我想从数据库中检索数据并在JComboBox中显示。我正在使用Java DB(德比)。我将数据库与我的项目连接起来,但是我收到一条错误消息,说我的文件使用了未经检查或不安全的操作。

package javafxapplication3;
import java.awt.Cursor;
import java.awt.Font; 
import java.awt.Image;
import javafx.scene.paint.Color;
import javax.swing.*;
import java.sql.*;

/**
*
* 
*/
public class Satsecond extends JFrame {

/**
 * Creates new form Satsecond
 */
private JLabel jlb;
private JComboBox jcb;
Statement st;
ResultSet rs;
Connection con;

public Satsecond() {

    super.setTitle("Satellite Page");

    jlb = new JLabel();    
    jcb = new JComboBox();
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().setLayout(null);

    setVisible(true);
    getContentPane().add(jcb);
    jcb.setBounds(300,150,150,60);
    jcb.setBackground(java.awt.Color.WHITE);
    jcb.setForeground(java.awt.Color.BLACK);

    jcb.setCursor(new Cursor(Cursor.HAND_CURSOR));
   // jcb.setRolloverEnabled(true);

   try{

    con =   DriverManager.getConnection("jdbc:derby://localhost:1527/contact");
    st = con.createStatement();
    String s = "SELECT NAME FROM SATELLITE";
   rs = st.executeQuery(s);
   while(rs.next())
   {
       jcb.addItem(rs.getString(1));
   }

}   
catch(Exception e){
    JOptionPane.showMessageDialog(null,"ERROR_CLOSE");
}finally{
    try{


    st.close();
    rs.close();
    con.close();
    }
    catch(Exception e)
    {
        JOptionPane.showMessageDialog(null,"ERROR_CLOSE");
    }
}
    //getContentPane().add(jlb);
    jlb.setBounds(0,0,1350,750);
     ImageIcon icon = new  ImageIcon("C:\\Users\\xyz\\Desktop\\photos\\mysat.jpg");
    Image img = icon.getImage();
    Image newimg = img.getScaledInstance(jlb.getWidth(), jlb.getHeight(),  Image.SCALE_SMOOTH);
    ImageIcon newimc = new ImageIcon(newimg);
    jlb.setIcon(newimc);
   add(jlb);



}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
/*
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    getContentPane().setLayout(null);

    pack();
}// </editor-fold>                        
*/
/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* 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(Satsecond.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Satsecond.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Satsecond.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
           java.util.logging.Logger.getLogger(Satsecond.class.getName()).log(java.util.logg  ing.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */

            new Satsecond();
         }

 // Variables declaration - do not modify                     
// End of variables declaration                   
}

0 个答案:

没有答案