import java.sql.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
//import java.awt.event.*;
publicclassdisplaydemoextendsjframe
//implements操作监听器
{
//私有字符串
dburl=' JDBC : Microsoft : SQL server ://202.115.26.18133601433 '; //数据库可分辨名称
私有字符串用户=' Devon '; //数据库用户
私有字符串密码=' book '; //数据库用户密码
私有jtable table;
private JButton ok,canel;
公共显示演示
super (显示数据库查询结果)//调用父类的构造函数
String[] columnNames={ '用户名','年龄','性别','电子邮件' }; //列名object [ ] [ ] row data=new object [5] [4]; //表数据
ok=newJbutton('ok ' );
canel=new JButton ('取消);
//ok.addactionlistener(this;
//Canel.addactionlistener(this;
try {
class.forname (sun.JDBC.odbc.jdbcodbcdriver );
连接
con=driver manager.getconnection (JDBC : odbc : example,' sa ',' ); //class.forname (com.Microsoft.JDBC.SQL server.SQL server driver ); //加载驱动器
//connection con=driver manager.getconnection (dburl,user,password ); //连接
String sqlStr='select * from users '; //查询语句
preparedstatementps=con.preparestatement (sqlstr; 获取属性语句对象
ResultSet rs=ps.executeQuery (; //执行查询
String name,sex,email; //查询结果
int age;
int count=0;
wile(RS.next ) () ) /遍历查询结果
row data [ count ] [0]=RS.getstring (' name ); //初始化数组内容的row data [ count ] [1]=integer.tostring (RS.getint ) ' age );
row data [ count ] [2]=RS.getstring (' sex ';
row data [ count ] [3]=RS.getstring (' email );
出局;
}
con.close (; //关闭连接
}
catch(exceptionex ) {
ex.printStackTrace (; //输出错误信息
}
容器容器=get content pane (; //获取窗口容器
//container.set layout (空);
container.add (确定; container.add(canel );
ok.set bounds (10,120,70,20 );
canel.set bounds (100、120、70、20 );
table=newjtable(rowdata,columnNames ); //实例化表
table.getColumn (年龄) ).setMaxwidth ) 25; //设定行宽
container.add (new jscrollpane (table ),BorderLayout.CENTER ); //增加组件
setsize (300200; //设置窗口大小
设置可见性(true; //可看到设定窗口
setdefaultcloseoperation (jframe.exit _ on _ close; //关闭窗口时退出程序
}
publicstaticvoidmain (string [ ] args ) {
新显示深度(;
}
}