窗体上有一JTable,如何让数据库里的数据在表格里显示出来
谢谢!!!
8 个解决方案
#1
import java.awt.*;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.*;
/**
* TableQueryFrame extends JInternalFrame to create a display which builds SQL
* CREATE statements
*/
class TableQueryFrame extends JInternalFrame
{
JTable table;
JScrollPane tableScroller;
DatabaseUtilities dbUtils;
String tableName = null;
String colNames[] = null;
String dataTypes[] = null;
String SQLQuery = null;
public TableQueryFrame(String tableName, DatabaseUtilities dbUtils)
{
setSize(450,300);
setClosable(true);
setMaximizable(true);
setIconifiable(true);
setResizable(true);
getContentPane().setLayout(new BorderLayout());
this.tableName=tableName;
this.dbUtils=dbUtils;
colNames = dbUtils.getColumnNames(tableName);
dataTypes = dbUtils.getDataTypes(tableName);
SQLQuery = "SELECT TOP 5 * FROM "+tableName;
Vector dataSet = dbUtils.executeQuery(SQLQuery);
table = createTable(colNames,dataSet);
tableScroller = new JScrollPane(table);
getContentPane().add(tableScroller,BorderLayout.CENTER);
setVisible(true);
}
JTable createTable(String[] colNames,Vector dataSet)
{
int nRows = dataSet.size();
String[][] rowData = new String[nRows][colNames.length];
for(int i=0;i<nRows;i++)
{
Vector row = (Vector)dataSet.elementAt(i);
for(int j=0;j<row.size();j++)
rowData[i][j]=((Object)row.elementAt(j)).toString();
}
JTable table = new JTable(rowData,colNames);
return table;
}
}
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.*;
/**
* TableQueryFrame extends JInternalFrame to create a display which builds SQL
* CREATE statements
*/
class TableQueryFrame extends JInternalFrame
{
JTable table;
JScrollPane tableScroller;
DatabaseUtilities dbUtils;
String tableName = null;
String colNames[] = null;
String dataTypes[] = null;
String SQLQuery = null;
public TableQueryFrame(String tableName, DatabaseUtilities dbUtils)
{
setSize(450,300);
setClosable(true);
setMaximizable(true);
setIconifiable(true);
setResizable(true);
getContentPane().setLayout(new BorderLayout());
this.tableName=tableName;
this.dbUtils=dbUtils;
colNames = dbUtils.getColumnNames(tableName);
dataTypes = dbUtils.getDataTypes(tableName);
SQLQuery = "SELECT TOP 5 * FROM "+tableName;
Vector dataSet = dbUtils.executeQuery(SQLQuery);
table = createTable(colNames,dataSet);
tableScroller = new JScrollPane(table);
getContentPane().add(tableScroller,BorderLayout.CENTER);
setVisible(true);
}
JTable createTable(String[] colNames,Vector dataSet)
{
int nRows = dataSet.size();
String[][] rowData = new String[nRows][colNames.length];
for(int i=0;i<nRows;i++)
{
Vector row = (Vector)dataSet.elementAt(i);
for(int j=0;j<row.size();j++)
rowData[i][j]=((Object)row.elementAt(j)).toString();
}
JTable table = new JTable(rowData,colNames);
return table;
}
}
#2
这段程序是去年我做的一个小型MIS中的一个类,应该符合你的要求,不过我是把JTable放入了JInternalFrame里,你也可以试试别的容器,去年我做的时候一开始把它放进JPanel是没有成功,别的容器还没有试
#3
非常感谢!!1我试试看
#4
import java.awt.*;
import javax.swing.*;
import javax.swing.table.TableColumn;
public class tableUI extends JPanel
{
public tableUI(String databaseName, String tableName, String[] colNames,
int[] colWidth, int totalWidth) {
try {
Class.forName("org.gjt.mm.mysql.Driver");
} catch (ClassNotFoundException e) {
System.out.println(e);
}
tableHandler = new TableHandler(databaseName, tableName, colNames);
tableHandler.updateTable();
TableColumn column = null;
table = new JTable(tableHandler);
table
.setPreferredScrollableViewportSize(new Dimension(totalWidth,
210));
table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
for (int i = 0; i < colWidth.length; i++) {
column = table.getColumnModel().getColumn(i);
column.setPreferredWidth(colWidth[i] * 100);
}
JScrollPane s = new JScrollPane(table);
this.add(s, BorderLayout.CENTER);
this.setSize(this.getPreferredSize());
setVisible(true);
}
JTable table;
JScrollPane scrollPane;
public TableHandler tableHandler;
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.table.AbstractTableModel;
public class TableHandler extends AbstractTableModel {
public TableHandler(String databaseName, String tableName, String[] colNames) {
super();
this.tableName = tableName;
this.databaseName = databaseName;
colNum = colNames.length;
this.colNames = new String[colNum];
System.arraycopy(colNames, 0, this.colNames, 0, colNum);
}
public void updateTable() {
try {
Connection conn;
conn = DriverManager.getConnection("jdbc:mysql://localhost/"
+ databaseName + "?user=root&password=");
Statement myStatement = conn.createStatement();
ResultSet rt = myStatement.executeQuery("SELECT count(*) FROM "
+ tableName);
rt.next();
rowNum = rt.getInt(1);
list = new String[rowNum][colNum];
ResultSet rs = myStatement.executeQuery("SELECT * FROM "
+ tableName);
int row = 0;
while (rs.next()) {
for (int i = 0; i < colNum; i++) {
list[row][i] = rs.getString(colNames[i]);
}
row++;
}
conn.close();
fireTableDataChanged();
} catch (SQLException e) {
System.out.println(e);
}
}
public int getColumnCount() {
return colNum;
}
public int getRowCount() {
return rowNum;
}
public String getColumnName(int col) {
return colNames[col];
}
public Object getValueAt(int row, int column) {
return list[row][column];
}
String[][] list;
String[] colNames;
String tableName;
String databaseName;
int rowNum;
int colNum;
}
#5
自己new一个实例看一下结果吧,
我假设你懂得怎样连接数据库,
我这里用的是mysql.
我假设你懂得怎样连接数据库,
我这里用的是mysql.
#6
谢谢 向往未来
#7
前几天,我刚做了一个,看看你对数组的操作是否熟悉,
像我不熟悉,就用数据模型去读库里的数据库,比较简单.
像我不熟悉,就用数据模型去读库里的数据库,比较简单.
#8
无冬之夜 能把你做的那个给我一份吗?我也是刚学的
imain@163.com
imain@163.com
#1
import java.awt.*;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.*;
/**
* TableQueryFrame extends JInternalFrame to create a display which builds SQL
* CREATE statements
*/
class TableQueryFrame extends JInternalFrame
{
JTable table;
JScrollPane tableScroller;
DatabaseUtilities dbUtils;
String tableName = null;
String colNames[] = null;
String dataTypes[] = null;
String SQLQuery = null;
public TableQueryFrame(String tableName, DatabaseUtilities dbUtils)
{
setSize(450,300);
setClosable(true);
setMaximizable(true);
setIconifiable(true);
setResizable(true);
getContentPane().setLayout(new BorderLayout());
this.tableName=tableName;
this.dbUtils=dbUtils;
colNames = dbUtils.getColumnNames(tableName);
dataTypes = dbUtils.getDataTypes(tableName);
SQLQuery = "SELECT TOP 5 * FROM "+tableName;
Vector dataSet = dbUtils.executeQuery(SQLQuery);
table = createTable(colNames,dataSet);
tableScroller = new JScrollPane(table);
getContentPane().add(tableScroller,BorderLayout.CENTER);
setVisible(true);
}
JTable createTable(String[] colNames,Vector dataSet)
{
int nRows = dataSet.size();
String[][] rowData = new String[nRows][colNames.length];
for(int i=0;i<nRows;i++)
{
Vector row = (Vector)dataSet.elementAt(i);
for(int j=0;j<row.size();j++)
rowData[i][j]=((Object)row.elementAt(j)).toString();
}
JTable table = new JTable(rowData,colNames);
return table;
}
}
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.*;
/**
* TableQueryFrame extends JInternalFrame to create a display which builds SQL
* CREATE statements
*/
class TableQueryFrame extends JInternalFrame
{
JTable table;
JScrollPane tableScroller;
DatabaseUtilities dbUtils;
String tableName = null;
String colNames[] = null;
String dataTypes[] = null;
String SQLQuery = null;
public TableQueryFrame(String tableName, DatabaseUtilities dbUtils)
{
setSize(450,300);
setClosable(true);
setMaximizable(true);
setIconifiable(true);
setResizable(true);
getContentPane().setLayout(new BorderLayout());
this.tableName=tableName;
this.dbUtils=dbUtils;
colNames = dbUtils.getColumnNames(tableName);
dataTypes = dbUtils.getDataTypes(tableName);
SQLQuery = "SELECT TOP 5 * FROM "+tableName;
Vector dataSet = dbUtils.executeQuery(SQLQuery);
table = createTable(colNames,dataSet);
tableScroller = new JScrollPane(table);
getContentPane().add(tableScroller,BorderLayout.CENTER);
setVisible(true);
}
JTable createTable(String[] colNames,Vector dataSet)
{
int nRows = dataSet.size();
String[][] rowData = new String[nRows][colNames.length];
for(int i=0;i<nRows;i++)
{
Vector row = (Vector)dataSet.elementAt(i);
for(int j=0;j<row.size();j++)
rowData[i][j]=((Object)row.elementAt(j)).toString();
}
JTable table = new JTable(rowData,colNames);
return table;
}
}
#2
这段程序是去年我做的一个小型MIS中的一个类,应该符合你的要求,不过我是把JTable放入了JInternalFrame里,你也可以试试别的容器,去年我做的时候一开始把它放进JPanel是没有成功,别的容器还没有试
#3
非常感谢!!1我试试看
#4
import java.awt.*;
import javax.swing.*;
import javax.swing.table.TableColumn;
public class tableUI extends JPanel
{
public tableUI(String databaseName, String tableName, String[] colNames,
int[] colWidth, int totalWidth) {
try {
Class.forName("org.gjt.mm.mysql.Driver");
} catch (ClassNotFoundException e) {
System.out.println(e);
}
tableHandler = new TableHandler(databaseName, tableName, colNames);
tableHandler.updateTable();
TableColumn column = null;
table = new JTable(tableHandler);
table
.setPreferredScrollableViewportSize(new Dimension(totalWidth,
210));
table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
for (int i = 0; i < colWidth.length; i++) {
column = table.getColumnModel().getColumn(i);
column.setPreferredWidth(colWidth[i] * 100);
}
JScrollPane s = new JScrollPane(table);
this.add(s, BorderLayout.CENTER);
this.setSize(this.getPreferredSize());
setVisible(true);
}
JTable table;
JScrollPane scrollPane;
public TableHandler tableHandler;
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.table.AbstractTableModel;
public class TableHandler extends AbstractTableModel {
public TableHandler(String databaseName, String tableName, String[] colNames) {
super();
this.tableName = tableName;
this.databaseName = databaseName;
colNum = colNames.length;
this.colNames = new String[colNum];
System.arraycopy(colNames, 0, this.colNames, 0, colNum);
}
public void updateTable() {
try {
Connection conn;
conn = DriverManager.getConnection("jdbc:mysql://localhost/"
+ databaseName + "?user=root&password=");
Statement myStatement = conn.createStatement();
ResultSet rt = myStatement.executeQuery("SELECT count(*) FROM "
+ tableName);
rt.next();
rowNum = rt.getInt(1);
list = new String[rowNum][colNum];
ResultSet rs = myStatement.executeQuery("SELECT * FROM "
+ tableName);
int row = 0;
while (rs.next()) {
for (int i = 0; i < colNum; i++) {
list[row][i] = rs.getString(colNames[i]);
}
row++;
}
conn.close();
fireTableDataChanged();
} catch (SQLException e) {
System.out.println(e);
}
}
public int getColumnCount() {
return colNum;
}
public int getRowCount() {
return rowNum;
}
public String getColumnName(int col) {
return colNames[col];
}
public Object getValueAt(int row, int column) {
return list[row][column];
}
String[][] list;
String[] colNames;
String tableName;
String databaseName;
int rowNum;
int colNum;
}
#5
自己new一个实例看一下结果吧,
我假设你懂得怎样连接数据库,
我这里用的是mysql.
我假设你懂得怎样连接数据库,
我这里用的是mysql.
#6
谢谢 向往未来
#7
前几天,我刚做了一个,看看你对数组的操作是否熟悉,
像我不熟悉,就用数据模型去读库里的数据库,比较简单.
像我不熟悉,就用数据模型去读库里的数据库,比较简单.
#8
无冬之夜 能把你做的那个给我一份吗?我也是刚学的
imain@163.com
imain@163.com