我已经测试了我的代码,所有stdData都为我提供了正确的值,但是当我在jsp页面上运行它时,它却给了我null值,为什么这需要进行任何更正或其他逻辑操作,所以我可以撤消代码中的错误并获得正确的值,请为此提供指导。
这是我配置数据库规范的类
package school;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ReadingStd {
String conURL = "jdbc:sqlserver://localhost:1433;"
+ "databaseName=schoolDB;user=fas;password=24071982";
List<String> stdData = new ArrayList<String>(222);
String std = null;
public void readStd(String stdId) {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection(conURL);
System.out.println("faisal:");
String sqlQuery = "select * from schoolDB.dbo.addstudent where sid = '" + stdId + "'";
Statement stmt = con.createStatement();
System.out.println("faisal11:");
ResultSet rs = stmt.executeQuery(sqlQuery);
while(rs.next()) {
//System.out.println(":5465");
stdData.add(0,rs.getString("sid"));
//System.out.println(rs.getString("sid"));
//std = rs.getString("sid");
stdData.add(1, rs.getString("name"));
//System.out.println(rs.getString("name"));
stdData.add(2, rs.getString("fname"));
stdData.add(3, rs.getString("dob"));
stdData.add(4, rs.getString("classs"));
stdData.add(5, rs.getString("occupation_father"));
stdData.add(6, rs.getString("edu_father"));
stdData.add(7, rs.getString("email"));
stdData.add(8, rs.getString("father_office_address"));
stdData.add(9, rs.getString("cont1"));
stdData.add(10, rs.getString("occupation_mother"));
stdData.add(11, rs.getString("mother_office_address"));
stdData.add(12, rs.getString("mname"));
stdData.add(13, rs.getString("cont2"));
stdData.add(14, rs.getString("caddress"));
stdData.add(15, rs.getString("paddress"));
stdData.add(16, rs.getString("sex"));
stdData.add(17, rs.getString("addate"));
stdData.add(18, rs.getString("division"));
stdData.add(19, rs.getString("division"));
}
System.out.println(stdData.get(16) + "SFsdf");
//System.out.println(stdData.size());
con.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
public List<String> returnStd() {
//System.out.println(stdData.size() + "fraaksfrafi");
return stdData;
}
public void printstd() {
//ArrayList<String> inter1 = (java.util.ArrayList) returnStd();
//for (int i = 0; i < stdData.size(); i++) {
System.out.println(stdData + "faisal rafi");
System.out.println(stdData.size() + "muhammad faisal muhammad rafi");
//}
System.out.println("fsdf");
}
/* public static void main (String[] args) {
ReadingStd reader = new ReadingStd();
reader.readStd("czc");
reader.printstd();
}*/
}
jsp页面
<%@ page contentType="text/html; charset=utf-8"
language="java" import="java.sql.*" errorPage="" %>
<%@ page import="java.util.*"%>
<%@ page import="school.ReadingStd" %>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<% ReadingStd dbData = new ReadingStd();
ArrayList<String> stdData1 = new ArrayList<String>((java.util.ArrayList) dbData.returnStd());
String [] stdData = new String[20];
for (String stdData2: stdData1) {
System.arraycopy( stdData2, 0, stdData, 0, 20 );
}
%>
<label>Student ID</label>
<input name="stdid" type = "text" value="<%= stdData[0]%>"></br>
<label>Student Name</label>
<input name= "stdName" type = "text" value="<%= stdData[1]%>"></br>
Date Of Birth <input type="date" value = "<%= stdData[3]%>"></br>
<select name="gender">
<option value = "<%= stdData[16]%>"></option>
<option>Male</option>
<option>Female</option>
</select></br>
<label>class</label>
<select name = "stdClass">
<option value = "<%= stdData[4]%>"></option>
</select></br>
<label>Division</label>
<select name = "division">
<option value = "<%= stdData[18]%>"></option>
</select></br>
<label>Contact Address</label>
<input name= "contactAdd" type = "text" value = "<%= stdData[14]%>"></br>
<label>Permenant Address</label>
<input name= "permAdd" type = "text" value = "<%= stdData[15]%>"></br>
<label>Father Information</label></br>
<label>Father Name</label>
<input name="fName" type ="text" value = "<%= stdData[2]%>"></br>
<label>Father Occupation</label>
<input name="fOccup" type ="text" value = "<%= stdData[5]%>"></br>
<label>Father Education</label>
<input name="fEdu" type ="text" value = "<%= stdData[6]%>"></br>
<label>Father Email</label>
<input name="fEmail" type ="text" value = "<%= stdData[7]%>"></br>
<label>Father Office Address</label>
<input name="fOfficeAddd" type ="text" value = "<%= stdData[8]%>"></br>
<label>Father Contact No</label>
<input name="fcontact" type ="text" value = "<%= stdData[9]%>"></br>
<label>Mother Information</label></br>
<label>Mother Name</label>
<input name="mName" type ="text" value = "<%= stdData[12]%>"></br>
<label>Mother Occupation</label>
<input name="mOccup" type ="text" value = "<%= stdData[10]%>"></br>
<label>Mother Education</label>
<input name="mEdu" type ="text" value = "<%= stdData[10]%>"></br>
<label>Mother Email</label>
<input name="mEmail" type ="text" value = "<%= stdData[19]%>"></br>
<label>Mother Office Address</label>
<input name="mOfficeAddd" type ="text"></br>
<label>Mother Contact No</label>
<input name="mcontact" type ="text" value = "<%= stdData[13]%>"></br>
Date Of Admission <input type="date"name ="addmissiomdate" value = "<%= stdData[17]%>"></br>
>
<input name="addStd" type="submit" value="Add Student">
</body>
</html>