2017-03-07 303 views
0

我想在JCombobx中顯示數據庫數據,我試過這個,但是我得到一個錯誤,如java.sql.SQLNonTransientConnectionException:發生連接認證失敗。原因:Userid或密碼無效。如何解決這裏,這是我的代碼如何在java中添加用戶名和密碼db(derby)

/* 
    * To change this license header, choose License Headers in Project Properties. 
    * To change this template file, choose Tools | Templates 
    * and open the template in the editor. 
    */ 
    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<String> jcb; 
    Statement st; 
    ResultSet rs; 
    Connection con; 

    public Satsecond() { 

     super.setTitle("Satellite Page"); 

    jlb = new JLabel();  
    jcb = new JComboBox<String>(); 
     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{ 
     Class.forName("org.apache.derby.jdbc.ClientDriver"); 
     con = DriverManager.getConnection("jdbc:derby://localhost:1527/contact"); 
     con.setSchema("APP"); 
     st = con.createStatement(); 
     String s = "SELECT * FROM APP.SATELLITE"; 
     rs = st.executeQuery(s); 
     while(rs.next()) 
     { 
      String n = rs.getString("NAME"); 
      jcb.addItem(rs.getString("n")); 
     } 

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


     st.close(); 
     rs.close(); 
     con.close(); 
     } 
     catch(Exception e) 
     { 
      JOptionPane.showMessageDialog(null,"ERROR_CLOSE"); 
      e.printStackTrace(); 
     } 
    } 
     //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.logging.Level.SEVERE, null, ex); 
     } 
     //</editor-fold> 

     /* Create and display the form */ 

       new Satsecond(); 
      } 

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

回答

0

它們作爲參數傳入如下:

DriverManager.getConnection("jdbc:derby://localhost:1527/contact", [username], [password]); 
相關問題