复制代码 代码如下:
import java.awt.BorderLayout;
import java.util.*;
import java.awt.event.*;
import java.awt.Container;
import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Test extends JFrame {
public static final String DBDRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
public static final String DBURL="jdbc:sqlserver://localhost:1433;DatabaseName=SystemTest;SelectMethod=Cursor";
public static final String DBUSER="sa";
public static final String DBPASSWORD="123";
public static Connection conn=null;
public static Statement stmt=null;
public static Statement stmt1=null;
public static Statement stmt2=null;
public static ResultSet rs=null;
public static ResultSet rs1=null;
public static void main(String args[]) {
try{
//数据库的连接。
Class.forName(DBDRIVER);
conn=DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
stmt= conn.createStatement(ResultSet. TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
stmt1= conn.createStatement(ResultSet. TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
stmt2= conn.createStatement(ResultSet. TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
}catch(Exception e)
{
e.printStackTrace();
}
Test t1=new Test();
t1.sel_save();
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test frame = new Test();
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
/*try{
sql_1="delete from stu_selAns";
stmt=conn.createStatement();
rs=stmt.executeQuery(sql_1);
}
catch(Exception event)
{
}*/
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame
*/
public Test() {
super();
setBounds(100, 100, 500, 386);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("测试");
final Container container = new Container();
container.setLayout(null);
getContentPane().add(container, BorderLayout.CENTER);
label = new JLabel();
label.setBounds(22, 68, 462, 57);
container.add(label);
final JButton nextButton = new JButton();
nextButton.setText("next");
nextButton.setBounds(60, 270, 106, 28);
container.add(nextButton);
nextButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{
int current=Integer.parseInt(label_3.getText());
current+=1;
if(current>0 &¤t<=10)
{
label_3.setText(""+current);
label_3.setVisible(true);
sel_show(current);
}
label_2.setVisible(false);
label_1.setVisible(false);
}
});
button = new JButton();
button.setText("显示答案");
button.setBounds(224, 270, 106, 28);
container.add(button);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{
label_1.setVisible(true);
label_2.setVisible(true);
int current=Integer.parseInt(label_3.getText());
current+=1;
try{
sql="select sel_Ans from stu_selAns where sel_Id="+current;
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
while(rs.next())
{
label_2.setText(rs.getString(1));
}
}catch(Exception e)
{
}
}
});
label_1 = new JLabel();
label_1.setText("正确答案是:");
label_1.setBounds(22, 199, 78, 28);
container.add(label_1);
label_1.setVisible(false);
label_2 = new JLabel();
label_2.setBounds(106, 199, 323, 28);
container.add(label_2);
label_2.setVisible(false);
button_1 = new JButton();
button_1.setText("退出");
button_1.setBounds(349, 270, 106, 28);
container.add(button_1);
button_1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{
dispose();
/*try{
sql_1="delete from stu_selAns";
stmt=conn.createStatement();
rs=stmt.executeQuery(sql_1);
}catch(Exception e)
{
e.printStackTrace();
}*/
}
});
label_3 = new JLabel();
label_3.setBounds(363, 175, 66, 18);
container.add(label_3);
label_3.setVisible(false);
label_3.setText("0");
}
//此方法用于获取选择题的数目。
public int sel_count()
{
int n = 0;
try{
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
Class.forName(DBDRIVER);
conn=DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
sql_1="select COUNT(selId) from sel_test where selId=1";
stmt=conn.createStatement();
rs=stmt.executeQuery(sql_1);
if(rs.next())
{
n=rs.getInt(1);
}
}
catch(Exception e )
{
e.printStackTrace();
}
return n;
}
//此方法用于产生十道选择题的随机数。
public int[] Random()
{
Test st=new Test();
int n=st.sel_count();
int[] numbers=new int[n];
for(int i=0;i<numbers.length;i++)
numbers[i]=i+1;
int[] result=new int[10];
for(int i=0;i<result.length;i++)
{
int r=(int)(Math.random()*n);
result[i]=numbers[r];
numbers[r]=numbers[n-1];
n--;
}
return result;
}
//此方法实现存取随机抽取的选择题
public void sel_save()
{
Test s=new Test();
int k[]=s.Random();
Arrays.sort(k);
for(int i=0;i<k.length;i++)
{
System.out.println(k[i]);
try{
sql_2="select selNo,selInf,selA,selB,selC,selD,selAns from sel_test where selNo="+k[i];
sql=" insert into stu_selAns(sel_No,sel_Inf,sel_A,sel_B,sel_C,sel_D,sel_Ans)"+sql_2;
stmt=conn.createStatement();
stmt1=conn.createStatement();
rs=stmt1.executeQuery(sql);
while(rs.next())
{
stmt1.execute(sql);
}
}catch(Exception e)
{
e.printStackTrace();
}
}
//此处的FOR循环实现将试题的题号从一到十进行排列。
for(int j=0;j<k.length;j++)
{
//System.out.println(k[j]+"a");//用于测试产生的随机数是否相同。
try{
sql="update stu_selAns set sel_Id="+(j+1)+"where sel_No="+k[j];
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}catch(Exception e)
{
e.printStackTrace();
}
}
}
//此方法实现选择题的调用
public void sel_show(int i)
{
try{
sql="select sel_Inf ,sel_Ans from stu_selAns where sel_No="+i;
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
while(rs.next())
{
label.setText(rs.getString(1));
label_2.setText(rs.getString("sel_Ans"));
}
}catch(Exception e)
{
e.printStackTrace();
}
}
private String sql;
public static String sql_1;
private String sql_2;
private JLabel label;
private JLabel label_1;
private JLabel label_2;
private JLabel label_3;
private JButton button; //显示答案的按钮。
private JButton button_1;
}
说明:执行此程序前需先建立两张表。其中一张表是(sel_test(selNo,selA,selB,selC,selD,selAns)),另一张表是(stu_selAns(sel_No,sel_A,sel_B,sel_C,sel_D,sel_Ans) 此张表用于保存从表sel_test
中随机抽取的题目。
此程序是大概思路是:先从题库中随机抽取十道题,然后将其保存在另一张表中并将其相应的题号变为一到十。便于接下来的其他操作。
import java.awt.BorderLayout;
import java.util.*;
import java.awt.event.*;
import java.awt.Container;
import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Test extends JFrame {
public static final String DBDRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
public static final String DBURL="jdbc:sqlserver://localhost:1433;DatabaseName=SystemTest;SelectMethod=Cursor";
public static final String DBUSER="sa";
public static final String DBPASSWORD="123";
public static Connection conn=null;
public static Statement stmt=null;
public static Statement stmt1=null;
public static Statement stmt2=null;
public static ResultSet rs=null;
public static ResultSet rs1=null;
public static void main(String args[]) {
try{
//数据库的连接。
Class.forName(DBDRIVER);
conn=DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
stmt= conn.createStatement(ResultSet. TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
stmt1= conn.createStatement(ResultSet. TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
stmt2= conn.createStatement(ResultSet. TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
}catch(Exception e)
{
e.printStackTrace();
}
Test t1=new Test();
t1.sel_save();
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test frame = new Test();
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
/*try{
sql_1="delete from stu_selAns";
stmt=conn.createStatement();
rs=stmt.executeQuery(sql_1);
}
catch(Exception event)
{
}*/
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame
*/
public Test() {
super();
setBounds(100, 100, 500, 386);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("测试");
final Container container = new Container();
container.setLayout(null);
getContentPane().add(container, BorderLayout.CENTER);
label = new JLabel();
label.setBounds(22, 68, 462, 57);
container.add(label);
final JButton nextButton = new JButton();
nextButton.setText("next");
nextButton.setBounds(60, 270, 106, 28);
container.add(nextButton);
nextButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{
int current=Integer.parseInt(label_3.getText());
current+=1;
if(current>0 &¤t<=10)
{
label_3.setText(""+current);
label_3.setVisible(true);
sel_show(current);
}
label_2.setVisible(false);
label_1.setVisible(false);
}
});
button = new JButton();
button.setText("显示答案");
button.setBounds(224, 270, 106, 28);
container.add(button);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{
label_1.setVisible(true);
label_2.setVisible(true);
int current=Integer.parseInt(label_3.getText());
current+=1;
try{
sql="select sel_Ans from stu_selAns where sel_Id="+current;
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
while(rs.next())
{
label_2.setText(rs.getString(1));
}
}catch(Exception e)
{
}
}
});
label_1 = new JLabel();
label_1.setText("正确答案是:");
label_1.setBounds(22, 199, 78, 28);
container.add(label_1);
label_1.setVisible(false);
label_2 = new JLabel();
label_2.setBounds(106, 199, 323, 28);
container.add(label_2);
label_2.setVisible(false);
button_1 = new JButton();
button_1.setText("退出");
button_1.setBounds(349, 270, 106, 28);
container.add(button_1);
button_1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event)
{
dispose();
/*try{
sql_1="delete from stu_selAns";
stmt=conn.createStatement();
rs=stmt.executeQuery(sql_1);
}catch(Exception e)
{
e.printStackTrace();
}*/
}
});
label_3 = new JLabel();
label_3.setBounds(363, 175, 66, 18);
container.add(label_3);
label_3.setVisible(false);
label_3.setText("0");
}
//此方法用于获取选择题的数目。
public int sel_count()
{
int n = 0;
try{
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
Class.forName(DBDRIVER);
conn=DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
sql_1="select COUNT(selId) from sel_test where selId=1";
stmt=conn.createStatement();
rs=stmt.executeQuery(sql_1);
if(rs.next())
{
n=rs.getInt(1);
}
}
catch(Exception e )
{
e.printStackTrace();
}
return n;
}
//此方法用于产生十道选择题的随机数。
public int[] Random()
{
Test st=new Test();
int n=st.sel_count();
int[] numbers=new int[n];
for(int i=0;i<numbers.length;i++)
numbers[i]=i+1;
int[] result=new int[10];
for(int i=0;i<result.length;i++)
{
int r=(int)(Math.random()*n);
result[i]=numbers[r];
numbers[r]=numbers[n-1];
n--;
}
return result;
}
//此方法实现存取随机抽取的选择题
public void sel_save()
{
Test s=new Test();
int k[]=s.Random();
Arrays.sort(k);
for(int i=0;i<k.length;i++)
{
System.out.println(k[i]);
try{
sql_2="select selNo,selInf,selA,selB,selC,selD,selAns from sel_test where selNo="+k[i];
sql=" insert into stu_selAns(sel_No,sel_Inf,sel_A,sel_B,sel_C,sel_D,sel_Ans)"+sql_2;
stmt=conn.createStatement();
stmt1=conn.createStatement();
rs=stmt1.executeQuery(sql);
while(rs.next())
{
stmt1.execute(sql);
}
}catch(Exception e)
{
e.printStackTrace();
}
}
//此处的FOR循环实现将试题的题号从一到十进行排列。
for(int j=0;j<k.length;j++)
{
//System.out.println(k[j]+"a");//用于测试产生的随机数是否相同。
try{
sql="update stu_selAns set sel_Id="+(j+1)+"where sel_No="+k[j];
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}catch(Exception e)
{
e.printStackTrace();
}
}
}
//此方法实现选择题的调用
public void sel_show(int i)
{
try{
sql="select sel_Inf ,sel_Ans from stu_selAns where sel_No="+i;
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
while(rs.next())
{
label.setText(rs.getString(1));
label_2.setText(rs.getString("sel_Ans"));
}
}catch(Exception e)
{
e.printStackTrace();
}
}
private String sql;
public static String sql_1;
private String sql_2;
private JLabel label;
private JLabel label_1;
private JLabel label_2;
private JLabel label_3;
private JButton button; //显示答案的按钮。
private JButton button_1;
}
说明:执行此程序前需先建立两张表。其中一张表是(sel_test(selNo,selA,selB,selC,selD,selAns)),另一张表是(stu_selAns(sel_No,sel_A,sel_B,sel_C,sel_D,sel_Ans) 此张表用于保存从表sel_test
中随机抽取的题目。
此程序是大概思路是:先从题库中随机抽取十道题,然后将其保存在另一张表中并将其相应的题号变为一到十。便于接下来的其他操作。
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年11月23日
2024年11月23日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]