📄
字号:
try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e){}
}
public void setKeyword(String s)
{keyword=s;
try{byte b[]=keyword.getBytes("ISO-8859-1");
keyword=new String(b);
}
catch(Exception e)
{ }
}
public StringBuffer byKeywordInquire()
{ String number,xingming;
Connection con=null;
Statement sql=null;
ResultSet rs=null;
StringBuffer buffer=new StringBuffer();
int math,english,physics;
try{ con=DriverManager.getConnection("jdbc:odbc:sun","sa","");
sql=con.createStatement();
String condition="SELECT * FROM students WHERE 学号 = "+"'"+keyword+"'";
rs=sql.executeQuery(condition);
buffer.append("<Table Border>");
buffer.append("<TR>");
buffer.append("<TH width=100>"+"学号");
buffer.append("<TH width=100>"+"姓名");
buffer.append("<TH width=50>"+"数学成绩");
buffer.append("<TH width=50>"+"英语成绩");
buffer.append("<TH width=50>"+"物理成绩");
buffer.append("</TR>");
while(rs.next())
{ buffer.append("<TR>");
number=rs.getString(1);
buffer.append("<TD >"+number+"</TD>");
xingming=rs.getString(2);
buffer.append("<TD >"+xingming+"</TD>");
math=rs.getInt("数学成绩");
buffer.append("<TD >"+math+"</TD>");
english=rs.getInt("英语成绩");
buffer.append("<TD >"+english+"</TD>");
physics=rs.getInt("物理成绩");
buffer.append("<TD >"+physics+"</TD>");
buffer.append("</TR>") ;
}
buffer.append("</Table>");
con.close();
return buffer;
}
catch(SQLException e)
{return new StringBuffer("无法建立查询");
}
}
}
通过主关键字查询记录的页面(效果如图6.28所示)
byKeyword.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="DataBaseInquire" %>
<HTML>
<BODY ><Font size=1>
<Font size=1>
<FORM action="byKeyword.jsp" Method="post" 1>
<P>成绩查询
<P>输入学号:
<Input type=text name="keyword">
<Input type=submit name="g" value="提交">
</Form>
<jsp:useBean id="database" class="DataBaseInquire" scope="request" >
</jsp:useBean>
<jsp:setProperty name= "database" property="keyword" param="keyword" />
<P>查询到记录:<BR>
<% StringBuffer b=database.byKeywordInquire();
%>
<%=b%>
</Body>
</HTML>
GuessNumber.java:
public class GuessNumber
{ int answer=0, //实际答案。
guessNumber=0, //客户猜测的数。
guessCount=0; //客户猜到正确答案之前所用的次数。
String result=null;
public void setAnswer(int n)
{ answer=n;
guessCount=0;
}
public int getAnswer()
{return answer;
}
public void setGuessNumber(int n)
{ guessNumber=n;
guessCount++;
if(guessNumber==answer)
result="恭喜,猜对了";
else if(guessNumber>answer)
result="猜大了";
else if(guessNumber<answer)
result="猜小了";
}
public int getGuessNumber()
{return guessNumber;
}
public int getGuessCount()
{return guessCount;
}
public String getResult()
{return result;
}
}
获取一个随机数页面(效果如图6.29所示)
getNumber.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="GuessNumber" %>
<HTML>
<BODY>
<% int n=(int)(Math.random()*100)+1;%>
<jsp:useBean id="guess" class="GuessNumber" scope="session" >
</jsp:useBean>
<jsp:setProperty name= "guess" property="answer" value="<%=n%>" />
<p>随机给你一个1到100之间的数,请猜测这个数是多少?
<% String str=response.encodeRedirectURL("guess.jsp");
%>
<Form action="<%=str%>" method=post >
<BR>输入你的猜测 <Input type=text name="guessNumber">
<Input type=submit value="提交">
</FORM>
</BODy>
猜数页面(效果如图6.30所示)
guess.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="GuessNumber" %>
<HTML>
<BODY>
<jsp:useBean id="guess" class="GuessNumber" scope="session" >
</jsp:useBean>
<jsp:setProperty name= "guess" property="guessNumber" param="guessNumber" />
<BR>
<jsp:getProperty name= "guess" property="result" />
<br>这是第
<jsp:getProperty name= "guess" property="guessCount" />
猜
<BR>你给出的数是
<jsp:getProperty name= "guess" property="guessNumber" />
<% String str=response.encodeRedirectURL("guess.jsp");
%>
<Form action="<%=str%>" method=post >
<BR>再输入你的猜测 <Input type=text name="guessNumber">
<Input type=submit value="提交">
</FORM>
<% String str1=response.encodeRedirectURL("getNumber.jsp");
%>
<BR><FORM action="<%=str1%>" method="post" name="f">
<Input type="submit" value="重新玩">
</FORM>
</BODy>
</HTML>
Test.java:
import java.io.*;
public class Test
{ String filename="", //存放考题文件名字的字符串。
correctAnswer="", //存放正确答案的字符串。
//存放试题和客户提交的答案的字符串:
testContent="" ,
selection="" ;
int score=0; //考试者的得分。
File f=null;
FileReader in=null;
BufferedReader buffer=null;
public void setFilename(String name)
{ filename=name;
//当选择了新的考题文件后,将用户的答案字符串清空,
//将分数设为0:
score=0;
selection="";
//读取试题文件的第一行:标准答案
try { f=new File("F:/2000",filename);
in=new FileReader(f);
buffer=new BufferedReader(in);
correctAnswer=(buffer.readLine()).trim();//读取一行,去掉前后空格。
}
catch(Exception e)
{testContent="没有选择试题";
}
}
public String getFilename()
{ return filename;
}
public String getTestContent() //获取试题的内容
{ try { String s=null;
StringBuffer temp=new StringBuffer();
if(buffer!=null) //如果客户选择了试题文件,buffer就不是空对象。
{while((s=buffer.readLine())!=null) //继续取某个试题。
{ if(s.startsWith("**")) //试题结束标志。
break;
//为了能显示原始的HTML或JSP文件考题内容需使用回压流技术:
s=getString(s+"\n");
temp.append(s);
if(s.startsWith("endend")) //试题文件结束标志。
{ in.close(); //关闭和文件的连接。
buffer.close();
}
testContent=new String(temp);
}
}
else
{ testContent=new String("没有选择试题");
}
}
catch(Exception e)
{ testContent="试题无内容,考试结束了!!";
}
return testContent;
}
public void setSelection(String s)
{
selection=selection+s; //将用户提交的答案依次尾加到selection。
}
public int getScore()
{ int i=selection.length()-1; //客户提交的第i题答案在selection中的位置。
int m=correctAnswer.length();
if(i<=m-1)
{ try{ //判定分数:
if(selection.charAt(i)==correctAnswer.charAt(i))
score++;
}
catch(StringIndexOutOfBoundsException e)
{ i=0;
}
}
return score;
}
//对字符串进行处理的方法:
public String getString(String content)
{try{ StringReader in=new StringReader(content) ;//指向字符串的字符流。
PushbackReader push=new PushbackReader(in); //回压流
StringBuffer stringbuffer=new StringBuffer(); //缓冲字符串对象。
int c;
char b[]=new char[1];
while ( (c=push.read(b,0,1))!=-1)//读取1个字符放入字符数组b。
{ String s=new String(b);
if(s.equals("<")) //回压的条件
{ push.unread('&');
push.read(b,0,1); //push读出被回压的字符字节,放入数组b.
stringbuffer.append(new String(b));
push.unread('L');
push.read(b,0,1); //push读出被回压的字符字节,放入数组b.
stringbuffer.append(new String(b));
push.unread('T');
push.read(b,0,1); //push读出被回压的字符字节,放入数组b.
stringbuffer.append(new String(b));
}
else if(s.equals(">")) //回压的条件
{ push.unread('&');
push.read(b,0,1); //push读出被回压的字符字节,放入数组b.
stringbuffer.append(new String(b));
push.unread('G');
push.read(b,0,1); //push读出被回压的字符字节,放入数组b.
stringbuffer.append(new String(b));
push.unread('T');
push.read(b,0,1); //push读出被回压的字符字节,放入数组b.
stringbuffer.append(new String(b));
}
else if(s.equals("\n"))
{ stringbuffer.append("<BR>");
}
else
{ stringbuffer.append(s);
}
}
push.close();
in.close();
return new String(stringbuffer); //返回处理后的字符串。
}
catch(IOException e)
{return content=new String("不能读取内容");
}
}
}
考试页面(效果如图6.32所示)
standardTest.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="Test" %>
<HTML>
<BODY bgcolor=cyan> <Font size=1 >
<jsp:useBean id="test" class="Test" scope="session" >
</jsp:useBean>
<P>选择试题文件
<% String str=response.encodeRedirectURL("standardTest.jsp");
%>
<Form action="<%=str%>" method="post">
<Select name="filename" value="A.txt">
<Option value="A.txt" > A.txt
<Option value="B.txt"> B.txt
<Option value="C.txt"> C.txt
<Option value="D.txt"> D.txt
<Option value="E.txt"> E.txt
</Select>
<Input type="submit" name="sub" value="确定">
</FORM>
<%-- 过beans设置文件的名字 ,下面的标签只有提交了相应的表单才被执行:--%>
<jsp:setProperty name="test" property="filename" param="filename"/>
<P>你选择的试题是:
<jsp:getProperty name= "test" property="filename" />
<%-- 通过beans获取试题的内容--%>
<BR>
<jsp:getProperty name="test" property="testContent" />
<BR>请选择:
<BR><Form action="<%=str%>" method="post">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -