aspose file tools*
The moose likes JDBC and the fly likes Inserting Date in database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Inserting Date in database" Watch "Inserting Date in database" New topic
Author

Inserting Date in database

Deepak Chawla
Ranch Hand

Joined: Nov 19, 2003
Posts: 50
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
^
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

There is Date class in both java.util and java.sql package. So a collision occurs. Use java.sql.Date explicitly.


Groovy
Deepak Chawla
Ranch Hand

Joined: Nov 19, 2003
Posts: 50
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
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

java.sql.Date today = new java.sql.Date();
Deepak Chawla
Ranch Hand

Joined: Nov 19, 2003
Posts: 50
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]
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
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 ]
Deepak Chawla
Ranch Hand

Joined: Nov 19, 2003
Posts: 50
can anyone tell me how to solve this problem.
Deepak Chawla
Ranch Hand

Joined: Nov 19, 2003
Posts: 50
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??
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
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 ]
Maria Peter
Greenhorn

Joined: Oct 27, 2003
Posts: 29
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());
Deepak Chawla
Ranch Hand

Joined: Nov 19, 2003
Posts: 50
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.
Deepak Chawla
Ranch Hand

Joined: Nov 19, 2003
Posts: 50
Can anyone please help me on this issue ??
Dana Hanna
Ranch Hand

Joined: Feb 28, 2003
Posts: 227
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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inserting Date in database