学生.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>
从studentDAO.java中获取student.java方法“ student1()”中的空值?StudentDAO.java正确连接了数据库,并分别正确地给出了结果。.但是没有将值作为参数传递给Student.java文件中的“ student1()”。
更改public void searchinfo(String id2)为public Student searchinfo(String id2)并返回studentinfo [即Student对象]。
public void searchinfo(String id2)
public Student searchinfo(String id2)
// JSP更正
String id = request.getParameter("ID1"); StudentDAO std = new StudentDAO(); Student st = std.searchinfo(id);