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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With