如何将数据库中的数据显示到 Java Swing Controls 的 JTable
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6382877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to show data from database to JTable of Java Swing Controls
提问by iamanapprentice
I have this problem on how to show data from my database using JTable of Java Swing Controls. I just drag and drop the table from the palette in the JForm.
关于如何使用 Java Swing Controls 的 JTable 显示数据库中的数据,我遇到了这个问题。我只是从 JForm 的调色板中拖放表格。
I have no Idea how to code it.
我不知道如何编码。
回答by arshabh
you need to connect your java program to your database using a JDBC. after that you need to extract the data using ResultSet class. you need to understand the basics of JDBC and bey clear about the commands of your database. You also need to know how to work with databases through java. so read the online tutorial of java by oracle(official tutorial).it will not take more than a hour. life will be easier
您需要使用 JDBC 将 Java 程序连接到数据库。之后,您需要使用 ResultSet 类提取数据。您需要了解 JDBC 的基础知识,并清楚了解数据库的命令。您还需要知道如何通过 java 使用数据库。所以阅读oracle的在线java教程(官方教程)。不会超过一个小时。生活会更轻松
回答by Ashutosh Adarsh
Use this code :
使用此代码:
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class DisplayEmpData extends JFrame implements ActionListener {
JFrame frame1;
JLabel l0, l1, l2;
JComboBox c1;
JButton b1;
Connection con;
ResultSet rs, rs1;
Statement st, st1;
PreparedStatement pst;
String ids;
static JTable table;
String[] columnNames = {"User name", "Email", "Password", "Country"};
String from;
DisplayEmpData() {
l0 = new JLabel("Fatching Employee Information");
l0.setForeground(Color.red);
l0.setFont(new Font("Serif", Font.BOLD, 20));
l1 = new JLabel("Select name");
b1 = new JButton("submit");
l0.setBounds(100, 50, 350, 40);
l1.setBounds(75, 110, 75, 20);
b1.setBounds(150, 150, 150, 20);
b1.addActionListener(this);
setTitle("Fetching Student Info From DataBase");
setLayout(null);
setVisible(true);
setSize(500, 500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
add(l0);
add(l1);;
add(b1);
try {
Class.forName("com.mysql.jdbc.Driver"); // (1)
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_name", "username", "password"); // (2)
st = con.createStatement();
rs = st.executeQuery("select uname from emp");
Vector v = new Vector();
while (rs.next()) {
ids = rs.getString(1);
v.add(ids);
}
c1 = new JComboBox(v);
c1.setBounds(150, 110, 150, 20);
add(c1);
st.close();
rs.close();
} catch (Exception e) {
}
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == b1) {
showTableData();
}
}
public void showTableData() {
frame1 = new JFrame("Database Search Result");
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setLayout(new BorderLayout());
//TableModel tm = new TableModel();
DefaultTableModel model = new DefaultTableModel();
model.setColumnIdentifiers(columnNames);
//DefaultTableModel model = new DefaultTableModel(tm.getData1(), tm.getColumnNames());
//table = new JTable(model);
table = new JTable();
table.setModel(model);
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
table.setFillsViewportHeight(true);
JScrollPane scroll = new JScrollPane(table);
scroll.setHorizontalScrollBarPolicy(
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroll.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
from = (String) c1.getSelectedItem();
//String textvalue = textbox.getText();
String uname = "";
String email = "";
String pass = "";
String cou = "";
try {
pst = con.prepareStatement("select * from emp where UNAME='" + from + "'");
ResultSet rs = pst.executeQuery();
int i = 0;
if (rs.next()) {
uname = rs.getString("uname");
email = rs.getString("umail");
pass = rs.getString("upass");
cou = rs.getString("ucountry");
model.addRow(new Object[]{uname, email, pass, cou});
i++;
}
if (i < 1) {
JOptionPane.showMessageDialog(null, "No Record Found", "Error", JOptionPane.ERROR_MESSAGE);
}
if (i == 1) {
System.out.println(i + " Record Found");
} else {
System.out.println(i + " Records Found");
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
frame1.add(scroll);
frame1.setVisible(true);
frame1.setSize(400, 300);
}
public static void main(String args[]) {
new DisplayEmpData();
}
}
EDIT: The above code use mysql as an example. Change the line marked with (1) and (2) if you are not using mysql.
编辑:上面的代码以 mysql 为例。如果您不使用 mysql,请更改标有 (1) 和 (2) 的行。