主頁 > 知識庫 > java+sql2005 隨機(jī)抽取試題的代碼

java+sql2005 隨機(jī)抽取試題的代碼

熱門標(biāo)簽:甘肅銷售電銷機(jī)器人公司 智能電話機(jī)器人銷售話術(shù) 外呼直播語音系統(tǒng) 四川正規(guī)外呼系統(tǒng)軟件 山東ai外呼電銷機(jī)器人好用嗎 地圖標(biāo)注專業(yè)和非專業(yè) 湖北地圖標(biāo)注公司 汝南縣地圖標(biāo)注app 福建電銷貓機(jī)器人收費
復(fù)制代碼 代碼如下:

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{
//數(shù)據(jù)庫的連接。
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");
}
//此方法用于獲取選擇題的數(shù)目。
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;
}
//此方法用于產(chǎn)生十道選擇題的隨機(jī)數(shù)。
public int[] Random()
{
Test st=new Test();
int n=st.sel_count();
int[] numbers=new int[n];
for(int i=0;inumbers.length;i++)
numbers[i]=i+1;
int[] result=new int[10];
for(int i=0;iresult.length;i++)
{
int r=(int)(Math.random()*n);
result[i]=numbers[r];
numbers[r]=numbers[n-1];
n--;
}
return result;
}
//此方法實現(xiàn)存取隨機(jī)抽取的選擇題
public void sel_save()
{
Test s=new Test();
int k[]=s.Random();
Arrays.sort(k);
for(int i=0;ik.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循環(huán)實現(xiàn)將試題的題號從一到十進(jìn)行排列。
for(int j=0;jk.length;j++)
{
//System.out.println(k[j]+"a");//用于測試產(chǎn)生的隨機(jī)數(shù)是否相同。
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();
}
}
}
//此方法實現(xiàn)選擇題的調(diào)用
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;
}
說明:執(zhí)行此程序前需先建立兩張表。其中一張表是(sel_test(selNo,selA,selB,selC,selD,selAns)),另一張表是(stu_selAns(sel_No,sel_A,sel_B,sel_C,sel_D,sel_Ans) 此張表用于保存從表sel_test

中隨機(jī)抽取的題目。
此程序是大概思路是:先從題庫中隨機(jī)抽取十道題,然后將其保存在另一張表中并將其相應(yīng)的題號變?yōu)橐坏绞?。便于接下來的其他操作?
您可能感興趣的文章:
  • java利用數(shù)組隨機(jī)抽取幸運觀眾
  • java隨機(jī)抽取指定范圍不重復(fù)的數(shù)字
  • java隨機(jī)抽取指定范圍內(nèi)不重復(fù)的n個數(shù)
  • Java利用數(shù)組隨機(jī)抽取幸運觀眾如何實現(xiàn)
  • java實現(xiàn)隨機(jī)抽取獎品工具類

標(biāo)簽:吳忠 臨沂 黔東 南充 昌都 肇慶 梅州 白銀

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《java+sql2005 隨機(jī)抽取試題的代碼》,本文關(guān)鍵詞  java+sql2005,隨機(jī),抽取,試,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《java+sql2005 隨機(jī)抽取試題的代碼》相關(guān)的同類信息!
  • 本頁收集關(guān)于java+sql2005 隨機(jī)抽取試題的代碼的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章