Announcement Announcement Module
Collapse
No announcement yet.
Date format using JDBC Template Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Date format using JDBC Template

    I'm trying to insert a date into a MYSQL DB,I fisrt create the sqlDate with the folowing code:
    Code:
    Calendar cal= Calendar.getInstance();		
    java.sql.Date sqlDate =  new java.sql.Date(cal.getTime().getTime() );
    When I print out the sqlDate variable, I get : 2005-06-27

    But when I look in the DB or when I pick up the date again using a select statement I get the following date: 27/05/1905

    I know Java start counting it's month's from 0-11 and its years from 1900. But I'm using java.sql.Date and I'm printing the correct Date in the console.

    is it possible that this has to do something about the way spring handles a date?

    I'm using following basic code to insert the Date in the DB:
    Code:
    template.update("INSERT INTO TABLE VALUES("+sqlDate+",'a')";

  • #2
    Try using the following:

    Code:
    template.update("INSERT INTO TABLE VALUES(?, ?)", new Object[] {sqlDate, "a"});

    Comment


    • #3
      I tried that from the start, but I use JDBC/ODBC, and that query isn't supported.

      Now I tried to get a connection through the jtds-driver.
      The problem is that a have 2 DB:
      192.53.62.75 -> MyDB
      and
      192.53.62.75\TESTDB -> MyDB

      I can connect to the first DB without any problems, but not to the second. Even though the jtds driver support named instances.

      Comment


      • #4
        I use following XML. Isn't there some possibility to give a property instance or something like that? Jtds does not suppot to put the instance directly in the URL. Or are there better drivers that support this possibility.

        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
        <property name="driverClassName">

        <value>net.sourceforge.jtds.jdbc.Driver</value>
        </property>
        <property name="url">
        <value>jdbc:jtds:sqlserver://192.X.X.X:1433/MyDB</value>
        </property>
        <property name ="instance">
        <value>TESTDB</value>
        </property>
        <property name="username">
        <value>un</value>
        </property>
        <property name="password">
        <value>pwd</value>
        </property>
        </bean>

        Comment


        • #5
          If anyone got the same problem.. Of course U can't add the property like that: (Check the Spring API doc)
          <property name ="instance">
          <value>TESTDB</value>
          </property>

          Instead of adding the named instance like that, you can simply add the instance name in you URL like this:

          <property name="url">
          <value>jdbc:jtds:sqlserver://192.X.X.X:1433/MyDB;instance=TESTDB</value>
          </property>

          Thnx for your help

          Comment


          • #6
            By The Way: that solved also my first problem
            When I use
            Code:
            template.update&#40;"INSERT INTO TABLE VALUES&#40;"+sqlDate+",'a'&#41;";
            I get a wrong date in my DB,
            and when I use
            Code:
            template.update&#40;"INSERT INTO TABLE VALUES&#40;?, ?&#41;", new Object&#91;&#93; &#123;sqlDate, "a"&#125;&#41;;
            I get a correct Date !!!!!!!!

            Comment

            Working...
            X