Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get parsed Date from Sqlite3 databse with Java?

Tags:

java

sqlite

jsp

I am getting data from sqlite3 DB, by creating a model for my method with POJO. While retrieving data in a arraylist I am getting parse error for date. The date is defined as timestamp in the sqlite3 DB and while compiling it is throwing : Unparseable date: "2015-05-21 12:41:56" error. And while compiling query it is throwing : Error parsing time stamp I am using getDate() to retrieve date from the table.

public List<EnergyMeter> getAllData() {
    List<EnergyMeter> meters = new ArrayList<EnergyMeter>();
    try {
        LOGGER.info("############### FetchData.getAllData() start");
        Statement statement = DBConnection.getConnection().createStatement();
        ResultSet rs = statement.executeQuery("SELECT * FROM ENERGY_METER");
        LOGGER.info("############### FetchData.getAllData() meters:"+rs.getDate("CREATION_TIME"));
        while (rs.next()) {
            EnergyMeter energyMeter = new EnergyMeter();
            energyMeter.setDEVICE_IP(rs.getString("IP"));
            energyMeter.setCREATION_TIME(rs.getDate("CREATION_TIME"));
            energyMeter.setDESCRIPTION(rs.getString("DESCRIPTION"));
            energyMeter.setDEVICE_NAME(rs.getString("DEVICE_NAME"));
            energyMeter.setEMAIL_ID(rs.getString("EMAIL_ID"));
            energyMeter.setFDU_NAME(rs.getString("FDU_NAME"));
            energyMeter.setICBS(rs.getString("ICBS"));
            energyMeter.setLOCATION(rs.getString("LOCATION"));
            energyMeter.setMAC_ADDRESS(rs.getString("MAC_ADDRESS"));
            energyMeter.setMASTER_ID(rs.getString("MASTER_ID"));
            energyMeter.setPARAMETER(rs.getString("PARAMETER"));
            energyMeter.setPORT(rs.getString("PORT"));
            energyMeter.setPRODUCT_NAME(rs.getString("PRODUCT_NAME"));
            energyMeter.setSMTP(rs.getString("SMTP"));
            energyMeter.setSNMP(rs.getString("SNMP"));
            meters.add(energyMeter);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }

    LOGGER.info("############### FetchData.getAllData() end");
    return meters;
}

I defined date as : private Date CREATION_TIME; in POJO.

like image 733
Vicky Singh Gill Avatar asked May 22 '15 13:05

Vicky Singh Gill


People also ask

How do I get data from sqlite3?

You can fetch data from MYSQL using the fetch() method provided by the sqlite python module. The sqlite3. Cursor class provides three methods namely fetchall(), fetchmany() and, fetchone() where, The fetchall() method retrieves all the rows in the result set of a query and returns them as list of tuples.

Can SQLite be used with Java?

To use SQLite with java programs, you must have SQLite JDBC Driver and Java set up on the system. Follow the steps given below: Download latest version of sqlite-jdbc-(VERSION). jar from sqlite-jdbc repository.


1 Answers

SQLite3 doesn't provide a date type.

You need to get it as a String and parse it in Java.

new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(rs.getString("CREATION_TIME")); //Throws exception

See: http://www.sqlite.org/datatype3.html

like image 74
tbraun Avatar answered Oct 02 '22 02:10

tbraun