Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Stringbased values needs to be quoted in SQL string. But, better is to use <a href="http://download.oracle.com/javase/tutorial/jdbc/basics/prepared.html" rel="nofollow"><code>PreparedStatement</code></a>. Then you don't worry about quoting the values nor about escaping from <a href="http://unixwiz.net/techtips/sql-injection.html" rel="nofollow">SQL injection attacks</a>.</p> <pre><code>String sql = "INSERT INTO logfile VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)"; preparedStatement = connection.prepareStatement(sql); preparedStatement.setInt(1, logMessage.getCreator()); preparedStatement.setInt(2, logMessage.getCreatorParent()); preparedStatement.setInt(3, logMessage.getSequenceNumber()); preparedStatement.setInt(4, logMessage.getCreatorCost()); preparedStatement.setInt(5, logMessage.getCreatorEnergy()); preparedStatement.setInt(6, interval); preparedStatement.setInt(7, lplinterval); preparedStatement.setInt(8, logMessage.getData()); preparedStatement.setTimestamp(9, new Timestamp(tmp2.getTime())); // Assuming tmp2 is java.util.Date. preparedStatement.executeQuery(); </code></pre> <p><em>(and work on your <a href="http://www.oracle.com/technetwork/java/codeconv-138413.html" rel="nofollow">coding conventions</a>, yours is pretty terrible, also don't forget to properly close JDBC resources, you don't seem to take this into account as well)</em></p> <hr> <p><strong>Update</strong>: as per the comments, <code>tmp2</code> is a <code>String</code>. Here's how you could convert it to <code>Date</code> so that you can use it in <code>setTimestamp()</code> so that the value will be properly saved in a <code>DATETIME</code> column type. Since you didn't mention what date format the <code>tmp2</code> is in, I'll just take <code>"yyyy-MM-dd HH:mm:ss"</code> as an example:</p> <pre><code>String datestring = "2011-05-19 12:34:56"; // Just an example. Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(datestring); Timestamp timestamp = new Timestamp(date.getTime()); // Store in SQL string by PreparedStatement#setTimestamp(). </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload