UpdateQuery doesn't execute. How to fix this code? - java

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.

Related

How to fix NumberFormatException?

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String selectQueryForPatient = "SELECT registration_id, dob FROM patient_registration WHERE registration_id IN(SELECT max(registration_id)FROM patient_registration)";
String insertQuery = "INSERT INTO patient_master_info(patient_id) VALUES (?);";
String id = null;
String dob = null;
int generatedID = 0;
try {
pst = con.prepareStatement(selectQueryForPatient);
rs = pst.executeQuery();
while(rs.next()){
id = Integer.toString(rs.getInt(1));
dob = rs.getString(2);
System.out.println("Id : "+id+" "+"DOB: "+dob);
Date dt = sdf.parse(dob);
dob = sdf.format(dt);
String result = id.concat(dob);
generatedID = Integer.parseInt(result);
}
pst = con.prepareStatement(insertQuery);
pst.setInt(1, generatedID);
pst.execute();
} catch(Exception e) {
e.printStackTrace();
}
For input string: "12919851203" I got java.lang.NumberFormatExceptionexception. How do I fix it?
`
I don't know what you are trying to do, but your problem is that the result of appending a date to an integer value easily exceeds the integer range.
For example, let id be "30" and dob (as read from the database) be "20190123". The result of id.concat(dob) is "3020190123", which is larger than "2147483647", the maximum value for an int.
Your sample value 12919851203 is even much larger (almost 13 billion, while Integer.MAX_VALUE is about 2 billion).
If the value has to be sortable like numbers (where 2 < 10) then you could change the datatype to long or BigInteger.
If the sorting doesn't matter (i.e. it's OK that "2" is larger than "10") then you could even store result as String (into a VARCHAR column).
Or you could explain what problem you want to solve so that we could find a better way to solve the problem.
This answer is not about the number format exception but about the date format mistake you have made.
For the moment, you are parsing 1986-03-01 into the date 1985-12-03 because your String dob contains -. You need two distinct formaters to get your result:
SimpleDateFormat sdfIn = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdfOut = new SimpleDateFormat("yyyyMMdd");
String dob = "1985-03-01";
Date dt = sdfIn.parse(dob);
dob = sdfOut.format(dt);
System.out.println(dob);
19850301
Of course, one could argue about the need to parse into a Date just to get a "similar" String, if you are sure about your data being a date, you can just remove - using :
dob = dob.replaceAll("-", "");
Last thing, you should use java.time API.
String dob = "1985-03-01";
LocalDate date = LocalDate.parse(dob, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
dob = date.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
System.out.println(date);
System.out.println(dob);

How to change the sysdate format in eclipse for queries?

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());

Java JDBC how to pass date in

How can I take the the date value from my html, then add it to the database.
I tried many ways but its not working. I keep getting back a NULL value in the database or an error message "java.text.dateformat.parse(unknown source)"
HTML:
Date Of Birth: <input type="date" name="dob">
JAVA:
String date = request.getParameter("dob");
Date dob1 = new SimpleDateFormat("MM/dd/yyyy"));
PreparedStatement createUser = connection.prepareStatement(
"INSERT into users (dob1)" +
" VALUES ( ?)");{
createUser.setDate(1, (java.sql.Date) dob1);
int newUser = createUser.executeUpdate();
}
String date = request.getParameter("dob");
Date dob1 = new SimpleDateFormat("MM/dd/yyyy"));
This second line is wrong. Change to
Date dob1 = new SimpleDateFormat("MM/dd/yyyy")).parse(date);
edit
By the comment you're using the wrong date format. Use it instead.
Date dob1 = new SimpleDateFormat("yyyy-MM-dd")).parse(date);

How to update date in Date format to a resultset?

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();

SimpleDateFormat value insert to ms db

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.

Categories

Resources