July 18 2007

07月18日(星期三)

 

Today i had already done last year exam on computer .

============================

here is the DBoperator

============================

import java.sql.*;
import java.io.FileReader;
import java.io.File;
import java.io.BufferedReader;
import java.io.*;

/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class DataOperator {
StuFrm frm;
public DataOperator(StuFrm frm) {
this.frm = frm;
selectall();

}

public void selectall() {
String Resource = "sun.jdbc.odbc.JdbcOdbcDriver";
boolean flag = false;

try {
Class.forName(Resource);
} catch (ClassNotFoundException ex1) {
}
Connection con = null;

try {
con = DriverManager.getConnection("jdbc:odbc:my");
} catch (SQLException ex2) {
}
String sql = "select * from stu";
Statement stmt = null;
try {
stmt = con.createStatement();
} catch (SQLException ex3) {
}
ResultSet rs = null;
try {
rs = stmt.executeQuery(sql);
while (rs.next()) {
frm.txtDisplay.append("学号: " + rs.getInt("StuId")+" ");
frm.txtDisplay.append("姓名: " + rs.getString("stuname")+" ");
frm.txtDisplay.append("性别: " + rs.getString("stusex")+" ");
frm.txtDisplay.append("年龄" + rs.getInt("stuage")+" ");
frm.txtDisplay.append(" ");

}
} catch (SQLException ex4) {
}

}

}

==============================

here is the Frame,but not same as yesterday's

==============================

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Rectangle;
import java.awt.Font;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.*;
import javax.swing.JOptionPane;

/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class StuFrm extends JFrame {
public StuFrm() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}

private void jbInit() throws Exception {
getContentPane().setLayout(null);
jLabel1.setFont(new java.awt.Font("宋体", Font.PLAIN, 20));
jLabel1.setText("学生信息");
jLabel1.setBounds(new Rectangle(68, 49, 95, 30));
jButton1.setBounds(new Rectangle(119, 346, 99, 25));
jButton1.setText("保存为文本");
jButton1.addActionListener(new StuFrm_jButton1_actionAdapter(this));
jButton2.setBounds(new Rectangle(304, 346, 99, 25));
jButton2.setText("查询数据库");
jButton2.addActionListener(new StuFrm_jButton2_actionAdapter(this));
jScrollPane2.setBounds(new Rectangle(185, 19, 230, 59));
this.getContentPane().add(jLabel1);
this.getContentPane().add(jScrollPane1);
this.getContentPane().add(jButton1);
this.getContentPane().add(jButton2);
this.getContentPane().add(jScrollPane2);
jScrollPane2.getViewport().add(txtnew);
jScrollPane1.getViewport().add(txtDisplay);
jScrollPane1.setBounds(new Rectangle(89, 105, 314, 210));
this.setTitle("查询学生信息");
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setSize(700, 600);
this.setResizable(false);
this.setVisible(true);

}

public static void main(String[] args) {
new StuFrm();
}

JLabel jLabel1 = new JLabel();
JScrollPane jScrollPane1 = new JScrollPane();
JTextArea txtDisplay = new JTextArea();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JScrollPane jScrollPane2 = new JScrollPane();
JTextArea txtnew = new JTextArea();
public void jButton2_actionPerformed(ActionEvent e) {
DataOperator d=new DataOperator(this);

}

public void jButton1_actionPerformed(ActionEvent e) {
FileWriter fr = null;
try {
fr = new FileWriter("save.txt");
} catch (IOException ex3) {
}
BufferedWriter br = new BufferedWriter(fr);
try {
br.write(txtDisplay.getText());
FileReader fd=new FileReader("save.txt");
BufferedReader bf=new BufferedReader(fd);
System.out.println("bf.read()=" + bf.readLine());
} catch (IOException ex2) {
}
try {
br.close();
} catch (IOException ex1) {
}
try {
fr.close();
} catch (IOException ex) {
}
JOptionPane.showMessageDialog(this, "保存在项目目录下");
this.dispose();

}}


class StuFrm_jButton1_actionAdapter implements ActionListener {
private StuFrm adaptee;
StuFrm_jButton1_actionAdapter(StuFrm adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}


class StuFrm_jButton2_actionAdapter implements ActionListener {
private StuFrm adaptee;
StuFrm_jButton2_actionAdapter(StuFrm adaptee) {
this.adaptee = adaptee;
}

public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值