• Post Reply Bookmark Topic Watch Topic
  • New Topic

To change format for date  RSS feed

 
pooja rathi
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should use a PreparedStatement with place-holders if you're going to try and use date columns. If you do that then you just have to provide a java.sql.Date object -- no formatting required -- and your JDBC driver will take care of knowing how to format the date for the database. In fact you should use a PreparedStatement any time you think it's a good idea to build an SQL command via string concatenation. Because it isn't a good idea.
 
pooja rathi
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:You should use a PreparedStatement with place-holders if you're going to try and use date columns. If you do that then you just have to provide a java.sql.Date object -- no formatting required -- and your JDBC driver will take care of knowing how to format the date for the database. In fact you should use a PreparedStatement any time you think it's a good idea to build an SQL command via string concatenation. Because it isn't a good idea.


can you please elaborate your answer by giving an example through code
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure. Here's a link to Oracle's tutorial about PreparedStatement: http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html
 
pooja rathi
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Sure. Here's a link to Oracle's tutorial about PreparedStatement: http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html

Its very complicated to change the whole code in prepared statement right now can you please suggest me some changes here in this code only so that i can get date and time along with the data
 
pooja rathi
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pooja rathi wrote:
Paul Clapham wrote:Sure. Here's a link to Oracle's tutorial about PreparedStatement: http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html

Its very complicated to change the whole code in prepared statement right now can you please suggest me some changes here in this code only so that i can get date and time along with the data
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not really, no. The code is a quite a mess anyway, rewriting it could improve it a great deal.
 
pooja rathi
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Not really, no. The code is a quite a mess anyway, rewriting it could improve it a great deal.

please check it now ,i have edited i just want to put date and time in insert query but getting an syntax error
 
pooja rathi
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pooja rathi wrote:
Paul Clapham wrote:Not really, no. The code is a quite a mess anyway, rewriting it could improve it a great deal.

please check it now ,i have edited i just want to put date and time in insert query but getting an syntax error

anybody knows the solution please help.!!
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you have an error, you should post the error message too when asking for help. I think it is this line:

There is no variable called "time".

But Paul Clapham's advice is good: you really should rewrite this program.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First things first - what is the structure of the table you are inserting into?
Which database are you using? Most times you don't actually need to separate "date" and "time" into two columns. One value stores both the date AND time together.

Here is a bit of your code modified to use a prepared statement instead of that SQL string concatenation you were using.
You need to work out how to get appropriate values of the variables.
Also I think your insertTableSQL is incorrect. You should be referring to column names, and I don't think Date [i] is a valid database column name :-)



Hope this helps some

Stefan
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As said back in Paul's first post, use a PreparedStatement.
For starters it will let you throw away all that concatenation.

But, since you are logging the SQL created, you could always post it for us.
But I will say (even without the full statement) that this:
Date [i]
can't be a valid column name, surely?
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!