我想在JCombobx中显示数据库数据,我试过这个,但是我收到了像java.sql.SQLNonTransientConnectionException这样的错误:发生了连接验证失败。原因:用户标识或密码无效。这里如何解决这个问题是我的代码
/*
* 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 :(得分:0)
它们作为参数传递如下:
DriverManager.getConnection("jdbc:derby://localhost:1527/contact", [username], [password]);