简体   繁体   中英

how to add username and password in java db (derby)

I want to show database Data in JCombobx and I tried this but I get an error like java.sql.SQLNonTransientConnectionException: Connection authentication failure occurred. Reason: Userid or password invalid. how to solve this here is my code

    /*
     * 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                   
    }

它们作为参数传递如下:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM