怎么样将数据库的表在Java中界面中显示出来
2个回答
推荐于2017-11-23
展开全部
import javax.swing.*;
import javax.swing.table.JTableHeader;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class Test extends JFrame{
// 定义组件
private JScrollPane scpDemo;
private JTableHeader jth;
private JTable tabDemo;
private JButton btnShow;
// 构造方法
public Test(){
// 窗体的相关属性的定义
super("JTable数据绑定示例");
this.setSize(330,400);
this.setLayout(null);
this.setLocation(100,50);
// 创建组件
this.scpDemo = new JScrollPane();
this.scpDemo.setBounds(10,50,300,270);
this.btnShow = new JButton("显示数据");
this.btnShow.setBounds(10,10,300,30);
// 给按钮注册监听
this.btnShow.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
btnShow_ActionPerformed(ae);
}
});
// 将组件加入到窗体中
add(this.scpDemo);
add(this.btnShow);
// 显示窗体
this.setVisible(true);
}
// 点击按钮时的事件处理
public void btnShow_ActionPerformed(ActionEvent ae){
// 以下是连接数据源和显示数据的具体处理方法,请注意下
try{
// 获得连接
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:localServer","sa","");
// 建立查询条件
String sql = "select * from localServer";
PreparedStatement pstm = conn.prepareStatement(sql);
// 执行查询
ResultSet rs = pstm.executeQuery();
// 计算有多少条记录
int count = 0;
while(rs.next()){
count++;
}
rs = pstm.executeQuery();
// 将查询获得的记录数据,转换成适合生成JTable的数据形式
Object[][] info = new Object[count][4];
count = 0;
while(rs.next()){
info[count][0] = Integer.valueOf( rs.getInt("id"));
info[count][1] = rs.getString("name");
info[count][2] = Integer.valueOf( rs.getInt("age") );
info[count][3] = rs.getString("sex");
count++;
}
// 定义表头
String[] title = {"学号","姓名","年龄","性别"};
// 创建JTable
this.tabDemo = new JTable(info,title);
// 显示表头
this.jth = this.tabDemo.getTableHeader();
// 将JTable加入到带滚动条的面板中
this.scpDemo.getViewport().add(tabDemo);
}catch(ClassNotFoundException cnfe){
JOptionPane.showMessageDialog(null,"数据源错误","错误",JOptionPane.ERROR_MESSAGE);
}catch(SQLException sqle){
JOptionPane.showMessageDialog(null,"数据操作错误","错误",JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String[] args){
new Test();
}
}
import javax.swing.table.JTableHeader;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class Test extends JFrame{
// 定义组件
private JScrollPane scpDemo;
private JTableHeader jth;
private JTable tabDemo;
private JButton btnShow;
// 构造方法
public Test(){
// 窗体的相关属性的定义
super("JTable数据绑定示例");
this.setSize(330,400);
this.setLayout(null);
this.setLocation(100,50);
// 创建组件
this.scpDemo = new JScrollPane();
this.scpDemo.setBounds(10,50,300,270);
this.btnShow = new JButton("显示数据");
this.btnShow.setBounds(10,10,300,30);
// 给按钮注册监听
this.btnShow.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
btnShow_ActionPerformed(ae);
}
});
// 将组件加入到窗体中
add(this.scpDemo);
add(this.btnShow);
// 显示窗体
this.setVisible(true);
}
// 点击按钮时的事件处理
public void btnShow_ActionPerformed(ActionEvent ae){
// 以下是连接数据源和显示数据的具体处理方法,请注意下
try{
// 获得连接
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:localServer","sa","");
// 建立查询条件
String sql = "select * from localServer";
PreparedStatement pstm = conn.prepareStatement(sql);
// 执行查询
ResultSet rs = pstm.executeQuery();
// 计算有多少条记录
int count = 0;
while(rs.next()){
count++;
}
rs = pstm.executeQuery();
// 将查询获得的记录数据,转换成适合生成JTable的数据形式
Object[][] info = new Object[count][4];
count = 0;
while(rs.next()){
info[count][0] = Integer.valueOf( rs.getInt("id"));
info[count][1] = rs.getString("name");
info[count][2] = Integer.valueOf( rs.getInt("age") );
info[count][3] = rs.getString("sex");
count++;
}
// 定义表头
String[] title = {"学号","姓名","年龄","性别"};
// 创建JTable
this.tabDemo = new JTable(info,title);
// 显示表头
this.jth = this.tabDemo.getTableHeader();
// 将JTable加入到带滚动条的面板中
this.scpDemo.getViewport().add(tabDemo);
}catch(ClassNotFoundException cnfe){
JOptionPane.showMessageDialog(null,"数据源错误","错误",JOptionPane.ERROR_MESSAGE);
}catch(SQLException sqle){
JOptionPane.showMessageDialog(null,"数据操作错误","错误",JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String[] args){
new Test();
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
public class Test extends JFrame{
// 定义组件
private JScrollPane scpDemo;
private JTableHeader jth;
private JTable tabDemo;
private JButton btnShow;
// 构造方法
public Test(){
// 窗体的相关属性的定义
super("JTable数据绑定示例");
this.setSize(330,400);
this.setLayout(null);
this.setLocation(100,50);
// 创建组件
this.scpDemo = new JScrollPane();
this.scpDemo.setBounds(10,50,300,270);
this.btnShow = new JButton("显示数据");
this.btnShow.setBounds(10,10,300,30);
// 给按钮注册监听
this.btnShow.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
btnShow_ActionPerformed(ae);
}
});
// 将组件加入到窗体中
add(this.scpDemo);
add(this.btnShow);
// 显示窗体
this.setVisible(true);
}
// 点击按钮时的事件处理
public void btnShow_ActionPerformed(ActionEvent ae){
// 以下是连接数据源和显示数据的具体处理方法,请注意下
try{
// 获得连接
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:localServer","sa","");
// 建立查询条件
String sql = "select * from localServer";
PreparedStatement pstm = conn.prepareStatement(sql);
// 执行查询
ResultSet rs = pstm.executeQuery();
// 计算有多少条记录
int count = 0;
while(rs.next()){
count++;
}
rs = pstm.executeQuery();
// 将查询获得的记录数据,转换成适合生成JTable的数据形式
Object[][] info = new Object[count][4];
count = 0;
while(rs.next()){
info[count][0] = Integer.valueOf( rs.getInt"prisoner_id");
info[count][1] = rs.getString("prisoner_name");
info[count][2] = rs.getString("prisoner_sex");
info[count][3] = Integer.valueOf( rs.getInt"nation");
info[count][4] = rs.getString("birthplace");
info[count][5] = Integer.valueOf( rs.getInt"id_card");
info[count][6] = Integer.valueOf( rs.getInt"birthday");
info[count][7] = rs.getString("imprison_area");
info[count][8] = Integer.valueOf( rs.getInt"jail_time");
info[count][9] = Integer.valueOf( rs.getInt"serve_time");
info[count][10] = rs.getString("charge");
info[count][11] = rs.getString("performance");
info[count][12] = rs.getString("priors");
count++;
}
// 定义表头
String[] title = "编号", , " 姓名", "性别", "民族",
"出生地", "身份证号","出生日期","关押区域","入狱时间","刑期","罪名","入狱表现","是否前科"};
this.tabDemo = new JTable(info,title);
// 显示表头
this.jth = this.tabDemo.getTableHeader();
// 将JTable加入到带滚动条的面板中
this.scpDemo.getViewport().add(tabDemo);
}catch(ClassNotFoundException cnfe){
JOptionPane.showMessageDialog(null,"数据源错误","错误",JOptionPane.ERROR_MESSAGE);
}catch(SQLException sqle){
JOptionPane.showMessageDialog(null,"数据操作错误","错误",JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String[] args){
new Test();
}
}
import java.awt.event.ActionListener;
import java.sql.*;
public class Test extends JFrame{
// 定义组件
private JScrollPane scpDemo;
private JTableHeader jth;
private JTable tabDemo;
private JButton btnShow;
// 构造方法
public Test(){
// 窗体的相关属性的定义
super("JTable数据绑定示例");
this.setSize(330,400);
this.setLayout(null);
this.setLocation(100,50);
// 创建组件
this.scpDemo = new JScrollPane();
this.scpDemo.setBounds(10,50,300,270);
this.btnShow = new JButton("显示数据");
this.btnShow.setBounds(10,10,300,30);
// 给按钮注册监听
this.btnShow.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
btnShow_ActionPerformed(ae);
}
});
// 将组件加入到窗体中
add(this.scpDemo);
add(this.btnShow);
// 显示窗体
this.setVisible(true);
}
// 点击按钮时的事件处理
public void btnShow_ActionPerformed(ActionEvent ae){
// 以下是连接数据源和显示数据的具体处理方法,请注意下
try{
// 获得连接
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:localServer","sa","");
// 建立查询条件
String sql = "select * from localServer";
PreparedStatement pstm = conn.prepareStatement(sql);
// 执行查询
ResultSet rs = pstm.executeQuery();
// 计算有多少条记录
int count = 0;
while(rs.next()){
count++;
}
rs = pstm.executeQuery();
// 将查询获得的记录数据,转换成适合生成JTable的数据形式
Object[][] info = new Object[count][4];
count = 0;
while(rs.next()){
info[count][0] = Integer.valueOf( rs.getInt"prisoner_id");
info[count][1] = rs.getString("prisoner_name");
info[count][2] = rs.getString("prisoner_sex");
info[count][3] = Integer.valueOf( rs.getInt"nation");
info[count][4] = rs.getString("birthplace");
info[count][5] = Integer.valueOf( rs.getInt"id_card");
info[count][6] = Integer.valueOf( rs.getInt"birthday");
info[count][7] = rs.getString("imprison_area");
info[count][8] = Integer.valueOf( rs.getInt"jail_time");
info[count][9] = Integer.valueOf( rs.getInt"serve_time");
info[count][10] = rs.getString("charge");
info[count][11] = rs.getString("performance");
info[count][12] = rs.getString("priors");
count++;
}
// 定义表头
String[] title = "编号", , " 姓名", "性别", "民族",
"出生地", "身份证号","出生日期","关押区域","入狱时间","刑期","罪名","入狱表现","是否前科"};
this.tabDemo = new JTable(info,title);
// 显示表头
this.jth = this.tabDemo.getTableHeader();
// 将JTable加入到带滚动条的面板中
this.scpDemo.getViewport().add(tabDemo);
}catch(ClassNotFoundException cnfe){
JOptionPane.showMessageDialog(null,"数据源错误","错误",JOptionPane.ERROR_MESSAGE);
}catch(SQLException sqle){
JOptionPane.showMessageDialog(null,"数据操作错误","错误",JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String[] args){
new Test();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询