2

I am new to J2EE application and trying to display records from database after searching it by the student id which is also my primary key. jdbc/studentDB is my JNDI name.
Is MY syntax @Resource(name="jdbc/studentDB") in studentdao.java enough for the connection?If yes,Is the problem with method executeFetchQuery(int id)? Where I am going wrong please help.

    //search.java(servlet)

    ResultSet rs = ***studentDao.executeFetchQuery(v);***
    String fName = null; 
    try{
          fName = rs.getString("firstname");
        while(rs.next()){ 

    fName = rs.getString("firstname");


    }
    }catch (Exception ex){
      System.out.println("error: "+ex);
            }

        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet getdata</title>");            
        out.println("</head>");
        out.println("<body>");
        out.println("fname: "+ fName);
        out.println("</body>");
        out.println("</html>");

//studentdao.java

 //required imports
 @Stateless
 public class StudentDao implements StudentDaoLocal {
 @Resource(name="jdbc/studentDB")
 private DataSource datasource;


@PersistenceContext
private EntityManager em;

@Override
public void addStudent(Student student) {
    em.persist(student);
}

@Override
public void editStudent(Student student) {
    em.merge(student);
}

@Override
public void deleteStudent(int studentId) {
    em.remove(getStudent(studentId));
}

@Override
public Student getStudent(int studentId) {
    return em.find(Student.class, studentId);
}

@Override
public List<Student> getAllStudents() {
    return em.createNamedQuery("Student.getAll").getResultList();
}

/**
 *
 * @param id
 * @return
 */
@Override
***public  ResultSet executeFetchQuery(int id){***
    ResultSet rs = null;
    try{ 
       Connection conn = datasource.getConnection();
       String sql = "SELECT * FROM STUDENT WHERE STUDENTID = '" + id +"'";
       rs = conn.createStatement().executeQuery(sql);
       conn.close();
   } catch (Exception e) {
       System.err.println(e.getMessage());
   }
  return rs;
  }

// output for a id in database

fname :null

Mohit Kumar
  • 500
  • 6
  • 24

1 Answers1

0

The following always will cause an exception because you are calling rs.getString() before rs.next()

try{ fName = rs.getString("firstname"); while(rs.next()){ fName = rs.getString("firstname"); } } Remove the first `fName = rs.getString("firstname");` The the try block will be like try{ while(rs.next()){ fName = rs.getString("firstname"); } }

Update

In StudentDao.executeFetchQuery(int) you are returning an ResultSet but the Connection used to get the ResultSet is closed so the ResultSet will also be closed.

This is not correct way to do. Return the firstname from StudentDao.executeFetchQuery(int) so it should be.

public String executeFetchQuery(int id){
  String firstname = null;
  try{ 
    Connection conn = datasource.getConnection();
    String sql = "SELECT * FROM STUDENT WHERE STUDENTID = '" + id +"'";
    rs = conn.createStatement().executeQuery(sql);
    if(rs.next()){
      firstName = rs.getString("firstname");
    }
    conn.close();
  }catch (Exception e) {
    System.err.println(e.getMessage());
  }
  return firstname;
}

And in search.java

String fname = studentDao.executeFetchQuery(v);
seenukarthi
  • 8,241
  • 10
  • 47
  • 68
  • ,Hi,I tried removing [fName = rs.getString("firstname");],but the output still remains same.Is the problem in ResultSet executeFetchQuery(int id) method in studentdao.java.The variable fName in search.java(servlet) is passesd out of try catch block properly.ryt?? please help – Mohit Kumar Dec 09 '14 at 09:14
  • fName: null,(why the value is not being changed after going through tryCatch block) – Mohit Kumar Dec 09 '14 at 16:46
  • Check is there any exceptions – seenukarthi Dec 09 '14 at 16:47