2015-06-30 64 views
0

JSP頁面從jsp頁面獲取瀏覽器中的空值?

<%@ page import="Student.*"%> 

<html> 


<body> 


     <% 


    String id =request.getParameter("ID1"); 

    StudentDAO std=new StudentDAO(); 
    Student st=std.searchinfo("id"); 


    %> 

     <h1> <%=st.showStudent()%> </h1> 


</body> 

</html> 

Student.java

import java.io.*; 

public class Student implements Serializable { 

String name; 

    String id1,phone,clas; 

    public Student() 
    { 

    } 

    public String showStudent() 

    { 

     return "Name: "+name+" Address: "+clas+" Phone: "+phone+" ID: "+id1; 
    } 
    public void Student1(String id,String n,String c,String ph) 

    { 

     name=n; 
     id1=id; 
     phone=ph; 
     clas=c; 
    } 

} 

StudentDAO.java

import java.sql.*; 

    import java.io.*; 

    public class StudentDAO implements Serializable 
    { 

      static String i,id,nam,clas,ph; 

     Student studentinfo=new Student(); 

     public Student searchinfo(String id2) 
     { 
      id=id2; 


     try{ 

     String url="jdbc:ucanaccess://C:\\Users\\Asim Iqbal\\Documents\\IT.accdb"; 

     Connection conn = DriverManager.getConnection(url); 

     String sql= "SELECT * FROM Student WHERE ID=?"; 

        PreparedStatement stmt = conn.prepareStatement(sql); 
        stmt.setString(1,id); 
        ResultSet rs=stmt.executeQuery(); 
        if (rs.next()) 
        { 
         i=rs.getString("ID"); 

         nam=rs.getString("Name"); 


         clas=rs.getString("Class"); 
         ph=rs.getString("Phone"); 
         studentinfo.Student1(i, nam, clas, ph); 
        } 
       } catch (SQLException e) { 
       } 
      return studentinfo; 
     } 
    } 

這是錯誤。但StudentDAO.java DB連接正確,並作爲單獨給輸出。 。但在瀏覽器中它給出空值如下。

名稱:空地址:空電話:空ID:空

+0

,你得到空字段,這意味着學生對象本身不是空。數據庫表中的列名和類型是什麼?這是否與rs.getString()調用相匹配? – NickJ

+0

可能的重複[在student.java方法中獲得空值「student1()」from studentDAO.java?](http://stackoverflow.com/questions/31137722/getting-null-values-in-student-java-method -student1從 - studentdao-java的) – Naman

回答

0
<html> 
<body> 
     <% 
    String id =request.getParameter("ID1"); 
    StudentDAO std=new StudentDAO(); 

學生ST = std.searchinfo(ID);

%> 
     <h1> <%=st.showStudent()%> </h1> 
</body> 
</html> 

更改你的JSP頁面代碼上面,你應該通過id的值作爲參數傳遞給DAO方法,你的傳球現在是一個字符串「ID」。

記住寫JSP中的Java代碼是一個不好的做法