Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / June 2005

Tip: Looking for answers? Try searching our database.

problem with timestamps in HSQL

Thread view: 
sean - 25 Jun 2005 06:23 GMT
Hey All,

I am having problems trying to insert a TIMESTAMP value into a simple
HSQL database, see code below.  The print statement indicates the
formatting of the timestamp looks reasonable, but I can never get past
the "Wrong data type" exception.  I've tried various formatting of the
timestamp string to no avail.  Does anyone know what's wrong?

std out:
----------------------------------------------------------------------------
INSERT INTO DATA VALUES('2005-06-24
22:18:53.109000000',48.89959329052301,64.86097067315892)
--------------------------
java.sql.SQLException: Wrong data type: type: TIMESTAMP (93) expected:
REAL value: 2005-06-24 22:18:53.109

code:
----------------------------------------------------------------------------

public final class HSQLTest {

   private Connection conn;
   //private final SimpleDateFormat formatter = new
SimpleDateFormat("yyyy-MM-dd hh:mm:ss.0");

   public HSQLTest() throws SQLException, InstantiationException,
IllegalAccessException, ClassNotFoundException {
       Class.forName("org.hsqldb.jdbcDriver").newInstance();
       conn = DriverManager.getConnection("jdbc:hsqldb:mem:aname", "sa",
"");
       final Statement stmt = conn.createStatement();
       stmt.executeUpdate("CREATE TABLE DATA(TIME TIMESTAMP, VOLTAGE
FLOAT, TEMP FLOAT)");
       stmt.close();
   }

   public void populateTable() {

       final Calendar cal = new GregorianCalendar();
       new Thread() {

           public void run() {

               while (true) {
                   try {
                       final Statement stmt = conn.createStatement();
                       final String t =
HsqlDateTime.getTimestampString(new
Timestamp(System.currentTimeMillis()), cal);
                       final String insertSQL = "INSERT INTO DATA
VALUES('" + t + "'," + ((Math.random() * 5.0) + 48.0) + "," +
((Math.random() * 5.0) + 60.0) + ")";
                       System.out.println(insertSQL);
                       stmt.executeUpdate(insertSQL);

                       final ResultSet rs = stmt.executeQuery("SELECT
* FROM DATA");

System.out.println("--------------------------");
                       while (rs.next()) {
                           System.out.println(rs.getDate(1) + "," +
rs.getFloat(1) + "," + rs.getFloat(2));
                       }

                   } catch (Exception ex) {
                       ex.printStackTrace();
                   }

                   try {
                       sleep(1000);
                   } catch (InterruptedException e) {
                   }
               }
           }

       }.start();

   }

    public static void main(String[] args) throws Exception {
       new HSQLTest().populateTable();
    }
}
joeNOSPAM@BEA.com - 25 Jun 2005 21:27 GMT
You can solve this by doing the insert the standard JDBC
way, via a PreparedStatement, and setTimestamp():

PreparedStatement p = conn.prepareStatement("insert into data
values(?,?,?)" );
p.setTimestamp(1, new Timestamp(System.currentTimeMi­llis()) );
p.setFloat(2, ((Math.random() * 5.0) + 48.0) );
p.setFLoat(3, ((Math.random() * 5.0) + 60.0) );

p.executeUpdate();

If you are looping, create the statement before the loop,
and then, just set arguments and execute the prepared
statement over an over again.

I hope this helps,
Joe Weinstein at BEA
fredt - 26 Jun 2005 16:43 GMT
Thanks Sean,

This looks like a new or recent bug, will be fixed in the release version of
1.8.0.

Fred Toussi
Maintainer, HSQLDB Project
http://hsqldb.org

> Hey All,
>
[quoted text clipped - 79 lines]
> }
> }
fredt - 26 Jun 2005 20:24 GMT
I checked this, and it works fine with 1.8.0 RC12, which you can download
from http://hsqldb.org

Fred

> Thanks Sean,
>
[quoted text clipped - 88 lines]
>> }
>> }


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.