Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You should be using <a href="http://docs.oracle.com/javase/6/docs/api/java/sql/PreparedStatement.html#setTimestamp%28int,%20java.sql.Timestamp%29" rel="nofollow"><code>PreparedStatement#setTimestamp()</code></a> to set a <code>TIMESTAMP/DATETIME</code> field (or <a href="http://docs.oracle.com/javase/6/docs/api/java/sql/PreparedStatement.html#setDate%28int,%20java.sql.Date%29" rel="nofollow"><code>setDate()</code></a> to set a <code>DATE</code> field, but that doesn't cover hours and minutes...).</p> <pre><code>String sql = "INSERT INTO" + " APP.TRUMPS (TRUMPID, DEPART, ARRIVE, START, PLACES, PROPOSING_USER_LOGIN, CLOSING_HOURS)" + " VALUES (?, ?, ?, ?, ?, ?, ?)"; // ... PreparedStatement preparedStatement = null; // ... try { // ... preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, newTrampTrumpistClient.login+dateForm.getText()+timeForm.getText()); preparedStatement.setString(2, from); preparedStatement.setString(3, to); preparedStatement.setTimestamp(4, new Timestamp(calendar.getTimeInMillis())); preparedStatement.setInt(5, places); preparedStatement.setString(6, newTrampTrumpistClient.login); preparedStatement.setInt(7, closinghours); preparedStatement.executeUpdate(); // ... } finally { // ... } </code></pre> <p>Additional bonus is that using prepared statements protects your application from <a href="http://en.wikipedia.org/wiki/SQL_injection" rel="nofollow">SQL injection attacks</a>.</p> <h3>See also:</h3> <ul> <li><a href="http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html" rel="nofollow">JDBC tutorial - using prepared statements</a></li> </ul> <hr> <p><strong>Unrelated</strong> to the concrete problem, converting from <code>String</code> to <code>Calendar</code> is pretty clumsy. Consider using <a href="http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html" rel="nofollow"><code>SimpleDateFormat</code></a> to convert from <code>String</code> to <code>Date</code>. You can then persist it as <code>new Timestamp(date.getTime())</code>.</p>
 

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