Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping a row from a SQL data to a Java object

Tags:

java

sql

jdbc

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 (into a ResultSet) and map it to an instance of this class.

For example:

I have a "Student" class with instance fields "FNAME", "LNAME", "GRADE" and appropriate getter and setter methods for each.

I also have a SQL table with three columns of the same name.

Right now I am doing something like this:

rs = statement.executeQuery(query);

Student student = new Student();

student.setFNAME(rs.getString("FNAME"));
student.setLNAME(rs.getString("LNAME"));
student.setGRADE(rs.getString("GRADE"));

There has to be a less verbose way of doing this, right? As I add columns this might get really annoying and messy.

like image 528
Martin Avatar asked Jan 24 '12 19:01

Martin


People also ask

Which class is used to map a database row to a Java?

RowMapper<T> interface is used by JdbcTemplate for mapping rows of a ResultSet on a per-row basis. Implementations of this interface perform the actual work of mapping each row to a result object.

What is the mapping between Java objects and database base tables called?

Object-Relational Mapping or ORM is a technique for converting data between Java objects and relational databases.

How do you create a ResultSet object?

But we can make this object to move forward and backward direction by passing either TYPE_SCROLL_INSENSITIVE or TYPE_SCROLL_SENSITIVE in createStatement(int,int) method as well as we can make this object as updatable by: Statement stmt = con. createStatement(ResultSet.

How to map a single result row to a Java object state?

To map a single result row to our java object Statewe will call jdbcTemplate.queryForObjectpassing in the sql to execute and the StateRowMapper. Running the junit testthe output shows that the sql is executed and the Statewas logged.

What is an example of a mapping in SQL?

Example of SQL Mapping Let us take a simple mapping example where we will take the class model and will see an equivalent physical data model in which each attribute of the class model will be mapped to a single column in the physical data model. Consider an example where we will be storing the class details of the stock at a shop.

What are the data types in mapping SQL and Java?

Mapping SQL and Java Data Types SQL data type Java data type Java data type DECIMAL java.math.BigDecimal BIT boolean Boolean TINYINT byte Integer SMALLINT short Integer 16 more rows ...

What is the best way to map between objects and tables?

You could use an ORM like one of the JPA providers e.g. Hibernate. This lets you set up mappings between your objects and your tables.


1 Answers

I recommend using Spring JDBC. You don't need to use the rest of Spring to use their JDBC library. It will manage connections for you (no more closing Connection, Statement, or ResultSet) and has many conveniences, including row mapping.

We've retrofitted legacy code with Spring JDBC with little trouble.

Here is a presentation (PDF) of an overview of Spring JDBC. It's a few years old but it still works essentially the same, even without letting Spring inject the dependencies.

Spring JDBC Presentation PDF

like image 95
Paul Avatar answered Oct 13 '22 15:10

Paul