I formatted my jspinner as:
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
JSpinner.DateEditor de = new JSpinner.DateEditor(jSpinner1, "MM/dd/yyyy");
jSpinner1.setEditor(de);
and try to insert the value of jSpinner to ms db:
String SQLString = "INSERT INTO Table1(DateToday)VALUES(?)";
stmt = con.prepareStatement(SQLString);
stmt.setDate(1, new java.sql.Date(sdf.format(jSpinner1.getValue())));
but I still get an error.
Please do me some favor if you could give any sample code to get it right.
Many many thanks...
For SQL Server, you could use a string value
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
....
stmt.setString(1, sdf.format(jSpinner1.getValue()));
but the date should have worked so the error is likely that you have some constraint on the table that is not satisfied.
Related
I want to get the date in DATE column and want to compare with the current date. if the date is less than current date then I want to SET the value of PERMISSION column allow. But my Query doesn't execute.
Which I have tried is given in my below code.
Date date1 = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//current date
Date date = new Date();
date1 = date;
CurrentDate.setText(date1.toString());
String UpdateQuery = null;
String Allow = "allow";
UpdateQuery = "UPDATE company SET permission = '"+Allow+"' WHERE date < ?";
pst = conn.prepareStatement(UpdateQuery);
SimpleDateFormat dateFormate = new SimpleDateFormat("yyyy-MM-dd");
String addDate = CurrentDate.getText();
pst.setString(2, addDate);
pst.executeUpdate();
System.out.println("Updated");
I want to get a value of permission table to SET to "allow" when the update query is executing;
pst.setString(2, addDate);
I think this should be changed to:
pst.setString(1, addDate);
because you only have one parameter in your prepared statement.
Also, when comparing dates, you need to enclose them in single quotes, so changing your UpdateQuery string to
"UPDATE company SET permission = '"+Allow+"' WHERE date < '?' ";
is also a necessary step.
Date today = new Date();
SimpleDateFormat df = new SimpleDateFormat ("dd/MM/yy");
String date = df.format(today);
System.out.println(date);
PreparedStatement sql = con.prepareStatement("select count(*), name, status from tablename where file_date = date group by name, status");
ResultSet rs = sql.executeQuery();
However, the sysdate format in SQL is dd/MM/yy
But the date format in Eclipse is yyyy-MM-dd HH:mm:ss.fff
How do I convert it in query so that I can get dd/MM/yy format?
What about passing a true SQL Date, don't bother about internal string representation for dates.
PreparedStatement sql = con.prepareStatement("select count(*), name, status from tablename where file_date = ? group by file_name, status");
sql.setDate(1, new java.sql.Date(new java.util.Date().getTime());
My java code generates two Strings:
String myDate = "10/10/2013";
String myTimestamp = "2013-10-09 14:30:20";
I need to feed these values to a prepared statement, so that I could upload them using jdbc to Teradata
Here is what I tried :
String in = " INSERT INTO " + myTab + " VALUES (?,?) ";
PreparedStatement prst = null;
prst = connection.prepareStatement(in);
// add date
prst.setDate(1, (Date) new SimpleDateFormat("MM/dd/yyyy").parse(myDate));
//add timestamp
prst.setDate(2, (Date) new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(myTimestamp));
The above code compiles but does not work. I get an empty string error . How can I convert a String into Teradata types DATE, TIMESTAMP in order to add them to the prepared statement ?
You could use the java.sql.Date constructor that takes a long, by using Date#getTime() and changing from
prst.setDate(1, (Date) new SimpleDateFormat("MM/dd/yyyy").parse(myDate));
to something like
prst.setDate(1, new java.sql.Date(new SimpleDateFormat("MM/dd/yyyy")
.parse(myDate)).getTime());
and the other one
prst.setDate(2, new java.sql.Date(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.parse(myTimestamp)).getTime());
Try this:
prst.setDate(1, new java.sql.Date(new SimpleDateFormat("MM/dd/yyyy").parse(myDate).getTime()));
This will convert your date then create a new sqlDate.
prst.setTimestamp(2, new java.sql.Timestamp(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(myTimeStamp).getTime());)
for the timestamp use setTimeStamp not setDate.
I have created a table in MySQL as :
CREATE TABLE scheduled(sid INT,id INT,tweet VARCHAR(255),sdate DATE,
stime TIME,PRIMARY KEY(sid),FOREIGN KEY(id) REFERENCES usercred(id));
I receive both Date and Time from the HTML input field. Date received from the HTML field looks like :
4/30/2014
How can I map this in Java ? After receiving both Date and Time and after mapping them correctly , I will commit the transaction or will update the table/entry.
could use the parse() method in the SimpleDateFormat object:
SimpleDateFormat sdf = new SimpleDateFormat("M-dd-yyyy");
String dateInString = "4-30-1982"";
Date date = sdf.parse(dateInString);
System.out.println(date);
In JDBC-layer inside PreparedStatement or ResultSet you work with the mapping java.sql.Date (for SQL-DATE) and java.sql.Time (for SQL-TIME). Then you can wrap both types like:
java.sql.Date sqlDate = ...; // from ResultSet
DateFormat df = new SimpleDateFormat("MM/dd/yyyy"); // or M/d/yyyy
String htmlFormat = df.format(sqlDate);
And in reverse:
String htmlFormat = ...;
DateFormat df = new SimpleDateFormat("MM/dd/yyyy"); // or M/d/yyyy
java.util.Date d = df.parse(htmlFormat);
java.sql.Date sqlDate = new java.sql.Date(d.getTime());
// use result in PreparedStatement for INSERT or UPDATE
Attention: Both approaches use the standard timezone of the server where this code is running. In case of doubt you should probably set the timezone UTC. Similar code for the TIME-part.
I have this MySQL Date data for 6 months in this format:
2010-01-01 to 2010-07-01
But from the UI the ToDate and FromDate are passed in this format:
Jan 1, 2010 and July 1, 2010
Please tell me how can I convert this data into MySQL equivalent format?
First create a SimpleDateFormat for parsing your input from the UI:
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy");
Next parse an input into a java.sql.Date (which is unfortunately named and different from java.util.Date). So for example:
java.sql.Date date = new java.sql.Date(sdf.parse(fromDate).getTime());
Finally use the date to pass to JDBC when making your database queries. Such as:
Connection con; // assuming you have a database connection
PreparedStatement ps = con.prepareStatement("SELECT * FROM table WHERE x = ?");
ps.setDate(1, date);
ResultSet resultSet = ps.executeQuery();