在JAVA中怎么将数据库统计的数写到GUI界面的表格中
展开全部
给你看个例子啊:自己按自己的意思改一下吧
import java.awt.BorderLayout;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class JTableTest extends JFrame {
public void JtableMake(String sql) {
JTable jt = new JTable();
String header[] ={"Sno","Sname","sex","Age","sdept"};
DefaultTableModel md = new DefaultTableModel(header,0);
JScrollPane scrollPane = new JScrollPane(jt);
jt.setModel(md);
corpertate cor = new corpertate();
Connection conn = null;
conn = cor.getConn();
Statement statement = null;
try {
statement = conn.createStatement();
} catch (SQLException e2) {
e2.printStackTrace();
}
try{
ResultSet rest = statement.executeQuery(sql);
while(rest.next()){
Vector v = new Vector();
v.addElement(rest.getString(1));
v.addElement(rest.getString(2));
v.addElement(rest.getString(3));
v.addElement(rest.getInt(4));
v.addElement(rest.getString(5));
md.addRow(v);
}
}catch(SQLException e1){
e1.printStackTrace();
}
this.getContentPane().add(scrollPane,BorderLayout.CENTER);
this.setSize(500,250);
//this.pack();
this.setVisible(true);
}
}
import java.awt.BorderLayout;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class JTableTest extends JFrame {
public void JtableMake(String sql) {
JTable jt = new JTable();
String header[] ={"Sno","Sname","sex","Age","sdept"};
DefaultTableModel md = new DefaultTableModel(header,0);
JScrollPane scrollPane = new JScrollPane(jt);
jt.setModel(md);
corpertate cor = new corpertate();
Connection conn = null;
conn = cor.getConn();
Statement statement = null;
try {
statement = conn.createStatement();
} catch (SQLException e2) {
e2.printStackTrace();
}
try{
ResultSet rest = statement.executeQuery(sql);
while(rest.next()){
Vector v = new Vector();
v.addElement(rest.getString(1));
v.addElement(rest.getString(2));
v.addElement(rest.getString(3));
v.addElement(rest.getInt(4));
v.addElement(rest.getString(5));
md.addRow(v);
}
}catch(SQLException e1){
e1.printStackTrace();
}
this.getContentPane().add(scrollPane,BorderLayout.CENTER);
this.setSize(500,250);
//this.pack();
this.setVisible(true);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询