MySQL query to retrieve data in JTable from MySQL DB is failing in Java Code but work fine in SQL Workbench
Following query:
SELECT date_format(emp_date,'%d-%b-%Y') as emp_date,emp_name,emp_accno,emp_amt,pay_mode,e_comp,e_remark FROM emppayment";
While retrieving i need "dd-mmm-yyyy" (04-Apr-2020) format so i am using above date_format to convert as per requirement. While executing in JTable i getting following error.
Error: java.sql.SQLException: Bad format for DATE '20-Mar-2020' in column 1.
String dquery = "SELECT date_format(emp_date,'%d-%b-%Y') as emp_date, emp_name,emp_accno,emp_ifsc,sal_month,emp_amt,pay_mode,e_comp,e_remark FROM emppayment;
pst = con.prepareStatement(dquery);
rs = pst.executeQuery();
DefaultTableModel tm = (DefaultTableModel) jTable1.getModel();
tm.setRowCount(0);
while (rs.next()) {
Object obj[]= {
rs.getString("emp_date"),
rs.getString("emp_name"),
rs.getString("emp_accno"),
rs.getString("emp_ifsc"),
rs.getString("sal_month"),
rs.getString("emp_amt"),
rs.getString(pay_mode),
rs.getString(e_comp),
rs.getString(e_remark)};
tm2.addRow(obj);
}
Please guide me
As to your error, I wonder if the type of the column in the data model backing your JTable specifies a date-time data type while your code is providing a String
object. So, a data type mismatch.
I would do such formatting in your Java app rather than in your SQL.
LocalDate
Retrieve the value from your DATE
column as a LocalDate
using a JDBC driver compliant with JDBC 4.2 or later. Such drivers are able to directly exchange java.time objects with the database.
LocalDate ld = myResultSet.getObject( … , LocalDate.class ) ;
DateTimeFormatter
Automatically localize for presentation to the user.
Locale locale = Locale.CANADA_FRENCH ; // Or Locale.US and so on.
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.MEDIUM ).withLocale( locale ) ;
String output = ld.format( f ) ;
If you insist on hard-coding a specific format, specify your formatting pattern.
DateTimeFormatter.ofPattern( "dd-MMM-uuuu" , Locale.UK ) ;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.