search
HomeJavajavaTutorialjava+sql2005 code for randomly selecting test questions

Randomly selected questions from

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

.
The general idea of ​​this program is: first randomly select ten questions from the question bank, then save them in another table and change their corresponding question numbers from one to ten. to facilitate other subsequent operations.

For more articles related to the code of java+sql2005 randomly selected test questions, please pay attention to the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
When Tomcat loads Spring-Web modules, does the SPI mechanism really destroy the visibility principle of Java class loaders?When Tomcat loads Spring-Web modules, does the SPI mechanism really destroy the visibility principle of Java class loaders?Apr 19, 2025 pm 02:18 PM

Analysis of class loading behavior of SPI mechanism when Tomcat loads Spring-Web modules. Tomcat is used to discover and use the Servle provided by Spring-Web when loading Spring-Web modules...

How to choose Java project management tools when learning back-end development?How to choose Java project management tools when learning back-end development?Apr 19, 2025 pm 02:15 PM

Confused with choosing Java project management tools for beginners. For those who are just beginning to learn backend development, choosing the right project management tools is crucial...

How to implement monitoring events? A comprehensive analysis from principle to practiceHow to implement monitoring events? A comprehensive analysis from principle to practiceApr 19, 2025 pm 02:12 PM

Regarding the implementation principles and methods of listening events In programming, listening to events is a common requirement, especially listening for changes in a certain value. Many people may...

How to dynamically modify the savePath parameter of @Excel annotation in easypoi when project starts in Java?How to dynamically modify the savePath parameter of @Excel annotation in easypoi when project starts in Java?Apr 19, 2025 pm 02:09 PM

How to dynamically configure the parameters of entity class annotations in Java During the development process, we often encounter the need to dynamically configure the annotation parameters according to different environments...

Why does the Python script not be found when submitting a PyFlink job on YARN?Why does the Python script not be found when submitting a PyFlink job on YARN?Apr 19, 2025 pm 02:06 PM

Analysis of the reason why Python script cannot be found when submitting a PyFlink job on YARN When you try to submit a PyFlink job through YARN, you may encounter...

What should I do if a third-party interface is called in Spring Boot project, and the field name case and getter method are inconsistent, resulting in data transmission failure?What should I do if a third-party interface is called in Spring Boot project, and the field name case and getter method are inconsistent, resulting in data transmission failure?Apr 19, 2025 pm 02:03 PM

The difficulties encountered when calling third-party interfaces to transmit data in SpringBoot project will be used for a Spring...

How to convert names to numbers to implement sorting within groups?How to convert names to numbers to implement sorting within groups?Apr 19, 2025 pm 01:57 PM

How to convert names to numbers to implement sorting within groups? When sorting users in groups, it is often necessary to convert the user's name into numbers so that it can be different...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.