How to convert String to java.sql.Date()?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • prasath03
    New Member
    • Jun 2007
    • 30

    How to convert String to java.sql.Date()?

    Dear All,

    I have one doubt for how to insert the java.sql.Date into ms-sql server.
    I tried the below code but it couldn't be inserted. I did wrong in
    my code that is ps.setDate(1,fu ll_date_time) to ps.setString(1, full_date_time) ;
    How can i covert the String(full_dat e_time) to java.sql.Date() and insert the MM/DD/YYYY format in ms-sql database?
    I set datetime as datatype in my products table.

    <%
    SimpleDateForma t sdf=new SimpleDateForma t("MM/dd/yyyy hh:mm:ss a");
    Date d=new Date();

    String full_date_time= sdf.format(d).t rim();
    out.println("Da te--------"+full_date_tim e);
    ps = con.prepareStat ement("insert into products values(?)");
    ps.setDate(1,pr oduct_id);
    ps.executeUpdat e();
    %>

    thanks in advance


    V. Prasath.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by prasath03
    Dear All,

    I have one doubt for how to insert the java.sql.Date into ms-sql server.
    I tried the below code but it couldn't be inserted. I did wrong in
    my code that is ps.setDate(1,fu ll_date_time) to ps.setString(1, full_date_time) ;
    How can i covert the String(full_dat e_time) to java.sql.Date() and insert the MM/DD/YYYY format in ms-sql database?
    I set datetime as datatype in my products table.

    <%
    SimpleDateForma t sdf=new SimpleDateForma t("MM/dd/yyyy hh:mm:ss a");
    Date d=new Date();

    String full_date_time= sdf.format(d).t rim();
    out.println("Da te--------"+full_date_tim e);
    ps = con.prepareStat ement("insert into products values(?)");
    ps.setInt(1,pro duct_id);
    ps.executeUpdat e();
    %>

    thanks in advance


    V. Prasath.
    What error message did you get ?

    Comment

    • prasath03
      New Member
      • Jun 2007
      • 30

      #3
      Originally posted by r035198x
      What error message did you get ?
      I wrongly typed in ps.setDate(1,da te_of_insert); instead of
      ps.setInt(1,dat e_of_insert);
      so please pardon me.


      when i compile that coding it throws the followin error......

      Incompatible type for method. Can't convert java.lang.Strin g to java.sql.Date.
      ps.setDate(1,da te_of_insert);

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by prasath03
        I wrongly typed in ps.setDate(1,da te_of_insert); instead of
        ps.setInt(1,dat e_of_insert);
        so please pardon me.


        when i compile that coding it throws the followin error......

        Incompatible type for method. Can't convert java.lang.Strin g to java.sql.Date.
        ps.setDate(1,da te_of_insert);
        To convert the String to sql Date, you use Date.valueOf (string). The string must be in the format yyyy-mm-dd

        Comment

        • sumittyagi
          Recognized Expert New Member
          • Mar 2007
          • 202

          #5
          Originally posted by prasath03
          I wrongly typed in ps.setDate(1,da te_of_insert); instead of
          ps.setInt(1,dat e_of_insert);
          so please pardon me.


          when i compile that coding it throws the followin error......

          Incompatible type for method. Can't convert java.lang.Strin g to java.sql.Date.
          ps.setDate(1,da te_of_insert);
          at one hand you are saying you want to insert date(and you are using the correct method as well), on the other hand you are passing the string object to it. Pass date object only. You don't have to worry about the database date format for that, setDate function will be taking care of that.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by sumittyagi
            at one hand you are saying you want to insert date(and you are using the correct method as well), on the other hand you are passing the string object to it. Pass date object only. You don't have to worry about the database date format for that, setDate function will be taking care of that.
            Yes, there's no need to change the date to string in the first place.

            Comment

            • prasath03
              New Member
              • Jun 2007
              • 30

              #7
              Originally posted by r035198x
              To convert the String to sql Date, you use Date.valueOf (string). The string must be in the format yyyy-mm-dd
              thanks for reply..

              i tried the following code to ur suggestion but it shows the
              the following error: java.lang.Illeg alArgumentExcep tion
              at java.sql.Date.v alueOf(Date.jav a:104)

              java.sql.Date dt = java.sql.Date.v alueOf(full_dat e_time);

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by prasath03
                thanks for reply..

                i tried the following code to ur suggestion but it shows the
                the following error: java.lang.Illeg alArgumentExcep tion
                at java.sql.Date.v alueOf(Date.jav a:104)

                java.sql.Date dt = java.sql.Date.v alueOf(full_dat e_time);
                Look at my first reply again, it requires the date to be in yyyy-mm-dd format. Better still don't change the date to the String at all. Just set it as a Date objec without formating it with the date format.

                Comment

                • sumittyagi
                  Recognized Expert New Member
                  • Mar 2007
                  • 202

                  #9
                  Originally posted by prasath03
                  thanks for reply..

                  i tried the following code to ur suggestion but it shows the
                  the following error: java.lang.Illeg alArgumentExcep tion
                  at java.sql.Date.v alueOf(Date.jav a:104)

                  java.sql.Date dt = java.sql.Date.v alueOf(full_dat e_time);
                  I am not able to understand what u are trying to do.
                  you first converted date to string.
                  then again converting string to date.
                  what for?

                  and second thing, you can't use valueOf(String) function here (I havn't heard of this function in date class, but it might exist for any other purpose) because, how will the date class be knowing what format your date string is in. there are thousands of formats of a date string.
                  You can get your date object back from string with SimpleDateForma t object only, by using the parse(String) function.
                  and if the date string is not in the same format as of SimpleDateForma t object then it will throw some parse Exception(I don't remember the exact exception).

                  But I don't understand why u need to convert date to string in the first place.

                  Comment

                  • Velmurugan1982
                    New Member
                    • Nov 2007
                    • 2

                    #10
                    Prasath,

                    did u get solution for this problem??? If so, please let me know...

                    Thanks

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Originally posted by Velmurugan1982
                      Prasath,

                      did u get solution for this problem??? If so, please let me know...

                      Thanks
                      Did you read the replies in this thread?
                      What problem are you getting?

                      Comment

                      • Velmurugan1982
                        New Member
                        • Nov 2007
                        • 2

                        #12
                        Originally posted by r035198x
                        Did you read the replies in this thread?
                        What problem are you getting?
                        I have to insert a date field in Oracle database from my java code. But the date field format in DB is MM/DD/YYYY HH/MM/SS AM/PM. I had read the earlier replies. But it is meant to only MM/DD/YY format. But in my case, the format must include the full date & time and also the am/pm notation. please advice.

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #13
                          Originally posted by Velmurugan1982
                          I have to insert a date field in Oracle database from my java code. But the date field format in DB is MM/DD/YYYY HH/MM/SS AM/PM. I had read the earlier replies. But it is meant to only MM/DD/YY format. But in my case, the format must include the full date & time and also the am/pm notation. please advice.
                          Have a look at Oracle's TO_DATE function.

                          Comment

                          • heat84
                            New Member
                            • Nov 2007
                            • 118

                            #14
                            Originally posted by Velmurugan1982
                            I have to insert a date field in Oracle database from my java code. But the date field format in DB is MM/DD/YYYY HH/MM/SS AM/PM. I had read the earlier replies. But it is meant to only MM/DD/YY format. But in my case, the format must include the full date & time and also the am/pm notation. please advice.
                            Will java.sql.Date be the accurate date since you want the date up to the second. Doesn't it give only 00/00/00 for the hours , minutes and seconds. I think java.util.Date is more accurate.

                            Comment

                            • r035198x
                              MVP
                              • Sep 2006
                              • 13225

                              #15
                              Originally posted by heat84
                              Will java.sql.Date be the accurate date since you want the date up to the second. Doesn't it give only 00/00/00 for the hours , minutes and seconds. I think java.util.Date is more accurate.
                              Why not use java.sql.Timest amp?

                              Comment

                              Working...