gpt4 book ai didi

java - 如何在java中动态连接mysql数据库并显示所选数据库的表、属性?

转载 作者:行者123 更新时间:2023-11-29 18:37:48 25 4
gpt4 key购买 nike

我使用的是 mysql 5.7,我创建了一些数据库。我在 netbeans ide 8.2 中使用下拉菜单 [JComboBox] 显示它们

数据库

+--------------------
| classicmodels
| db1
| db2

以下内容为必填内容

当选择 db1(可以选择任何显示值)时,我想与其建立连接。然后在窗口中显示表格以及窗口中每个表格下的属性/列。

数据库.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
}

MySqlConnect.java

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;
}
}

}

最佳答案

public static Connection ConnectDB() 传递参数如public static Connection ConnectDB(String db)

然后

Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+db, "root", "system");

使用有效的数据库名称在构造函数中调用 Connection 类。例如:conn = MySqlConnect.ConnectDB(classicmodels);

然后是jComboBox1ActionPerformed事件再次使用连接作为 conn = MySqlConnect.ConnectDB(jComboBox1.getSelectedItem().toString);

然后您可以使用选定的数据库进行表填充。

关于java - 如何在java中动态连接mysql数据库并显示所选数据库的表、属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45142081/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com