import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class QueryScoreServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public QueryScoreServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String s1 = request.getParameter("s1");
String conText = request.getParameter("conText");
String queryCon = "";
if(s1!=null && s1!=""){
if(s1.equals("0")){
queryCon = "chinese = '"+conText+"'";
}
if(s1.equals("1")){
queryCon = "math = '"+conText+"'";
}
if(s1.equals("2")){
queryCon = "english = '"+conText+"'";
}
if(s1.equals("3")){
queryCon = "computer = '"+conText+"'";
}
if(s1.equals("4")){
queryCon = "total = '"+conText+"'";
}
if(s1.equals("5")){
queryCon = "uid = '"+conText+"'";
}
}
String strReturn = "";
String xmlText = "";
int i=1;
Connection con = null;
try {
Class.forName("com.ibm.db2.jcc.DB2Driver");
con = DriverManager.getConnection("jdbc:db2://localhost:50000/sample","wyr","123");
if(con != null ) {
PreparedStatement statement = con.prepareStatement("select * from stu where "+queryCon);
ResultSet rs = statement.executeQuery();
List list = new ArrayList();
strReturn ="[";
while(rs.next()){
if(i==1){
strReturn =strReturn+ "{'uid':'"+rs.getInt(1)+"','chinese':'"+rs.getDouble(2)+"','math':'"+rs.getDouble(3)+"','english':'"+rs.getDouble(4)+"','computer':'"+rs.getDouble(5)+"','total':'"+rs.getDouble(6)+"'}";
}else{
strReturn = strReturn + "," + "{'uid':'"+rs.getInt(1)+"','chinese':'"+rs.getDouble(2)+"','math':'"+rs.getDouble(3)+"','english':'"+rs.getDouble(4)+"','computer':'"+rs.getDouble(5)+"','total':'"+rs.getDouble(6)+"'}";
}
i++;
}
strReturn = strReturn+"]";
rs.close();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if(con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
if(i==1){
strReturn = "error";
}
xmlText = "<?xml version=\"1.0\" encoding=\"GBK\"?>" //字符编码GBK
+ "<root>"
+ strReturn
+ "</root>";
out.write(xmlText);
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
- 1
- 2
前往页