萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> 數據庫綜合 >> java+sql2005 隨機抽取試題的代碼

java+sql2005 隨機抽取試題的代碼

 

今天小編為大家帶來的是java+sql2005 隨機抽取試題的代碼 ,這裡詳細的介紹了怎麼實現目標,希望能對大家有所幫助。

先從題庫中隨機抽取十道題,然後將其保存在另一張表中並將其相應的題號變為一到十。便於接下來的其他操作。

 代碼如下:


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)
{
}
}
});

copyright © 萬盛學電腦網 all rights reserved