如何在java中动态连接到mysql数据库,并显示所选数据库中的表和属性?
我使用的是mysql 5.7,我创建了一些数据库。 我使用netbeans ide 8.2中的下拉菜单[如何在java中动态连接到mysql数据库,并显示所选数据库中的表和属性?,java,mysql,swing,jdbc,netbeans-8,Java,Mysql,Swing,Jdbc,Netbeans 8,我使用的是mysql 5.7,我创建了一些数据库。 我使用netbeans ide 8.2中的下拉菜单[JComboBox]显示了它们 数据库 +-------------------- | classicmodels | db1 | db2 以下是必需的 选择db1(可以选择任何显示的值)时,我希望获得到它的连接。 然后在窗口中显示表格,并在窗口中显示每个表格下的属性/列。 Database.java import java.sql.*; import java.util.ArrayList
JComboBox
]显示了它们
数据库
+--------------------
| classicmodels
| db1
| db2
以下是必需的
选择db1(可以选择任何显示的值)时,我希望获得到它的连接。
然后在窗口中显示表格,并在窗口中显示每个表格下的属性/列。
Database.java
import java.sql.*;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
public class Database extends javax.swing.JFrame {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
/**
* Creates new form Database
*/
public Database() {
initComponents();
java.util.ArrayList<String> tables = new ArrayList<>();
conn = MySqlConnect.ConnectDB();
String sql = "show databases;";
try {
stmt = conn.createStatement();
stmt.execute(sql);
rs = stmt.getResultSet();
while (rs.next()) {
jComboBox1.addItem(rs.getString("Database"));
}
} catch (SQLException ex) {
Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
}
if (null != selection) {
selection.setVisible(false);
}
}
/**
* 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() {
jMenuItem1 = new javax.swing.JMenuItem();
jMenuItem2 = new javax.swing.JMenuItem();
jMenuItem3 = new javax.swing.JMenuItem();
jComboBox1 = new javax.swing.JComboBox<>();
jMenuItem1.setText("jMenuItem1");
jMenuItem2.setText("jMenuItem2");
jMenuItem3.setText("jMenuItem3");
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Available DataSources");
setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
setForeground(new java.awt.Color(51, 0, 255));
jComboBox1.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
jComboBox1ItemStateChanged(evt);
}
});
jComboBox1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox1ActionPerformed(evt);
}
});
jComboBox1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
jComboBox1KeyPressed(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(159, 159, 159)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(213, Short.MAX_VALUE)));
layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup().addGap(92, 92, 92)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(401, Short.MAX_VALUE)));
setBounds(0, 0, 416, 552);
}// </editor-fold>
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
selection = new QueryPredictor(jComboBox1.getSelectedItem().toString());
selection.setVisible(true);
}
private void jComboBox1KeyPressed(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
}
private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {
// TODO add your handling code here:
}
/**
* @param args
* the command line arguments
*/
public static void main(String args[]) {
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(Database.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Database.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Database.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Database.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 Database().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JComboBox<String> jComboBox1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem3;
// End of variables declaration
}
将
公共静态连接ConnectDB()的参数作为公共静态连接ConnectDB(字符串db)
然后
使用有效的db名称在构造函数中调用连接类。
例如:conn=MySqlConnect.ConnectDB(classicmodels)代码>
然后,对于执行的jcombox1action
事件,再次使用连接作为conn=MySqlConnect.ConnectDB(jcombox1.getSelectedItem().toString)代码>
然后,可以使用选定的数据库填充表
import java.sql.*;
import javax.swing.JOptionPane;
public class MySqlConnect {
Connection conn = null;
public static Connection ConnectDB() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/classicmodels", "root", "system");
//"jdbc:mysql://localhost:3306/db1","root","system");
//"jdbc:mysql://localhost:3306/db2","root","system");
return conn;
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
return null;
}
}
}
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+db, "root", "system");