我正在为MySQL构建一个GUI,JFrame与jdbc合作。 我在Java方面不是那么好,我无法理解以下内容。
我想用我的查询中的数据填充JTable,但我希望此查询包含用户参数。所以..
以下是查询:
String sql = "SELECT `field1`, `field2`, `field3`"
+ "FROM `Table1` INNER JOIN `Table2` on `table1PK` = `table2PK`"
+ "WHERE `column1Value` = '"+JTextfield1.getText()+'" AND"
+ " `column2Value` = '"+JTextfield2.getText()+"'";
然后,使用此字符串的输出,在按下JButton之后,我想填充一个JTable。
换句话说,我想要"输出" JTable是" UserParameterDefined" (通过JTextFields)
以下是工作自动填充JTable的漏洞代码,但没有用户自定义参数,我有完美的工作:
package pkginterface;
import java.awt.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import java.time.*;
import java.sql.Date;
import java.text.*;
public class Movies_Info extends JFrame
{
public Movies_Info()
{
ArrayList columnNames = new ArrayList();
ArrayList data = new ArrayList();
// Connect to an MySQL Database, run query, get result set
String url = "jdbc:mysql://localhost:3306/cinema";
String userid = "root";
String password = "root";
String sql = "SELECT movie_title, timetable_starttime, timetable_movietype "
+ "FROM Movies INNER JOIN TimeTable on timetable_movie_ID = movie_ID";
try (Connection connection = DriverManager.getConnection( url, userid, password );
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery( sql ))
{
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
// Get column names
for (int i = 1; i <= columns; i++)
{
columnNames.add( md.getColumnName(i) );
}
// Get row data
while (rs.next())
{
ArrayList row = new ArrayList(columns);
for (int i = 1; i <= columns; i++)
{
row.add( rs.getObject(i) );
}
data.add( row );
}
}
catch (SQLException e)
{
System.out.println( e.getMessage() );
}
Vector columnNamesVector = new Vector();
Vector dataVector = new Vector();
for (int i = 0; i < data.size(); i++)
{
ArrayList subArray = (ArrayList)data.get(i);
Vector subVector = new Vector();
for (int j = 0; j < subArray.size(); j++)
{
subVector.add(subArray.get(j));
}
dataVector.add(subVector);
}
for (int i = 0; i < columnNames.size(); i++ )
columnNamesVector.add(columnNames.get(i));
// Create table with database data
JTable table = new JTable(dataVector, columnNamesVector)
{
public Class getColumnClass(int column)
{
setTitle("Staff Interface - Movies Info");
for (int row = 0; row < getRowCount(); row++)
{
Object o = getValueAt(row, column);
if (o != null)
{
return o.getClass();
}
}
return Object.class;
}
};
JScrollPane scrollPane = new JScrollPane( table );
getContentPane().add( scrollPane );
JPanel buttonPanel = new JPanel();
getContentPane().add( buttonPanel, BorderLayout.SOUTH );
TableCellRenderer tableCellRenderer = new DefaultTableCellRenderer() {
// Time shown correction
SimpleDateFormat f = new SimpleDateFormat("HH:mm:ss");
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
if( value instanceof Time) {
value = f.format(value);
}
return super.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, column);
}
};
table.setDefaultRenderer(Time.class, tableCellRenderer);
// Time shown correction finish
pack();
}
public static void main(String[] args)
{
Movies_Info frame = new Movies_Info();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setVisible(true);
}
}
提前感谢您的任何帮助!
答案 0 :(得分:2)
基本概念看起来像......
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JTextField field;
private JTable table;
public TestPane() {
setLayout(new BorderLayout());
field = new JTextField(20);
table = new JTable();
add(field, BorderLayout.NORTH);
add(new JScrollPane(table));
JButton update = new JButton("Update");
add(update, BorderLayout.SOUTH);
update.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
TableModel model = executeQueryWith(field.getText());
table.setModel(model);
} catch (SQLException ex) {
JOptionPane.showMessageDialog(TestPane.this, "Failed to execute query", "Error", JOptionPane.ERROR_MESSAGE);
ex.printStackTrace();
}
}
});
}
protected TableModel executeQueryWith(String value) throws SQLException {
String url = "jdbc:mysql://localhost:3306/cinema";
String userid = "root";
String password = "root";
String sql = "SELECT movie_title, timetable_starttime, timetable_movietype "
+ "FROM Movies INNER JOIN TimeTable on timetable_movie_ID = ?";
DefaultTableModel model = new DefaultTableModel();
try (Connection connection = DriverManager.getConnection(url, userid, password)) {
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
stmt.setString(1, value);
try (ResultSet rs = stmt.executeQuery()) {
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
// Get column names
for (int i = 1; i <= columns; i++) {
model.addColumn(md.getColumnName(i));
}
// Get row data
while (rs.next()) {
Vector<Object> row = new Vector(columns);
for (int i = 1; i <= columns; i++) {
row.add(rs.getObject(i));
}
model.addRow(row);
}
}
}
}
return model;
}
}
}
您可能还想看一下: