发布于 2016-08-16 12:04:29 | 105 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的JSP由浅入深,程序狗速度看过来!

JSP JAVA公司的WEB开发编程语言

JSP全名为Java Server Pages - java服务器页面是由Sun Microsystems公司倡导、许多公司参与一起建立的一种动态网页技术标准。JSP技术有点类似ASP技术,它是在传统的网页HTML(标准通用标记语言的子集)文件(*.htm,*.html)中插入Java程序段(Scriptlet)和JSP标记(tag),从而形成JSP文件,后缀名为(*.jsp)。


先从题库中随机抽取十道题,然后将其保存在另一张表中并将其相应的题号变为一到十。便于接下来的其他操作。
 
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 

中随机抽取的题目。
此程序是大概思路是:先从题库中随机抽取十道题,然后将其保存在另一张表中并将其相应的题号变为一到十。便于接下来的其他操作。


相关阅读 :
java+sql2005 随机抽取试题的代码
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务