cancel
Showing results for 
Search instead for 
Did you mean: 

date timestamp format and JDBC

Former Member
0 Kudos

hi,

i have a table which contains the data type and timestamp type. I am setting up prepared statements - however i have difficulties getting the date right.

I searched help.sap.com and found this code:


import  java.sql.SQLException;

 
String dateString = "2003-03-24";

java.sql.Date date = new java.sql.Date.valueOf(dateString);

PreparedStatement stmt =

   conn.prepareStatement("insert into RESERVATION "

                  + "(ID,  RESERVATION_DATE)"

                  + "values (23, ?)");

try {

   stmt.setDate(1, date);

   stmt.executeUpdate();

} finally {

   stmt.close();

}

i've got the import correct and tried the code but i get the "cannot be resolved or type" error.

How do I get that straight?

thanx in advance, matthias

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi matthias,

Have you imported class PreparedStatement ?

It seems that java runtime cannot resolve one of your symbols, maybe the PreparedStatement class.

Try adding import java.sql.PreparedStatement and let us know

Former Member
0 Kudos

Hi,

I 've got all the imports correct, i reckon.

At least I have got this bunch of guys in my java-file:

package com.wd.jdbc;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.<b>PreparedStatement</b>;

import java.sql.ResultSet;

import java.sql.<b>SQLException</b>;

import java.util.Iterator;

import java.util.List;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import javax.sql.DataSource;

import com.wd.vo.Row;

import com.wd.vo.Table;

I also tried

import java.sql.Date;

but in my method I get the error:

the method goes like this:


	public void insert(Row row) throws SQLException {			

		String  dateString = "20031212"; 
		
		java.sql.Date ndate = new java.sql.Date.valueOf(dateString);
		
		PreparedStatement pstmt3 =
			con.prepareStatement("insert into TAB_VORGANG " +
				"(VORNR, BETRIEB, CREA_DATE) values (?,?,?)");

			pstmt3.setString(1, row.getVornr());
			pstmt3.setString(2, row.getBetrieb());
			pstmt3.setDate(3, ndate);
			pstmt3.execute();
			pstmt3.close();

	}

and <i>java.sql.Date.valueOf</i> is underlined red.

Former Member
0 Kudos

btw this insert works fine with strings - only the date time formats cause error...

Former Member
0 Kudos

java.util.Date d=new Date(String);

java.sql.Date dd=new java.sql.Date(d.getTime());

stmt.setDate(dd);

try this out;

Former Member
0 Kudos

Hi Ashuto

ok - I tried:

		Date dt;
		dt.setDate(24);
		dt.setMonth(3);
		dt.setYear(2005);
		
		PreparedStatement pstmt3 =
			con.prepareStatement("insert into Tab_VORGANG " +
				"(VORNR, BETRIEB, CREA_DATE) values (?,?,?)");

			pstmt3.setString(1, row.getVornr());
			pstmt3.setString(2, row.getBetrieb());
			pstmt3.setDate(3, dt);

now in the last line dt turns red - and the above declarations are marked as deprecated (which i experienced before - apparently we shall use the Calendar now)

former_member185029
Active Contributor
0 Kudos

Ok this should work

java.sql.Date dt=new Date(System.currentTimeMillis());

//For current date only

Please don't import both java.util and java.sql dates as it may lead to confusion.

Ashutosh

Ashutosh Moharir

Former Member
0 Kudos

Hi Noufal,

java.util.Date d=new Date(<b>String</b>);
java.sql.Date dd=new java.sql.Date(d.getTime());
stmt.setDate(dd);

does <b>String</b> have to be set up before as "2003-12-03"?

like this it does not work neither.

Now it says: "the constructor date(String) is undefined"...

Former Member
0 Kudos

hi Ashutosh Moharir ,

<i>java.sql.Date dt=new Date(System.currentTimeMillis());</i>

seems to work, at least I don't get errors - I check if I get an entry in my table and if so come back and give you the points

matthias

Former Member
0 Kudos

Hi,

Try this !

Date d = wdContext.currentContextElement().getExpecteddate();

SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");

String date = sdf.format(d);

Pass this string.. and instead of using setDate .. use setString and give this string value.

It will be automatically set as date in Oracle.

Regards

Bharathwaj

Former Member
0 Kudos

Guys, now i really get trouble.

the webdynpro-view which interacts, with the connection.java

is breaking up - even though i commented all relevant source I get an error in this standard line

" private final IPrivateView1 wdThis;" with corresponding further errors.

I tried to repair an rebuild - no effect.

How did this happen?

Former Member
0 Kudos

ok - i found a backup - i shall setup the scenario again and tehn comeback to award more points - promised.

matthias :-]

Former Member
0 Kudos

Hi

if u lost ur wdThis, wdContext etcc.

try checking for an error in the Task View where there will be single error standing alone try rectifying that and it will automatically solve the remaining errors.

That error will not be merger with the general errors.

try identifying that

Wishes

Krishna kanth

Former Member
0 Kudos

Concerning my date format problem - as Ashutosh Moharir stated "java.sql.Date dt=new Date(System.currentTimeMillis());" I get no error with that code, but neither an entry in my db-table.

What I did is creating a db-tab in the Java Dictionary and deployed it to the J2EE. To the date field I assigned the type "date". Strangely my Quantum Eclipse Plugin tells me the length of the date field to be only (7).

I wonder which kind of date fits in there??? also string fields which I assigned a certain length appear to have a different length-value as shown in my quantum tool...

So - who to believe-NWDevStudio or Quantum or what???

Thanks for some light on that issue, too.

Matthias

Message was edited by: matthias kasig

Former Member
0 Kudos

Hi,

Maybe its this format .. 23Feb 05. Use the codee i have given and in the simple date format give dd-mmm-yy and pass it . This should work

By the by what is the DB .. Both are right i guess

regards

Bharathwaj

Message was edited by: Bharathwaj R

Former Member
0 Kudos

Hi Bharathwaj R,

you are right - the date format is like 23Feb2005 - in the db it appears as 2005-02-23 00:00:00.0

now right before the insert i created this, almost like you proposed (because I don't need a context here) - i simply need the actual date right here:

		java.sql.Date kd_date = new Date(System.currentTimeMillis());
		SimpleDateFormat sdf = new SimpleDateFormat(<b>"dd-MMM-yyyy"</b>);
		kd_date.getTime();
		String date = sdf.format(kd_date);

and then stmt.setDate(3, date);

not stmt.setString... this would not work.

still i get no entry in my db-tab...

Message was edited by: matthias kasig

Former Member
0 Kudos

Hi,

Only if use the string the whole process of getting in that format will work.. Since u r using the date again directly... it wont make any difference

Did u check with setString.. Because i had a pretty similar situation. For date field i gave it in the string format ..(Only difference was i dint use a prepared statement!).

Are all other datas in that record entered.

If u r using EJB throw the Exception in the particular function.. catch it in WebDynpro and see what it is saying.

Regards

Bharathwaj

Message was edited by: Bharathwaj R

Former Member
0 Kudos

hi Bharatwaj,

i am using JDBC - my java skills are more or less basic.

i have a row.java which describes the table-row and a table.java which creates the table based on row.java.

Now i noticed, that in my row.java I set the attributes (also for date and time as String). Is that correct?

public class Row

{

private String vornr;

private String kd_date;

private String kd_time;

...

Answers (4)

Answers (4)

Former Member
0 Kudos

Hi,

I am not very clear about how you are using this class.

But if u r using this class to get the values in the prepared statement. And are u setting the date inside a function in that class.

If so then make it string and assign to the simpledateformat string value and set it in the prepared statement .

Regards

Bharathwaj

Former Member
0 Kudos

hi bharatwaj,

i rebuild my webdynpro and its model. now i get the date written to the db.

it only displays in the wrong column of my table on the webdynpro - but i think i can figure that out.

thanx a lot for your patience.

matthias

Former Member
0 Kudos

Hi,

Check all the errors..Check the last error in that list.. You might have missed some binding ?

Due to that one error the whole code will go haywire .If u solve that it will work..

Regards

Bharathwaj

former_member185029
Active Contributor
0 Kudos

Ok,

try this code then

Date dt;

dt.setDate(24);

dt.setMonth(3);

dt.setYear(2005);

......

......

stmt.setDate(1, date);

Tell me the result.

Ashutosh

former_member185029
Active Contributor
0 Kudos

Hello Matthias,

try

String dateString = "20050324";

and let me know the result.

Ashutosh

Former Member
0 Kudos

hi Ashutosh,

it gives the same error.

😐