java


Using java.sql.Date


I am using jdbc to call a sql query ".... where mydate like '?%' "
and I want to pass the parameter to the query with:
PreparedStatement ps;
ps.setDate(1, new java.sql.Date.valueOf("2000-01-13"));
But this last line is not compiling, and I don't know why. When i enter the date directly into the query above, as in ".. where mydate like '2000-01-13%'", it works.
Apart from the basic compilation error (just remove new), I spot 2 serious problems:
Given the fact that LIKE works, your mydate field is apparently of a varchar type instead of a fullworthy date, datetime or timestamp type. This is recipe for problems. You should always use the right data type for the information the field holds.
% cannot be put after preparedstatement placeholder ?. It has to be set in the value directly. However, this works with String values only (and thus varchar field types). For a fullworthy date, datetime and timestamp type you'd rather use =, <, > or BETWEEN instead.
There are 2 solutions:
Change the data type of the field to be a real date, datetime or timestamp, so that you can use the proper SQL syntax such as WHERE mydate BETWEEN ? AND ?.
Use preparedStatement.setString(1, "2000-01-13%") instead and remove those singlequotes around the placeholder ? as well so that it ends like WHERE mydate LIKE ?.
See also:
Preparedstatement tutorial
MySQL data types
You'll want to use:
java.sql.Date.valueOf("2000-01-13")
That is, do not use new in front of it. By using new, you are telling the compiler you want to create a new object. Since valueOf is a static method, you do not need to create an object in order to call it.
Regardless, the reason it does not compile is because new java.sql.Date.valueOf("str") is not a valid statement. You would have to say new java.sql.Date() with parentheses if you wanted to create a new instance of Date, which you do, but by a different means.

Related Links

Is this the best way to initialize an array
onetime login in fragmentactivity
How can i add custom icon onto my personal launcher screen programmatically
Convert a long to an int cutting off the overflow
Maximum size of 2D array in Java
is it possible to assign an bean id to spring integration inbound adapter with DSL
ContextWrapper.java has a lot of errors in logcat [duplicate]
Java code does not get udp packet in a specific port
Java sqlite row disappear after Select query
Dependency on IText 4.2.0 vs ITextPdf 5.5.9
Setting Postgresql schema with hibernate parameters
How does the Java reverse array method works?
Need to save two tables from different jsp pages with foreign key relationship using hibernate dao implementation method
Android customized adapter with both editText and textView
understand Java 8 Stream [closed]
JTextArea entry bug? [duplicate]

Categories

HOME
amazon-ec2
google-api-php-client
stock
electronics
icloud
convolution
webrequest
esper
add
adfs
ssl-client-authentication
dax
nstableview
visual-studio-cordova
iron-router
workload-scheduler
oxyplot
zapier
clickonce
scriptcs
csrf-protection
kvc
trading
qhull
greendao
replaceall
procdump
ecto
semantic-versioning
az-application-insights
sqlite2
avro4s
nomethoderror
taffy
rainbowtable
pebble-watch
idisposable
composite-key
gcsfuse
imanage
spring-cache
django-scheduler
pintos
url-masking
transmitfile
moveit
rvest
pickadate
php-internals
oauth2client
two-factor-authentication
system.management
maven-tomcat-plugin
coveralls
ionic
sortedlist
skos
notify
oxwall
javafx-webengine
titanium-modules
android-radiobutton
yorick
dealloc
picturefill
didselectrowatindexpath
centos5
gridfs
coverflow
jplaton
eventual-consistency
eventlistener
flashvars
http-unit
unc
dice
frameset
uiviewanimation-curve
punbb
nsobject
genshi
xmlspy
f#-powerpack
propagation
remember-me
mozilla-prism
gin
brewmp
eqatec
paul-graham

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App