I am doing code in Eclipse using Java Swing and MySQL. I am storing Date of birth using Calendar in database.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String DOB=sdf.format(db1.getDate());
I want to retrieve date from database and display in GUI for updating if user want to update.
How can I do that?
String idpop = JOptionPane.showInputDialog(null , "Enter Student ID to update record:");
int sid=Integer.parseInt(idpop);
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/schl","root","root");
String sql = "select * from stud_info where ID='"+sid+"' ";
PreparedStatement ps=con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
if(rs.next()) {
String Sid=rs.getString("ID");
id1.setText(Sid);
String Snm=rs.getString("Name");
nm1.setText(Snm);
SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy");
java.util.Date date = sdf.parse("DOB");
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
db1.setCalendar(calender);
String Sem=rs.getString("Email");
em1.setText(Sem);
String Smb=rs.getString("MobNo");
mb1.setText(Smb);
String Saddr=rs.getString("Address");
addr1.setText(Saddr);
String Sssc=rs.getString("SSCMrks");
ssc1.setText(Sssc);
String Shsc=rs.getString("HSCMrks");
hsc1.setText(Shsc);
}
In that, I am trying for updating records, and for that, I am taking id from a user by pop-up and then It is loading data from the database but for dob, it giving error for parsing. So I want to know how to convert the date to Calendar?? I have removed the code of date after that it gave the current date and other data loading normally.
java.time
I recommend that you use java.time, the modern Java date and time API, for your date work.
This gives you an instance of the modern
LocalDate
type, which is much nicer to work with than the oldCalendar
andDate
classes. At the same time it saves you from any parsing. Edit: Since JDBC 4.2 (out in 2014) the conversion from a date in SQL to a LocalDate is supported (implemented by MySQL for very long now). You’re now ready for the day when either your date picker gets updated to java.time or you pick a more modern date picker. In the meantime, if you need aCalendar
for the date picker that you are currently using, convert like this:No matter if using the modern
DateTimeFormatter
or the old and notoriously troublesomeSimpleDateFormat
or a similar formatter in some other language than Java format pattern letters are case sensitive. If both upper case and lower case of a letter can be used, they have different meanings. For example:yyyy
means year of era while upper caseYYYY
means week-based year (the year that the week number belongs to, not always the same as the calendar year that the date belongs to). Often lower case is for the most used meaning.mm
is for minute of hour while upper caseMM
is for month of year (think: a month is longer than a minute).dd
is for day of month (1–31 in the Gregorian calendar) while upper caseDDD
is for day of year (1–366) (think: the day of year number is typically greater and again not so often used).Links
mm
andMM
: SimpleDateFormat ignoring month when parsing