• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Inserting Date in database

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
^
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is Date class in both java.util and java.sql package. So a collision occurs. Use java.sql.Date explicitly.
 
Deepak Chawla
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java.sql.Date today = new java.sql.Date();
 
Deepak Chawla
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can anyone tell me how to solve this problem.
 
Deepak Chawla
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone please help me on this issue ??
 
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
She said she got a brazillian. I think owning people is wrong. That is how I learned ... tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic