java+sql2005 code for randomly selected test questions

  • 2020-05-19 05:30:28
  • OfStack

 
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{ 
// Database connection.  
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(" test "); 
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 & because 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(" According to the answer "); 
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(" The correct answer is: "); 
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(" exit "); 
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"); 
} 
// This method is used to get the number of multiple choice questions.  
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; 
} 
// This method is used for generation 10 Random number of a multiple choice question.  
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; 
} 
// This method enables access to randomly selected multiple choice questions  
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(); 
} 
} 
// Here the FOR The cycle will be realized from the question number 1 to 10 Arrange them.  
for(int j=0;j<k.length;j++) 
{ 
//System.out.println(k[j]+"a");// Used to test whether the generated random Numbers are the same.  
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(); 
} 
} 
} 
// This method implements the call to the multiple choice question  
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; // The button that displays the answer.  
private JButton button_1; 
} 
 Note: two tables should be created before executing this program. Among them 1 Table is (sel_test(selNo,selA,selB,selC,selD,selAns)), On the other 1 Table is (stu_selAns(sel_No,sel_A,sel_B,sel_C,sel_D,sel_Ans)  This table is used to save slave tables sel_test 

A randomly selected problem in.
The general idea of this program is: first, randomly select 10 questions from the question bank, then save them in another table and change the corresponding question number into 1 to 10. Facilitate other operations that follow.

Related articles: