JAVA+SQL2005 code _jsp Programming for randomly sampled questions

Source: Internet
Author: User
Tags gettext seld stmt
Copy Code code as follows:

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{
The connection to the database.
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 &¤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 ("show 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 selection 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 to produce random numbers of 10-way multiple-choice questions.
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 realizes the choice problem of access random extraction
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 ();
}
}
The For loop implementation here arranges the question number from one to ten.
for (int j=0;j<k.length;j++)
{
System.out.println (k[j]+ "a");//is used to test the same random number produced.
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 selection 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 must be created before executing this program. One of the tables is (Sel_test (Selno,sela,selb,selc,seld,selans)) and the other is (Stu_selans (Sel_no,sel_a,sel_b,sel_c,sel_d,sel_ans) This table is used to save from the table Sel_test

Randomly selected topics in the.
This program is the general idea is: first of all, from the test paper randomly 10 questions, and then save it in another table and its corresponding title number into one to 10. Facilitates the rest of the next operation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.