hello, i try to make a gui for some queries i work with netbeans and mysql and i have for example a student table with 6 fields age,id,year,... when i type the query:
from Students
it shows all the fields of the table and all data perfectly
when i type a query like
select year from Students
I got a table but not with the year field only but with some columns and the result write in bytes and not in strings as its defined.
when i want to see it as a gui in netbeans it works fine with the first query "from Students" but when I type the second query with the select clause it show me this error:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to project.entity.Students
when Students is the class wehere all the data fields are defined.
why it gives me this error? it seems that when i use the select clasue or any query that ask not for all the fields it doesn't show the result table as excepted when i try this query from Students where s.Age<30 it works fine and shows all the fields in the table but only with the age <30
here is my code:
private void executeHQLQuery(String hql) {
try {
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Query q = session.createQuery(hql);
List resultList = q.list();
displayResult(resultList);
session.getTransaction().commit();
} catch (HibernateException he) {
he.printStackTrace();
}
}
private void displayResult(List resultList) {
Vector<String> tableHeaders = new Vector<String>();
Vector tableData = new Vector();
tableHeaders.add("StudentID");
tableHeaders.add("Age");
tableHeaders.add("City");
tableHeaders.add("Name");
tableHeaders.add("SchoolID");
tableHeaders.add("Year");
for (Object o : resultList) {
Students student= (Students) o;
Vector<Object> oneRow = new Vector<Object>();
oneRow.add( student.getAge() );
oneRow.add(student.getStudentId());
oneRow.add(student.getAge());
oneRow.add(student.getName());
oneRow.add(student.getSchoolId());
oneRow.add(student.getYear());
tableData.add(oneRow);
}
resultTable.setModel(new DefaultTableModel(tableData, tableHeaders));
thanks!!