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

attachments by outlook with strange filename using javax getFileName
How to add list in list
About Java Simple Algorithm
Faild to Start Spring Boot +thymeleaf application in IntelliJ
getLastIndexOf(int item) LinkedList
How does Spring's InternalResourceViewResolver decide how to parse file?
Android Button initialized but not working
How to remove property fields that ObjectMapper inserts into my JSON Output?
Base Entity and Serializable
javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist in hibernate
What does spring-boot-starter-parent exactly do in pom file?
Java: rootPane packages does not exist
No issuer certificate for certificate in certification path found
How to make a Java program be more efficient? (take up less memory, etc) [migrated]
Deserializing JSON to polymorphic object model using #JsonTypeInfo & #JsonSubTypes not working?
increment NOT working in javafx controller

Categories

HOME
testing
arduino-uno
getelementsbytagname
survey
retrofit
ssl-client-authentication
dax
mapserver
windows-azure-storage
msp430
ghc
pc
connection-string
jquery-ajaxq
dcevm
tapestry
blazemeter
h2db
poltergeist
typo3-6.2.x
elasticsearch-net
bluestacks
nameservers
.net-4.0
lxd
rotational-matrices
cloud-code
google-api-nodejs-client
html5-fullscreen
catalog
scaffold
unspecified
vapor
streamreader
graphenedb
node-gyp
nxlog
arena-simulation
network-flow
jedis
qsslsocket
dds
worksheet
jxcore
worker-thread
nsarray
r-forge
gcsfuse
lync-client-sdk
specrun
android-fonts
removechild
infix-notation
login-control
transmitfile
fuzzy-search
multiple-regression
holder.js
captivenetwork
dataview
pyke
cloudbees
eclipse-clp
t4mvc
collapse
wireshark-dissector
browser-bugs
emailrelay
android-listview
cakephp-3.1
twirl
arcanist
graph-drawing
sankey-diagram
createprocessasuser
remobjects
block-device
oxwall
braille
datagridviewcolumn
didselectrowatindexpath
ember-charts
cloud-connect
openlaszlo
multiprocessor
quantlib-swig
.aspxauth
access-rights
back-stack
itmstransporter
coderush
labwindows
appconkit
windows-phone-7.1.1
blackberry-playbook
locationlistener
celltable
sql-server-profiler
gamequery
ctp4
avatar
3gp

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App