Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

UUID Java Type to SQL CHAR(40) – Unable to store into Array

I am creating a dynamic web application, I have to use Java, HTML and MySQL. I am having issues using the type UUID.

The SQL table is as follows:

CREATE TABLE `logs` (
  `uuid` char(40) COLLATE utf8_unicode_ci NOT NULL,
  `title` char(128) COLLATE utf8_unicode_ci DEFAULT NULL,
  `content` text COLLATE utf8_unicode_ci,
  `createTimestamp` date DEFAULT NULL,
  PRIMARY KEY (`uuid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

I have a standard Log class with getters/setters for each variable.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have an applicationDAO class that inserts logs into the database – which works fine, but I must convert the UUID to a string in order to get all fields into the database.

public int insertLog(TextLog log) throws SQLException {
        
        int rows = 0;
          try { 
                
                connection = DBConnection.getConnectionToDatabase();
                PreparedStatement ps = connection.prepareStatement(INSERT_LOG_SQL);
                
                
                ps.setObject(1, log.getUUID().toString());
                ps.setString(2, log.getTitle());
                ps.setString(3, log.getContent());
                ps.setObject(4, java.sql.Date.valueOf(java.time.LocalDate.now()));
                
                rows = ps.executeUpdate();
                
            } catch (Exception e) {
            
                e.printStackTrace();
            }
        return rows;
        
    }

Now the problem lies when I try to store the SQL table into an array.

@Override
    public List<TextLog> listLogs() {
        
        TextLog log = null;
        List<TextLog> tl = new ArrayList<>();
        
        try { 
            
            connection = DBConnection.getConnectionToDatabase();
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(SELECT_ALL_LOGS);
        
            
            while (rs.next()) { 
                log = new TextLog();
                log.setUUID(rs.getObject("uuid", java.util.UUID.class));
                log.setTitle(rs.getString("title"));
                log.setContent(rs.getString("content"));
                log.setDate(rs.getDate("createTimestamp"));

                
            }
        
        } catch (Exception e) {
        
            e.printStackTrace();
        }
    
        
        return tl;
    }

Because the field in SQL is char(40) it is not reading it as a UUID and I am getting the following error:
java.sql.SQLException: Conversion not supported for type java.util.UUID

Keep in mind I have to keep the fields as is and cannot adjust.

Any thoughts on how I can get around this error?

Thanks in advance,

>Solution :

Get String and then convert it to UUID:

log.setUUID(java.util.UUID.fromString(rs.getString("uuid")));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading