This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JDBC and the fly likes PreparedStatement/update query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "PreparedStatement/update query" Watch "PreparedStatement/update query" New topic
Author

PreparedStatement/update query

ashirvad jain
Ranch Hand

Joined: Nov 09, 2006
Posts: 50
hello friends,

I have prob with update Query..

1)

PreparedStatement ps=conn.prepareStatement("UPDATE tablename SET exitdate=?,exittime=? WHERE mobileno=?");

ps.setString(1,dateOfEntry);
ps.setString(2,timeOfEntry);
ps.setLong(3,mobileno);

2)

PreparedStatement ps=conn.prepareStatement("UPDATE tablename SET exitdate=? WHERE mobileno=?");

ps.setString(1,dateOfEntry);
ps.setLong(2,mobileno);


the (1) not executing wid no error.
(2) works fine ..confuse with this behaviour..

I have MySqlSERVER 5.0
and JSP 1.2

please help me.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You seem to be using Strings for Date values. Why are you not using a date data type?
[ January 17, 2007: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
ashirvad jain
Ranch Hand

Joined: Nov 09, 2006
Posts: 50
does it make any differences?
Malith Yapa
Ranch Hand

Joined: Jan 17, 2007
Posts: 45
Theoritically your statement should work fine.. (upto my knowledge) I mean I have worked on much more complicated statements.

If an error doesn't occur my guess is that you didn't properly initialize the fields you're pa.. if anythings wrong with the database or your sql syntax an SQLException should occur.. make sure you catch all of'em.

You better also use a java.sql.Date object instead of a string to set up the date. If you have a String represantation of the date creating a date object is really easy. check out the doc. otherwise you can get a string from the calendar class. There has been a few topics around check them out..
Malith Yapa
Ranch Hand

Joined: Jan 17, 2007
Posts: 45
Theoritically your statement should work fine.. (upto my knowledge) I mean I have worked on much more complicated statements.

If an error doesn't occur my guess is that you didn't properly initialize the fields you're pa.. if anythings wrong with the database or your sql syntax an SQLException should occur.. make sure you catch all of'em.

You better also use a java.sql.Date object instead of a string to set up the date. If you have a String representation of the date creating a date object is really easy. check out the doc. otherwise you can get a string from the calendar class. There has been a few topics around check them out..
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Originally posted by ashirvad jain:
does it make any differences?


Yes:
  • From a database point of view you weaken your data constraints by using a character data type. The field might be called "date" but you can insert "yuyiuyu" as a valid date.
  • It removes the need to ensure the formatting of the date remains consistant between the application and the database.

  •  
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: PreparedStatement/update query
     
    Similar Threads
    Converting String to Clob
    prepared statement and tomcat in unix environment
    PostgreSQL prepared statement failure
    Cannot perform SQL UPDATE null
    How does one insert a column value into an existing record?