java+sql2005 随机抽取试题的代码
程序员文章站
2023-11-24 09:31:52
复制代码 代码如下: import java.awt.borderlayout; import java.util.*; import java.awt.event.*;...
复制代码 代码如下:
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
中随机抽取的题目。
此程序是大概思路是:先从题库中随机抽取十道题,然后将其保存在另一张表中并将其相应的题号变为一到十。便于接下来的其他操作。
上一篇: ProE怎么创建创建双向边界混合曲面?