各位大佬,最近用Jsp+servlet写教务管理系统,其中servlet端有一个问题始终无法解决
严重: Servlet.service() for servlet [com.jwglxt.data.searchStudent] in context with path [/jwglxt] threw exception
java.lang.NullPointerException
目的是根据从前台传来的查询类型和查询关键词在数据库中进行检索,目前前后端数据传递正常,NullPointerException这个问题始终得不到解决,尝试了很多办法都不行,恳请各位大佬施以援手!
代码如下:
package com.jwglxt.data;
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.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
//思路:绑定一个弹出框在“搜索”按钮上,弹出框里放置一个datagrid,url为searchStudent
@WebServlet("/searchStudent")
public class searchStudent extends HttpServlet {
@SuppressWarnings({ "null", "null", "null" })
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置编码格式
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/json; charset=utf-8");
//获取学生信息主码-学号(sno)
String name=request.getParameter("name");
System.out.println("搜索类型:"+name);
String value=request.getParameter("value");
System.out.println("搜索值:"+value);
//姑且赋值
/*String name="sno";
String value="990101";*/
//连接数据库
Connection connection=null;
String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";//SQL数据库引擎
String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=tjl";//数据源 !!!注意若出现加载或者连接数据库失败一般是这里出现问题
String Name="sa";
String Pwd="19990713";
ResultSet rs=null;
String sql="sql";
//定义JSON数组,用于保存从数据库查出来的数据
JSONArray array = new JSONArray();
try {
Class.forName(driverName);
connection=DriverManager.getConnection(dbURL,Name,Pwd);
System.out.println("搜索学生:连接数据库成功");
} catch (ClassNotFoundException e2) {
e2.printStackTrace();
}catch (SQLException e) {
e.printStackTrace();
}
//处理搜索类型 尝试switch语句
/*switch(name) {
case "sno":{
try {
sql="select * from student where sno=?";
connection.prepareStatement(sql);
pstmt.setString(1,value);
//rs=pstmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
};break;
}//case
}//switch
*/
//处理搜索类型 尝试If语句
if(name!=null) {
if("sno".equals(name)){
try {
//仅构造SQL语句,暂不运行
sql="select * from student where sno=?";
PreparedStatement pstmt=connection.prepareStatement(sql);
pstmt.setString(1,value);
rs = pstmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
else if("sname".equals(name)){
try {
//仅构造SQL语句,暂不运行
sql="select * from student where sname=?";
PreparedStatement pstmt=connection.prepareStatement(sql);
pstmt.setString(1,value);
rs = pstmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
else if("sex".equals(name)){
try {
//仅构造SQL语句,暂不运行
sql="select * from student where sex=?";
PreparedStatement pstmt=connection.prepareStatement(sql);
pstmt.setString(1,value);
rs = pstmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
else if("dno".equals(name)){
try {
//仅构造SQL语句,暂不运行
sql="select * from student where dno=?";
PreparedStatement pstmt=connection.prepareStatement(sql);
pstmt.setString(1,value);
rs = pstmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
else {
try {
//仅构造SQL语句,暂不运行
sql="select * from student where dormno=?";
PreparedStatement pstmt=connection.prepareStatement(sql);
pstmt.setString(1,value);
rs = pstmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
System.out.println(sql);
}
try {
// 获取列数
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
while (rs.next()) {
JSONObject jsonObj = new JSONObject();
// 遍历每一列
for (int i = 1; i <= columnCount; i++) {
String columnName =metaData.getColumnLabel(i);
String value2 = rs.getString(columnName);
try {
jsonObj.put(columnName, value2);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
array.put(jsonObj);
}
} catch (SQLException e1) {
e1.printStackTrace();
}
//关闭
try {
if(rs!=null) rs.close();
//pstmt.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
//PrintWriter out = response.getWriter();
System.out.println(array);
}//总类
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
public static void main(String args[]) {
searchStudent ss=new searchStudent();
try {
ss.doGet(null, null);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}