📄 jt.java
字号:
import javax.swing.table.*;
import java.sql.*;
import java.awt.*;
import java.awt.Window;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.text.MessageFormat;
import java.net.*;
import java.util.*;
public class jt extends JFrame implements ActionListener
{
JButton b1;// 导出按钮
JButton b2;//打印
JButton b3;//退出
String receivedDatabaseName="";
String receivedTableName="";
JTable table;
private JFileChooser chooser;//选择器
Connection con;
Statement stm;
ResultSet res1;
String dbname="test";
String user="123";
String pass="s";
String db="sqlserver";
public jt(String dbName,String user,String pass,String db) throws Exception
{
receivedDatabaseName=dbName;//得到数据库名
receivedTableName="score";//得到数据库中表名
this.user=user;
this.pass=pass;
this.db=db;
String[] names={"考号",
"成绩",
"考试ip",
"考生信息"};
int i,j,RowNum=0,ColNum;
Object[][] info;
String sqlstr;
setTitle("考生考试成绩"); //连接数据库
{if(this.db=="sqlserver")
{ try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(ClassNotFoundException err){
System.out.println("驱动未找到");
}
try{
con=DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;DatabaseName="+receivedDatabaseName,user,pass);
stm=con.createStatement();
}
catch(SQLException error){
System.out.println("醉了"+error.toString());
}
}
if(this.db=="access")
{ try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException err){
System.out.println("驱动未找到");
}
try{
con=DriverManager.getConnection("jdbc:odbc:"+receivedDatabaseName,user,pass);
stm=con.createStatement();
}
catch(SQLException error){
System.out.println("醉了"+error.toString());
}
}
if(this.db=="mysql")
{ try{
Class.forName("com.mysql.jdbc.Driver");
}
catch(ClassNotFoundException err){
System.out.println("驱动未找到");
}
try{
con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/"+receivedDatabaseName+"?user="+user+"&password="+pass);
stm=con.createStatement();
}
catch(SQLException error){
System.out.println("醉了"+error.toString());
}
}
if(this.db=="access数据库直连")
{ try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException err){
System.out.println("驱动未找到");
}
try{
con=DriverManager.getConnection("jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="+receivedDatabaseName,user,pass);
stm=con.createStatement();
}
catch(SQLException error){
System.out.println("醉了"+error.toString());
}
} }
// Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
// String url="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=test.mdb";
// Connection connection=DriverManager.getConnection(url,user,pass);
//Statement sta=connection.createStatement();
///////////////////////////////////////////////////////////////////////////
sqlstr="select count(*) from "+receivedTableName; //取得数据表的记录数和字段数
res1=stm.executeQuery(sqlstr);
res1.next();
RowNum=res1.getInt(1);
sqlstr="select * from "+receivedTableName;
res1=stm.executeQuery(sqlstr);
ResultSetMetaData rsmd=res1.getMetaData();
ColNum=rsmd.getColumnCount();
info=new Object[RowNum][];
i=0;
while (res1.next())
{
info[i]=new Object[ColNum];
for (j=1;j<=ColNum;j++)
{
info[i][j-1]=res1.getObject(j);
}
i++;
}
con.close();
chooser=new JFileChooser();
table=new JTable(info,names);
table.setPreferredScrollableViewportSize(new Dimension(400,50));
JScrollPane scrollPane=new JScrollPane(table);
Container cont=getContentPane();
cont.setLayout(new GridLayout(2,1));
cont.add(scrollPane);
Panel p=new Panel();
b1=new JButton("导出");
b1.addActionListener(this);
b2=new JButton("打印");
b2.addActionListener(this);
b3=new JButton("退出");
b3.addActionListener(this);
p.add(b1);p.add(b2);p.add(b3);
cont.add(p);
pack();
setSize(600,300);
setLocation(300,300);
//f.setResizable(false);
setVisible(true);
}
public void exportTable(JTable table, File file) throws IOException {
TableModel model = table.getModel();
FileWriter out = new FileWriter(file);
for(int i=0; i < model.getColumnCount(); i++) {
out.write(model.getColumnName(i) + "\t");
}
out.write("\n");
for(int i=0; i< model.getRowCount(); i++) {
for(int j=0; j < model.getColumnCount(); j++) {
out.write(model.getValueAt(i,j).toString()+"\t");
}
out.write("\n");
}
out.close();
System.out.println("write out to: " + file);
}
public void actionPerformed(ActionEvent event){
if(event.getSource()==b1){
try {
String ss="score.xls";
int state; //文件选择器返回状态
chooser.removeChoosableFileFilter(chooser.getAcceptAllFileFilter()); //移去所有文件过滤器
chooser.addChoosableFileFilter(new MyFileFilter("xls","excel文件")); //增加文件过滤器,接爱xls文件
{ state=chooser.showSaveDialog(null); //显示打开文件对话框
//显示保存文件对话框
File file = chooser.getSelectedFile(); //得到选择的文件
if(file != null && state == JFileChooser.APPROVE_OPTION) { //选择了文件并点击了打开可保存按钮
JOptionPane.showMessageDialog(null, file.getPath()); //显示提示信息
ss=file.getPath();
ss=ss.concat(".xls");
ss=ss.replaceAll("\\\\","\\\\\\\\");
//dbta.setText(file.getPath());
}
else if(state == JFileChooser.CANCEL_OPTION) { //点击了撤销按钮
JOptionPane.showMessageDialog(null, "退出!"); //显示提示信息
}
else if(state == JFileChooser.ERROR_OPTION) {
JOptionPane.showMessageDialog(null, "错误!"); //显示提示信息
}}
exportTable(table, new File(ss));
} catch (Exception ex) {
System.out.println(ex.getMessage());
ex.printStackTrace();
}
}
if(event.getSource()==b2){
MessageFormat header = new MessageFormat("Page {0,number,integer}");
try {
table.print(JTable.PrintMode.FIT_WIDTH, header, null);
} catch (java.awt.print.PrinterException e) {
System.err.format("Cannot print %s%n", e.getMessage());
}
}
if(event.getSource()==b3){
dispose();
}
}
//public static void main(String args[]){
//try{ jt j=new jt("D:\\java\\tt\\test","","","access数据库直连");}
//catch(Exception ex){
// System.out.println(ex.toString());
//}
//}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -