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());
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 dateformat=null;
Date i5Date=null;
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String date = i5t1.getText();
i5Date=(Date) formatter.parse(date);
while(rs.next())
{
rs.moveToInsertRow();
rs.updateDate("Date",i5Date);
rs.insertRow();
}
This doesnt update the resultset in date format. May i know what changes i have to make to update the resultset in date format. (Note : - I dont want to update in string format.)
Note: - i have made the connections and the resultset is opened.
The date in result set is a java.sql.Date type. You are trying to format java.util.Date.
You need to convert between them to get this to work assuming you are just rendering the date.
If not you need to make the change directly to your schema. See these SO posts:
ResultSet.getTimestamp("date") vs ResultSet.getTimestamp("date", Calendar.getInstance(tz))
java.util.Date vs java.sql.Date
Update
To retrieve between dates like you are trying to do, you need to do:
Connection conn = null;
PreparedStatement pstmt = null;
conn = getConnection();
String query = "select * from table_name between ? and ?";
pstmt = conn.prepareStatement(query);
pstmt.setDate(1, new java.sql.Date(startDate.getTime()));
pstmt.setDate(2, new java.sql.Date(endDate.getTime()));
ResultSet resultSet = pstmt.executeQuery();
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();
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.
I am trying to set a timestamp in my database using java, however in my table all I get is the date, and no time (i.e., looks like "2010-09-09 00:00:00").
I am using a datetime field on my mysql database (because it appears that datetime is more common than timestamp). My code to set the date looks like this:
PreparedStatement ps = conn.prepareStatement("INSERT INTO mytable (datetime_field) VALUES (?)")
java.util.Date today = new java.util.Date();
java.sql.Date timestamp = new java.sql.Date(today.getTime());
ps.setDate(1, timestamp);
ps.executeUpdate();
How do I set the date to include the time?
Edit: I changed the code as per below, and it sets both the date and the time.
PreparedStatement ps = conn.prepareStatement("INSERT INTO mytable (datetime_field) VALUES (?)")
java.util.Date today = new java.util.Date();
java.sql.Timestamp timestamp = new java.sql.Timestamp(today.getTime());
ps.setTimestamp(1, timestamp);
ps.executeUpdate();
Use java.sql.Timestamp and setTimestamp(int, Timestamp). java.sql.Date is date-only, regardless of the type of the column it's being stored in.
Not exactly sure what you need to use, but
ps.setDate();
expects a column type of Date. So it's normalizing it, removing the time.
Try
ps.setTimetamp();
You could use :
private static String getTimeStamp() {
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return f.format(new Date());
}