?? daima.txt
字號:
package pg1;
import java.awt.*;
import javax.swing.*;
import pg1.Jd;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.sql.*;
public class Jd extends JFrame implements ActionListener
{
JButton a,b;
JLabel lb1,lb2,lb3,lb4;
JComboBox k,x,y,z;
String kName;
Connection con=null;
Statement stmt=null;
String url="jdbc:odbc:Question";
/**
*
*/
private static final long serialVersionUID = 6551465348039211771L;
Jd()
{ // 界面布置
super("試卷組題");
setSize(300,300);
setVisible(true);
Container ctp=getContentPane();
a=new JButton("組題");
b=new JButton("退出");
lb1=new JLabel("科目選擇");
lb2=new JLabel("選擇題");
lb3=new JLabel("填空題");
lb4=new JLabel("判斷題");
k=new JComboBox();
k.addItem("語文");
k.addItem("數(shù)學(xué)");
k.addItem("英語");
x=new JComboBox();
for(int i=0;i<=50;i++){
x.addItem(i);
}
y=new JComboBox();
for(int i=0;i<=50;i++){
y.addItem(i);
}
z=new JComboBox();
for(int i=0;i<=50;i++){
z.addItem(i);
}
ctp.setLayout(new GridLayout(5,2));
ctp.add(lb1);ctp.add(k);
ctp.add(lb2);ctp.add(x);
ctp.add(lb3);ctp.add(y);
ctp.add(lb4);ctp.add(z);
ctp.add(a);ctp.add(b);
// 數(shù)據(jù)庫連接
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");}
catch(ClassNotFoundException e)
{System.out.println(e.getMessage());}
try{
con=DriverManager.getConnection(url);
stmt=con.createStatement();}
catch(SQLException ee){System.out.println(ee.getMessage());}
k.addItemListener(new ItemListener(){
public void itemStateChanged(ItemEvent e){
kName = k.getSelectedItem().toString();
}
});
// 按鈕分別監(jiān)聽
a.addActionListener(this);
b.addActionListener(this);
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);}
});
}
public void actionPerformed(ActionEvent e)
{
JButton btn=(JButton) e.getSource();
if(btn==b)
System.exit(0);
else if(btn==a)
{try{zuti();}
catch(SQLException ee)
{System.out.println(ee.getMessage());}
}
}
public void zuti() throws SQLException { ResultSet rs = stmt.executeQuery(
"SELECT TOP 2 QUE "+
"FROM Question "+
"WHERE (OBJ = 2) AND (TYPE = 1) "+
"ORDER BY NEWID()");
while(rs.next()){
String questionName=rs.getString(1);
write("c:\123.txt",questionName);
}
}
public void write(String path, String content){
String s = new String();
String s1 = new String();
try {
File f = new File(path);
if (f.exists()) {
System.out.println("文件存在");
} else {
System.out.println("文件不存在,正在創(chuàng)建...");
if (f.createNewFile()) {
System.out.println("文件創(chuàng)建成功!");
} else {
System.out.println("文件創(chuàng)建失敗!");
}
}
BufferedReader input = new BufferedReader(new FileReader(f));
while ((s = input.readLine()) != null) {
s1 += s + "\n";
}
System.out.println("文件內(nèi)容:" + s1);
input.close();
s1 += content;
BufferedWriter output = new BufferedWriter(new FileWriter(f));
output.write(s1);
output.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args)
{
new Jd();
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -