Results 1 to 6 of 6
- 06-28-2011, 12:52 PM #1
Inserting current date to oracle Date data type
Hi,
I know this is simple, and its really getting on my nerves. Heres my code...
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.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());
Very many thanks
NOTE:
Already tried
- to_char(sysdate, 'DD-MON-YYYY')
- to_date(sysdate, 'DD-MON-YYYY')
- sysdate
- 06-28-2011, 12:53 PM #2
to clear my concern, these are the different errors
-ORA-00932: inconsistent datatypes: expected NUMBER got DATE
-ORA-00936: missing expressionLast edited by bartolay13; 06-28-2011 at 12:54 PM. Reason: wrong grammar
- 06-28-2011, 02:23 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 06-29-2011, 06:06 AM #4
timer is the different datatype.. i used setTime but datatype is number(6,0).
thanks
- 06-29-2011, 09:24 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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?
- 02-16-2012, 09:26 AM #6
Member
- Join Date
- Feb 2012
- Posts
- 1
- Rep Power
- 0
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
-
Compare Mysql date with current date
By gioarvan in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 04-24-2011, 05:19 PM -
inserting date in oracle database
By jackjosh_in in forum Advanced JavaReplies: 6Last Post: 05-23-2010, 06:38 PM -
Compare date input to database with current date
By hungleon88 in forum Advanced JavaReplies: 2Last Post: 11-25-2008, 08:10 AM -
Inserting current date into a DB table
By Java Tip in forum Java TipReplies: 0Last Post: 02-15-2008, 08:38 AM -
Getting current date using dual (Oracle)
By Java Tip in forum Java TipReplies: 1Last Post: 02-13-2008, 04:46 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks