Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

jsp - Getting null values in student.java method "student1()" from studentDAO.java?

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 id,detailsi,nam,ph,clas,i;

    public void 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");
                Student studentinfo=new Student();
                studentinfo.Student1(i, nam, clas, ph);
            }
        } catch (SQLException e) {
        }
    } 
}

second.jsp

<html> 
    <body>
        <% 
            String id =request.getParameter("ID1");
            StudentDAO std=new StudentDAO();
            Student st=new Student();
            std.searchinfo("id");
        %>
        <h1> <%=st.showStudent()%> </h1>
    </body>
</html>

Getting null values in student.java method "student1()" from studentDAO.java?? StudentDAO.java connects DB properly and give result properly as individually.. But not passing values as a parameter to "student1()" in Student.java file.. Please suggest where i am getting wrong...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Change public void searchinfo(String id2) to public Student searchinfo(String id2) and return studentinfo[i.e. Student object].

//JSP corrections

String id = request.getParameter("ID1");
StudentDAO std = new StudentDAO();

Student st = std.searchinfo(id);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...