package test;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.sql.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.*;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class testFrom1 extends JFrame {
JPanel contentPane;
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JTextField jt1 = new JTextField();
JButton jb1 = new JButton();
JTextArea ja1 = new JTextArea();
JLabel jLabel3 = new JLabel();
JLabel jLabel4 = new JLabel();
JTextArea ja2 = new JTextArea();
JLabel jLabel5 = new JLabel();
JTextArea ja3 = new JTextArea();
JLabel jLabel6 = new JLabel();
JTextArea ja4 = new JTextArea();
JLabel jLabel7 = new JLabel();
JTextArea ja5 = new JTextArea();
JLabel jLabel8 = new JLabel();
JTextField jt2 = new JTextField();
JButton jb2 = new JButton();
JButton jb3 = new JButton();
public testFrom1() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(406, 345));
setTitle("修改试题");
jLabel1.setFont(new java.awt.Font("隶书", Font.BOLD, 18));
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setText("试 题 修 改");
jLabel1.setBounds(new Rectangle(97, 11, 210, 20));
jLabel2.setText("请输入题号:");
jLabel2.setBounds(new Rectangle(22, 40, 81, 21));
jt1.setBounds(new Rectangle(105, 41, 60, 19));
jb1.setBounds(new Rectangle(241, 39, 89, 20));
jb1.setText("查找试题");
jb1.addActionListener(new testFrom1_jb1_actionAdapter(this));
ja1.setBounds(new Rectangle(105, 73, 245, 42));
jLabel3.setText("题干");
jLabel3.setBounds(new Rectangle(22, 77, 63, 17));
jLabel4.setText("选项A");
jLabel4.setBounds(new Rectangle(22, 125, 65, 18));
ja2.setBounds(new Rectangle(104, 124, 245, 20));
jLabel5.setText("选项B");
jLabel5.setBounds(new Rectangle(22, 156, 52, 19));
ja3.setBounds(new Rectangle(105, 153, 245, 23));
jLabel6.setText("选项C");
jLabel6.setBounds(new Rectangle(23, 187, 51, 19));
ja4.setBounds(new Rectangle(102, 184, 248, 25));
jLabel7.setText("选项D");
jLabel7.setBounds(new Rectangle(23, 218, 54, 21));
ja5.setBounds(new Rectangle(104, 216, 248, 26));
jLabel8.setText("答案:");
jLabel8.setBounds(new Rectangle(25, 251, 54, 20));
jt2.setBounds(new Rectangle(104, 251, 60, 21));
jb2.setBounds(new Rectangle(123, 281, 83, 23));
jb2.setText("确认修改");
jb2.addActionListener(new testFrom1_jButton2_actionAdapter(this));
jb3.setBounds(new Rectangle(246, 281, 76, 22));
jb3.setText("退出");
jb3.addActionListener(new testFrom1_jb3_actionAdapter(this));
contentPane.add(jLabel1);
contentPane.add(ja1);
contentPane.add(jLabel3);
contentPane.add(jLabel2);
contentPane.add(jLabel4);
contentPane.add(ja2);
contentPane.add(jLabel5);
contentPane.add(ja3);
contentPane.add(jLabel6);
contentPane.add(ja4);
contentPane.add(jLabel7);
contentPane.add(ja5);
contentPane.add(jLabel8);
contentPane.add(jt2);
contentPane.add(jb2);
contentPane.add(jb3);
contentPane.add(jt1);
contentPane.add(jb1);
}
//实现修改功能
public void jb2_actionPerformed(ActionEvent e) {
String tihao=jt1.getText();//声明变量获取输入的值
Integer ID=Integer.valueOf(tihao);
String timu=ja1.getText();
String opA=ja2.getText();
String opB=ja3.getText();
String opC=ja4.getText();
String opD=ja5.getText();
String answers=jt2.getText().trim();
Connection con=null;
Statement s1=null;
ResultSet rs1=null;
try
{
con=SearchDB.link(); //连接数据库
s1=con.createStatement();
rs1=s1.executeQuery("select * from Question where QuestionID='"+ID+"'"); //查找对应的题号
if(rs1.next())
{
s1.executeUpdate("update Question set Content='"+timu+"' where QuestionID='"+ID+"'"); //更新相应的记录
s1.executeUpdate("update Question set OptionA='"+opA+"' where QuestionID='"+ID+"'");
s1.executeUpdate("update Question set OptionB='"+opB+"' where QuestionID='"+ID+"'");
s1.executeUpdate("update Question set OptionC='"+opC+"' where QuestionID='"+ID+"'");
s1.executeUpdate("update Question set OptionD='"+opD+"' where QuestionID='"+ID+"'");
s1.executeUpdate("update Question set Answers='"+answers+"' where QuestionID='"+ID+"'");
JOptionPane.showMessageDialog(null,""+ID+"号试题修改完成!"); //完成并获取题号
con.close();
s1.close();
}
}
catch(Exception co)
{
JOptionPane.showMessageDialog(null,co.getMessage());
}
}
//查找功能
public void jb1_actionPerformed(ActionEvent e) {
String ID=jt1.getText();
Connection con=null;
Statement s=null;
ResultSet rs=null;
try
{
if(ID.length()==0)
{
JOptionPane.showMessageDialog(null,"请输入题号!");
}
else
{
con = SearchDB.link();
s = con.createStatement();
rs = s.executeQuery("select * from Question where QuestionID='" + ID + "'"); //查询题号
if (rs.next())
{
String tigan = rs.getString("Content"); //声明变量获取数据库对应的列值
String A = rs.getString("OptionA");
String B = rs.getString("OptionB");
String C = rs.getString("OptionC");
String D = rs.getString("OptionD");
String daan = rs.getString("Answers");
ja1.setText(tigan); //从数据库中提取数据显示在文本筐内
ja2.setText(A);
ja3.setText(B);
ja4.setText(C);
ja5.setText(D);
jt2.setText(daan);
con.close();
s.close();
}
else
{
JOptionPane.showMessageDialog(null,"没有此题号!");
}
}
}
catch(SQLException ce)
{
JOptionPane.showMessageDialog(null,ce.getMessage());
}
}
public void jb3_actionPerformed(ActionEvent e) {
System.exit(0); //退出功能
}
}
class testFrom1_jb3_actionAdapter implements ActionListener {
private testFrom1 adaptee;
testFrom1_