📄 servletapp.java
字号:
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.text.*;
public class ServletApp extends HttpServlet {
public void init(){}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException ,ServletException{
String tf1Str=request.getParameter("num");
String tf2Str=request.getParameter("code").trim();
int num=Integer.parseInt(tf1Str);
String returnStr;
connectDb t=new connectDb(num);
if(tf1Str.length()==10) {
t.run();
if( (t.getcode().trim().equals(tf2Str))) {
returnStr=" Hello, "+t.getname()+" Your Score is "+"\n"+
" Chinese : "+t.getchi()+"\n" +
" Mathmatics : "+t.getmath()+"\n"+
" English : "+t.geteng()+"\n"+
" Physics: "+t.getphy()+"\n"+
" Chemics: "+t.getche()+"\n"+
" Biology: "+t.getbio();
} else { returnStr="false";}
}
else
{ returnStr="false";}
response.setContentType("text/html;charset=gb2312");
response.setContentLength(returnStr.length());
System.out.println("\n"+"\t"+"**************************");
System.out.println("\t"+"服务器的信息如下:");
System.out.println("\n"+"\t"+"收到的参数为 : number: "+tf1Str+" code :" +tf2Str);
System.out.println("\t"+"从数据库获得的密码 :" +t.getcode());
System.out.println("\t"+"返回的数据为:"+returnStr);
System.out.println("\t"+"返回的数据长度为: "+returnStr.length());
System.out.println("\n"+"\t"+"**************************");
PrintWriter out=response.getWriter();
out.println(returnStr);
out.close();
out.flush();
}
public void doPost
(HttpServletRequest request,HttpServletResponse response) throws
ServletException, IOException{
doGet(request, response);
}
public void destroy(){}
}
class connectDb {
private int i;
private int num=0;
private String name,code;
private int chi,math,eng,phy,che,bio;
connectDb( int i){
this.i=i;
}
public String getcode(){
return this.code;
}
public String getname(){
return this.name;
}
public int getchi(){
return this.chi;
}
public int getmath(){
return this.math;
}
public int geteng(){
return this.eng;
}
public int getphy(){
return this.phy;
}
public int getche(){
return this.che;
}
public int getbio(){
return this.bio;
}
public void run(){
try
{
// 加载JDBC-ODBC桥驱动程序
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// 通过数据源与数据库建立起连接
Connection c
=DriverManager.getConnection(
"jdbc:odbc:studentSqlDatabase","sa",null);
Statement s=c.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
// 获取数据库表数据
ResultSet r=s.executeQuery("select * from 学生成绩 where 学号="+i);
try
{
r.next();
this.num=r.getInt("学号");
this.name=r.getString("姓名");
this.code=r.getString("密码");
this.chi=r.getInt("语文");
this.math=r.getInt("数学");
this.eng=r.getInt("英语");
this.phy=r.getInt("物理");
this.che=r.getInt("化学");
this.bio=r.getInt("生物");
}
catch (Exception e)
{
System.err.println("异常: " + e.getMessage( ));
} // try-catch结构结束
s.close( );
c.close( );
}
catch (Exception e)
{
System.err.println("异常: " + e.getMessage( ));
} // try-catch结构结束
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -