如何在 JTable 中显示来自 MySQL 的数据
How display in JTable the data from MySQL
我想在 JTable 中显示来自 MySQL 的数据,但只显示了来自 table 的最后一行,仅此而已。请帮帮我。我知道我遇到了问题,因为 jt = new JTable(data, columns) 每次都会为每一行创建一个新的 table (删除前一行),但我找不到正确的选项。
public class Test2 extends JPanel {
static final String USERNAME = "root";
static final String PASSWORD = "root";
static final String CONN_STRING = "jdbc:mysql://localhost:3306/mydbtest?useSSL=false";
JTable jt;
public Test2 () {
try {
Connection conn;
conn = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);
Statement stmt = (Statement) conn.createStatement();
String query = "Select title, season, episode from movie";
ResultSet rs = stmt.executeQuery(query);
rs.beforeFirst();
while (rs.next()) {
String title = rs.getString("Title");
String season = rs.getString("Season");
String episode = rs.getString("Episode");
String[] columns = {"Title", "S", "E"};
String[][] data = {{title, season, episode}};
jt = new JTable(data, columns);
};
jt.setPreferredScrollableViewportSize(new Dimension(450, 63));
jt.setFillsViewportHeight(true);
JScrollPane jps = new JScrollPane(jt);
add(jps);
}
catch (Exception er) {System.err.println(er);}
}
public static void main(String[] args) {
JFrame jf = new JFrame();
Test2 t = new Test2();
jf.setTitle("Test");
jf.setSize(500,500);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(t);
}
}
您的问题在这里:
while (rs.next()) {
String title = rs.getString("Title");
String season = rs.getString("Season");
String episode = rs.getString("Episode");
String[] columns = { "Title", "S", "E" };
String[][] data = { { title, season, episode } };
jt = new JTable(data, columns); // *** you're making many JTables here!! ***
}
每次 while 循环循环时,您都会创建 并 丢弃 一个新的 JTable 对象。异常发生在最后一次循环时,结果集最后一行的数据没有被丢弃,然后显示在最终创建的 JTable 中。为了解决这个问题,要在JTable中显示所有的数据,你需要在while循环中整理你所有的结果集数据,然后将它添加到JTable中,最简单的这样做的方法是创建一个 table 模型,这里是一个简单的 DefaultTableModel,before while 循环,并将其填充到 while 循环内的每一行结果集数据中:
// create a table model with the appropriate column headers
// and with 0 rows (to start with)
String[] columnNames = {"Title", "Season", "Episode"};
DefaultTableModel tableModel = new DefaultTableModel(columnNames, 0);
while (rs.next()) {
String title = rs.getString("Title");
String season = rs.getString("Season");
String episode = rs.getString("Episode");
// create a single array of one row's worth of data
String[] data = { title, season, episode } ;
// and add this row of data into the table model
tableModel.addRow(data);
}
jt.setModel(tableModel); // place model into JTable
或者更好,将最后一行更改为:
jt = new JTable(tableModel); // to create a new JTable
试试这个……你可以轻松做到……
public final void EmployeeGridView(){
try{
Connection conn = DBConn.connect();
PreparedStatement ps = conn.prepareStatement("Select * from empdetails");
ResultSet rs=ps.executeQuery();
DefaultTableModel tm = (DefaultTableModel)jTable1.getModel();
tm.setRowCount(0);
while(rs.next()){
Object o[] = {rs.getInt("EMPID"),rs.getString("FirstName"),rs.getString("LastName"),rs.getString("Designation"),rs.getString("NIC"),rs.getString("PhoneNO"),rs.getString("DOB"),rs.getString("Address"),rs.getString("Gender")};
tm.addRow(o);
}
}
catch(Exception e){
JOptionPane.showMessageDialog(null,"Error in Employee Grid View..... "+e);
}
}
我想在 JTable 中显示来自 MySQL 的数据,但只显示了来自 table 的最后一行,仅此而已。请帮帮我。我知道我遇到了问题,因为 jt = new JTable(data, columns) 每次都会为每一行创建一个新的 table (删除前一行),但我找不到正确的选项。
public class Test2 extends JPanel {
static final String USERNAME = "root";
static final String PASSWORD = "root";
static final String CONN_STRING = "jdbc:mysql://localhost:3306/mydbtest?useSSL=false";
JTable jt;
public Test2 () {
try {
Connection conn;
conn = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);
Statement stmt = (Statement) conn.createStatement();
String query = "Select title, season, episode from movie";
ResultSet rs = stmt.executeQuery(query);
rs.beforeFirst();
while (rs.next()) {
String title = rs.getString("Title");
String season = rs.getString("Season");
String episode = rs.getString("Episode");
String[] columns = {"Title", "S", "E"};
String[][] data = {{title, season, episode}};
jt = new JTable(data, columns);
};
jt.setPreferredScrollableViewportSize(new Dimension(450, 63));
jt.setFillsViewportHeight(true);
JScrollPane jps = new JScrollPane(jt);
add(jps);
}
catch (Exception er) {System.err.println(er);}
}
public static void main(String[] args) {
JFrame jf = new JFrame();
Test2 t = new Test2();
jf.setTitle("Test");
jf.setSize(500,500);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(t);
}
}
您的问题在这里:
while (rs.next()) {
String title = rs.getString("Title");
String season = rs.getString("Season");
String episode = rs.getString("Episode");
String[] columns = { "Title", "S", "E" };
String[][] data = { { title, season, episode } };
jt = new JTable(data, columns); // *** you're making many JTables here!! ***
}
每次 while 循环循环时,您都会创建 并 丢弃 一个新的 JTable 对象。异常发生在最后一次循环时,结果集最后一行的数据没有被丢弃,然后显示在最终创建的 JTable 中。为了解决这个问题,要在JTable中显示所有的数据,你需要在while循环中整理你所有的结果集数据,然后将它添加到JTable中,最简单的这样做的方法是创建一个 table 模型,这里是一个简单的 DefaultTableModel,before while 循环,并将其填充到 while 循环内的每一行结果集数据中:
// create a table model with the appropriate column headers
// and with 0 rows (to start with)
String[] columnNames = {"Title", "Season", "Episode"};
DefaultTableModel tableModel = new DefaultTableModel(columnNames, 0);
while (rs.next()) {
String title = rs.getString("Title");
String season = rs.getString("Season");
String episode = rs.getString("Episode");
// create a single array of one row's worth of data
String[] data = { title, season, episode } ;
// and add this row of data into the table model
tableModel.addRow(data);
}
jt.setModel(tableModel); // place model into JTable
或者更好,将最后一行更改为:
jt = new JTable(tableModel); // to create a new JTable
试试这个……你可以轻松做到……
public final void EmployeeGridView(){
try{
Connection conn = DBConn.connect();
PreparedStatement ps = conn.prepareStatement("Select * from empdetails");
ResultSet rs=ps.executeQuery();
DefaultTableModel tm = (DefaultTableModel)jTable1.getModel();
tm.setRowCount(0);
while(rs.next()){
Object o[] = {rs.getInt("EMPID"),rs.getString("FirstName"),rs.getString("LastName"),rs.getString("Designation"),rs.getString("NIC"),rs.getString("PhoneNO"),rs.getString("DOB"),rs.getString("Address"),rs.getString("Gender")};
tm.addRow(o);
}
}
catch(Exception e){
JOptionPane.showMessageDialog(null,"Error in Employee Grid View..... "+e);
}
}