Mapping a row from a SQL data to a Java object

前端 未结 9 1170
悲哀的现实
悲哀的现实 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.

    0 讨论(0)
  • 2021-02-06 02:58

    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.

    0 讨论(0)
  • 2021-02-06 03:01

    There are many ORM libraries that simplify or eliminate the JDBC drudgery. See Source Forge ORM for some examples. I like my library, sormula, since it can be used with minimal configuration.

    0 讨论(0)
  • 2021-02-06 03:01

    When you execute a query you can get metadata from the ResultSet. You have access to the columns from this. Here's an example:

    
    @RestController
    public class MyController {
    
        @GetMapping("/characters")
        public List<Payload> characters() {
            List<Payload> results = new ArrayList<>();
    
            try (Connection conn = new Connection()) {
                conn.makeConnection();
                Statement stmt = conn.createStatement();
    
                ResultSet result = stmt.executeQuery("SELECT * FROM public.hello;");
                ResultSetMetaData resultMetaData = result.getMetaData();
    
                Set<String> columns = new HashSet<>();
    
                for (int i = 1; i <= resultMetaData.getColumnCount(); i++) {
                    columns.add(resultMetaData.getColumnName(i));
                }
    
                while (result.next()) {
                    results.add(new Data(result, columns));
                }
            } catch (Exception e) {
                results.add(new Fail("404", e.getMessage()));
            }
    
            return results;
        }
    }
    
    public class Data implements Payload {
        private final Map<String, Object> data = new HashMap<>();
    
        public Data(ResultSet result, Set<String> columns) throws SQLException {
    
            for (String column : columns) {
                data.put(column, result.getString(column));
            }
        }
    
        public Map<String, Object> getData() {
            return data;
        }
    }
    
    

    Now you can have one class object that parses out the columns and data for any table. You never really care what columns there are. The down side is that all of your info is now stored in a data field. So the payload would look something like:

    [
      {"data":{"id":"1","name":"Rick Sanchez"}},
      {"data":{"id":"2","name":"Morty Smith"}},
      {"data":{"id":"3","message":"Summer Smith"}}
    ]
    
    0 讨论(0)
  • 2021-02-06 03:03

    You can do it generically by doing the following simple methods:

    Interface to use as a method pointer:

    public interface I_DBtoJavaObjectConvertable<T>
    {
        public T createFromDB(ResultSet i_rs) throws SQLException;
    }
    

    Generic class to handle every mapping from SQL to java Object:

    public class DBManager
    {
    
        static volatile Connection conn;
    
        //set here a static c'tor to handle the connection to the database
    
        //The General generic method:    
        public static <T> List<T> GetObjectsFromDB(String i_Query, I_DBtoJavaObjectConvertable i_Converter)
        {
            List<T> ResList = new ArrayList<>();
    
            try
            {
                Statement st = conn.createStatement();
                for (ResultSet rs = st.executeQuery(i_Query); rs.next();)
                {
                    ResList.add((T) i_Converter.createFromDB(rs));
                }
            }
            catch (SQLException ex)
            {
                _LOG_ERROR(ex.getMessage());
            }
    
            return ResList;
        }
    }
    

    Now By using Lanbda expression use can easlly convert an sql row to object, by given your convertion method, for example:

    public static User FetchUserFromDB(ResultSet i_rs)
    {
        User userToCreate = null;
        try
        {
            String FirstName = i_rs.getString("FirstName");
            String LastName = i_rs.getString("LastName");
            String Password = i_rs.getString("Password");
    
            userToCreate = new User(FirstName, LastName, Password);
    
        }
        catch (SQLException ex)
        {
            _LOG_ERROR("Error in fetching user from DB: \n" + ex.getMessage());
        }
        return userToCreate;
    }
    

    And now you can use this this method to bring any Users you want:

    public static List<User> GetAllUsersFromDB() throws SQLException
    {
        String Query = "select * "
                + "from UsersTable";
    
        return DBManager.GetObjectsFromDB(Query, rs -> FetchUserFromDB(rs));
    }
    

    Or:

    public static List<String> GetAllNamesFromDB() throws SQLException
    {
        String Query = "select FirstName "
                + "from UsersTable";
    
        return DBManager.GetObjectsFromDB(Query, rs -> rs.getString("FirstName"));
    }
    
    0 讨论(0)
  • 2021-02-06 03:04

    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

    0 讨论(0)
提交回复
热议问题