Mapping a row from a SQL data to a Java object

前端 未结 9 1169
悲哀的现实
悲哀的现实 2021-02-06 02:34

I have a Java class with instance fields (and matching setter methods) that match the column names of a SQL database table. I would like to elegantly fetch a row from the table

9条回答
  •  [愿得一人]
    2021-02-06 02:57

    A slightly less verbose way would be to give Student a constructor that accepts 3 strings. Then you could do this:

    Student student = new Student(rs.getString("FNAME"), rs.getString("LNAME"), rs.getString("GRADE"));
    

    The other way to do it is to use an ORM like Hibernate but Hibernate only becomes worth the massive setup effort for really big projects dealing with lots of tables.

提交回复
热议问题