📄 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("数学");
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);
// 数据库连接
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();
}
});
// 按钮分别监听
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("文件不存在,正在创建...");
if (f.createNewFile()) {
System.out.println("文件创建成功!");
} else {
System.out.println("文件创建失败!");
}
}
BufferedReader input = new BufferedReader(new FileReader(f));
while ((s = input.readLine()) != null) {
s1 += s + "\n";
}
System.out.println("文件内容:" + 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();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -