Results 1 to 6 of 6
  1. #1
    bartolay13's Avatar
    bartolay13 is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Red face Inserting current date to oracle Date data type

    Hi,

    I know this is simple, and its really getting on my nerves. Heres my code...

    Java Code:
    String sql = "insert into exams (title, instructions, use_timer, timer, date_created, author) " +
    					"values (?,?,?,?, to_char((SELECT TO_CHAR(SYSDATE, 'DD-MON-YYYY') FROM dual), 'DD-MON-YYYY'),?)";
    			preparedStatement = database.getConnection().prepareStatement(sql);
    
    			preparedStatement.setString(1, exam.getTitle());
    			preparedStatement.setString(2, exam.getInstructions());			
    			preparedStatement.setBoolean(3, exam.isUse_timer());
    			preparedStatement.setTime(4, exam.getTimer());			
    			preparedStatement.setInt(5, exam.getAuthor());
    my problem is i just want to add the current database date (as you can see in the sql string). I dont want java preparedStatement to go through this because its going to call different date classes and besides oracle has it.

    Very many thanks

    NOTE:

    Already tried
    - to_char(sysdate, 'DD-MON-YYYY')
    - to_date(sysdate, 'DD-MON-YYYY')
    - sysdate

  2. #2
    bartolay13's Avatar
    bartolay13 is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    to clear my concern, these are the different errors

    -ORA-00932: inconsistent datatypes: expected NUMBER got DATE
    -ORA-00936: missing expression
    Last edited by bartolay13; 06-28-2011 at 12:54 PM. Reason: wrong grammar

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,797
    Rep Power
    19

    Default

    What is the datatype of the created_date column?
    If it's DATE then SYSDATE will do you.
    All that to-charing is nonsense.

    INSERT INTO exams (title, instructions, use_timer, timer, date_created, author)
    VALUES (?,?,?,?, SYSDATE, ?)

    If it doesn't work then there's something else wrong, and you'll have to post that problem.

  4. #4
    bartolay13's Avatar
    bartolay13 is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    timer is the different datatype.. i used setTime but datatype is number(6,0).
    thanks

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,797
    Rep Power
    19

    Default

    Right.
    So you have more than one problem.
    You first asked about SYSDATE for a date/timestamp type of column. I explained that you should just use SYSDATE. Don't faff about with to-char rubbish.

    The other problem is with the timer column.
    If timer is a NUMBER then why do you expect setTime to work with it?

  6. #6
    januv is offline Member
    Join Date
    Feb 2012
    Posts
    1
    Rep Power
    0

    Default Re: Inserting current date to oracle Date data type

    how to insert a date (MM-dd-yy hh:mm:ss) format value from a file and store in msacess database.I tried with the pgm below but am getting datatype mismatch error.When i use setDate(),it says PreparedStatement doesnt not have this.Please help me.
    import java.sql.*;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.Reader;
    import java.io.Writer;
    import java.io.BufferedReader;
    import java.util.StringTokenizer;
    import java.sql.PreparedStatement;
    import java.sql.Date;
    import java.util.*;

    public class parsingmed{

    static String inpath = "C:/cdr";
    static int linenumber=0;
    static Double key1,key2;
    static String key,key3,key4,key5;
    public static void main(String[]args) throws IOException{
    try{
    StringBuilder contents=new StringBuilder();
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con=DriverManager.getConnection("jdbc:odbc:abc");
    Statement st=con.createStatement();

    File Folder = new File(inpath);
    File files[]=Folder.listFiles();
    for(int i = 0;i<files.length
    ; i++){
    BufferedReader br=new BufferedReader(new FileReader(files[i]));
    String str="";
    String value="";
    StringTokenizer stri=null;
    while((str=br.readLine())!=null)
    {
    linenumber++;
    stri=new StringTokenizer(str,",");
    while(stri.hasMoreTokens())
    {
    String key=stri.nextToken();
    Double key1=Double.parseDouble(stri.nextToken());
    Double key2=Double.parseDouble(stri.nextToken());
    String key3=stri.nextToken();
    String key4=stri.nextToken();
    String key5=stri.nextToken();
    /*PreparedStatement ps=con.prepareStatement("INSERT INTO file(ID,sourcemobilenumber,destinationmobilenumber ,startdatetime,enddatetime,duration)VALUES(?,?,?,? ,?,?)");
    ps.setString(1,key);
    ps.setDouble(2,key1);
    ps.setDouble(3,key2);
    ps.setString(4,key3);
    ps.setString(5,key4);
    ps.setString(6,key5);
    ps.executeUpdate();*/
    System.out.println(key+"\t"+key1+"\t"+key2+"\t"+ke y3+"\t"+key4+"\t"+key5+"\n");
    }}}}
    catch (IOException ex) {
    System.out.println(ex);

    }
    catch (ArrayIndexOutOfBoundsException ar) {
    System.out.println(ar);
    }
    catch(ClassNotFoundException e){
    System.out.println(e.toString());}
    catch(SQLException e){
    System.out.println(e.toString());
    }
    }
    }

Similar Threads

  1. Compare Mysql date with current date
    By gioarvan in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 04-24-2011, 05:19 PM
  2. inserting date in oracle database
    By jackjosh_in in forum Advanced Java
    Replies: 6
    Last Post: 05-23-2010, 06:38 PM
  3. Compare date input to database with current date
    By hungleon88 in forum Advanced Java
    Replies: 2
    Last Post: 11-25-2008, 08:10 AM
  4. Inserting current date into a DB table
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-15-2008, 08:38 AM
  5. Getting current date using dual (Oracle)
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 02-13-2008, 04:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •