title: Java: Insert into a table datetime data
body: <p>I am trying to insert into a variable in MS- SQL database the current date and the time.
  I use this format: </p>
  
  <pre><code>DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); 
  Calendar cal = Calendar.getInstance();  
  System.out.println(dateFormat.format(cal.getTime()));
  </code></pre>
  
  <p>and I get this as a result 2013-01-28 09:29:37.941</p>
  
  <p>My field in the database is defined <code>datetime</code> and as I have seen in other tables which have the same field, the date and the time is written exactly like this 2011-07-05 14:18:33.000.</p>
  
  <p>I try to insert into the database with a query that I do inside a java program, but I get this error</p>
  
  <blockquote>
    <p>SQL Exception: State  : S0003 Message: The conversion of a varchar
    data type to a datetime data type of the value is out of range. Error 
    : 242</p>
  </blockquote>
  
  <p>My query is like that: </p>
  
  <pre><code>query = "INSERT INTO Companies CreatedOn"+ 
           "VALUES ('" + dateFormat.format(cal.getTime()) + "')"
  </code></pre>
  
  <p>but I don't understand what I am doing wrong.</p>