import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
public class TableFromDatabase extends JFrame {
private static final long serialVersionUID = 1L;
private JTable tbl;
private JButton btn;
public TableFromDatabase() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new BorderLayout());
setTitle("Table from database");
tbl = new JTable();
getContentPane().add(new JScrollPane(tbl), BorderLayout.CENTER);
btn = new JButton("Read from database");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
tbl.setModel(loadQuery("com.mysql.jdbc.Driver", "jdbc:
mysql://localhost/Test", "root", "", "SELECT f1,f2 FROM t1"));
}
});
getContentPane().add(btn, BorderLayout.SOUTH);
pack();
}
TableModel loadQuery(String driver, String conurl, String un, String pw, String sql) {
GeneralDatabaseModel res = new GeneralDatabaseModel();
try {
Class.forName(driver);
Connection con = DriverManager.getConnection(conurl, un, pw);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
ResultSetMetaData rsmd = rs.getMetaData();
for(int colix = 1; colix <= rsmd.getColumnCount(); colix++) {
res.addColumn(rsmd.getColumnName(colix));
}
while(rs.next()) {
List<Object> row = new ArrayList<Object>();
for(int colix = 1; colix <= rsmd.getColumnCount(); colix++) {
row.add(rs.getObject(colix));
}
res.addRow(row);
}
rs.close();
stmt.close();
con.close();
return res;
} catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
return null;
} catch (SQLException e) {
JOptionPane.showMessageDialog(this, e.getMessage());
return null;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame f = new TableFromDatabase();
f.setVisible(true);
}
});
}
}
class GeneralDatabaseModel extends AbstractTableModel {
private static final long serialVersionUID = 1L;
private List<String> colnams;
private List<List<Object>> data;
public GeneralDatabaseModel() {
colnams = new ArrayList<String>();
data = new ArrayList<List<Object>>();
}
public void addColumn(String colnam) {
colnams.add(colnam);
}
public void addRow(List<Object> row) {
data.add(row);
}
@Override
public int getColumnCount() {
return colnams.size();
}
@Override
public int getRowCount() {
return data.size();
}
public String getColumnName(int colix) {
return colnams.get(colix);
}
@Override
public Object getValueAt(int rowix, int colix) {
return data.get(rowix).get(colix);
}
}