Forums Register Login

Inserting Date in database

+Pie Number of slices to send: Send
I am trying to insert date in database, I have primary key EVENTID with data type Date:
This part of my class where i am performing this function
package DBAccess;
import DBAccess.*;
import java.io.* ;
import java.util.*;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
Date today = new Date();
This is my function
public void createEvent(String Attendees,String Notes, String Check)
{
try
{
Context context = new InitialContext();
DataSource ds = (DataSource) context.lookup( "itarchutil-datasource" );
Connection con = ds.getConnection();
Statement stmt = con.createStatement();
stmt.executeUpdate ("Insert into sandbox.event(EVENTID,OTHERATTENDEE,NOTES,APPROVE)values('" + today + "','" + Attendees + "','" + Notes + "','" + Check + "')");
stmt.close();
con.close();
}
catch (Exception e)
{
System.out.println("Error:" + e);

}

}
I am getting the following errors:
H:\itads\source\DBAccess\ProjectDatabase.java:11: 'class' or 'interface' expected
[javac] today = new Date();
H:\itads\source\DBAccess\ProjectDatabase.java:298: cannot resolve symbol
[javac] symbol : variable today
[javac] location: class DBAccess.ProjectDatabase
[javac] stmt.executeUpdate ("Insert into sandbox.event(EVENTID,OTHERATTENDEE,NOTES,APPROVE)values('" + today + "','" + Attendees + "','" + Notes + "','" + Check + "')");

Can any one help??
Thanks
^
+Pie Number of slices to send: Send
There is Date class in both java.util and java.sql package. So a collision occurs. Use java.sql.Date explicitly.
+Pie Number of slices to send: Send
I can't take 1 package out boz i am using some classes which are in util package, i tried to do this with my import statement
package DBAccess;
import DBAccess.*;
import java.io.* ;
import java.util.*;
import java.sql.*;
import java.sql.Date;
import javax.sql.*;
import javax.naming.*;
Same errors.
plz help
+Pie Number of slices to send: Send
java.sql.Date today = new java.sql.Date();
+Pie Number of slices to send: Send
Sorry Pardeep i got confused before. Here's what i want to do, i want that
date should be converted into string format, so that i can use it as primary key.
sql and util has tostring() method but different functionality.
sql toString() converts into mm-dd-yy.
util toString() converts into string.
so i have to use util package.
My eventid in database is primary key with varchar2 datatype.
this is what i am doing now
package DBAccess;
import DBAccess.*;
import java.io.* ;
import java.util.*;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;

java.util.Date today = new java.util.Date();
String Id = today.toString();
My insert query is like this :
stmt.executeUpdate ("Insert into sandbox.event(EVENTID,OTHERATTENDEE,NOTES,APPROVE)values('" + Id + "','" + Attendees + "','" + Notes + "','" + Check + "')");
I am getting these errors now:
:\itads\source\DBAccess\ProjectDatabase.java:11: 'class' or 'interface' expected
[javac] java.util.Date today = new java.util.Date();
[javac] ^
[javac] H:\itads\source\DBAccess\ProjectDatabase.java:13: 'class' or 'interface' expected
[javac] String Id = today.toString();
[javac] ^
[javac] H:\itads\source\DBAccess\ProjectDatabase.java:300: cannot resolve symbol
[javac] symbol : variable Id
[javac] location: class DBAccess.ProjectDatabase
[javac] stmt.executeUpdate ("Insert into sandbox.event(EVENTID,OTHERATTENDEE,NOTES,APPROVE)values('" + Id + "','" + Attendees + "','" + Notes + "','" + Check + "')");
[javac]
+Pie Number of slices to send: Send
A couple of things. First, since you have added the "package DBAccess;" statement, there is no need to have the statement "import DBAccess.*;" since the compiler will automatically include those.
But your problem stems from the fact that you haven't put your code inside of a "class" declaration. You need to do this:

This makes "today" an instance variable. If that's not what you want, then put it inside of a method definition.
[ December 18, 2003: Message edited by: Wayne L Johnson ]
+Pie Number of slices to send: Send
can anyone tell me how to solve this problem.
+Pie Number of slices to send: Send
Guys it is working now, but i have 1 problem, i want that date should be into numbers. Right now it is storing like this in database:
Thu Dec 18 11:36:07 EST 2003
I want that it should break it down into numbers and also include time with seconds.
Can anyone tell me how to do that??
+Pie Number of slices to send: Send
Look at the java.text.SimpleDateFormat class to see how you can format your Date to any format you want.
[ December 18, 2003: Message edited by: Wayne L Johnson ]
+Pie Number of slices to send: Send
Just change the date format if you want it to be displyed in some other format ex: "yyyy-MM-dd"
SimpleDateFormat myFormat = new SimpleDateFormat("MM-dd-yyyy");
String myDate = myFormat.format(new Date());
+Pie Number of slices to send: Send
I tried what you guys advised to me and it compiles fine, but when i
try to run the page i am getting this error:
10.4.5 404 Not Found
The server has not found anything matching the Request-URI. No indication is given of whether the condition is temporary or permanent.
If the server does not wish to make this information available to the client, the status code 403 (Forbidden) can be used instead. The 410 (Gone) status code SHOULD be used if the server knows, through some internally configurable mechanism, that an old resource is permanently unavailable and has no forwarding address.

These are the changes i did:
package DBAccess;
import DBAccess.*;
import java.io.* ;
import java.util.*;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
import java.text.*;
public class ProjectDatabase
{
java.util.Date today = new java.util.Date();
SimpleDateFormat myFormat = new SimpleDateFormat("yyMMddHHmmssZ");
String Id = myFormat.format(today);
}
This is my sql query
stmt.executeUpdate ("Insert into sandbox.event(EVENTID,OTHERATTENDEE,NOTES,APPROVE)values('" + Id + "','" + Attendees + "','" + Notes + "','" + Check + "')");
Can anyone help what i did wrong here. My other pages are running fine.
+Pie Number of slices to send: Send
Can anyone please help me on this issue ??
+Pie Number of slices to send: Send
What data type is EVENTID in the database? If its a date, use PreparedStatement. You mention that it is also appearing the wrong way. Where is it appearing the wrong way? If it's a date in the database, its the tool displaying the date that is formatting it...
Get out of my mind! Look! A tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1688 times.
Similar Threads
Eclips error
Null Pointer Exception
Display information
jdbc connect Access
JDBC and Servlet
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 07:11:56.