Ad
How To Retrieve An Object From ResultSet In Java?
So I tried to make a getRecord class to retrieves info from database based on the ID the user entered,
public Student getRecord(String id) {
String queryStr = "SELECT * FROM " + tableName + " WHERE ID = ?";
Student student = null;
try {
stmt = conn.prepareStatement(queryStr);
stmt.setString(1, id);
ResultSet rs = stmt.executeQuery();
int count = 1;
if (rs.next()) {
String level = rs.getString("Level");
char level1 = level.charAt(0);
student = new Student(id, rs.getString("IC"), rs.getString("Name"), level1, rs.getObject("ProgrammeCode"), Integer.parseInt(rs.getString("Yr")));
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
}
return student;
}
So it run the query and call the constructor of student,
public Student(String id, String ic, String name, char level, Programme programme, int year) {
this.id = id;
this.ic = ic;
this.name = name;
this.level = level;
this.programme = programme;
this.year = year;
}
To create an object for student class and then return it to the main program. However, the student constructor also need another object of programme. How can i retrieve the object from the database through ResultSet? I have stucked on this for hours and attempted to use getObject but it didn't work. Please guide me. TQ and have a good day.
Ad
Answer
SOLUTION FOUND THANKS TO @Joop Eggen
I managed to solve this by declaring and initializing a new object, replacing the rs.getString("ProgrammeCode"); to (new Programme(rs.getString("ProgrammeCode"))).
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad