Guest User

Column Retrieval and Comparison With Last Month

a guest
Aug 24th, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. package Test;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.ResultSetMetaData;
  7. import java.sql.Statement;
  8. import java.time.LocalDateTime;
  9. import java.time.LocalTime;
  10. import java.time.format.DateTimeFormatter;
  11.  
  12. public class TimePatterns {
  13. public static void main(String[] args) {
  14. LocalDateTime now = LocalDateTime.now();
  15. DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MM");
  16. // System.out.println(dtf.format(now));
  17. LocalDateTime lastMonth=now.minusMonths(1);
  18. // System.out.println(dtf.format(lastMonth));
  19. try{
  20. Class.forName("oracle.jdbc.driver.OracleDriver");
  21. Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","hr","hr");
  22. Statement stmt=conn.createStatement();
  23. // ResultSet rs=stmt.executeQuery("select id,totalworkhours from NEC ");
  24.  
  25. ResultSet rs = stmt.executeQuery("SELECT * FROM attendance");
  26. ResultSetMetaData rsmd = rs.getMetaData();
  27. int count=rsmd.getColumnCount();
  28. // System.out.println(count);
  29. while(rs.next()) {
  30. for(int i=4;i<=count;i++) {
  31. String name = rsmd.getColumnName(i);
  32. // System.out.println(name.substring(7, 9));
  33. if(name.substring(7, 9).equals(dtf.format(lastMonth))){
  34. // System.out.println("yess");
  35. LocalTime todaysTotal;
  36. String a=rs.getString(i);
  37. if(rs.wasNull()) {
  38. todaysTotal=LocalTime.parse("00:00");
  39. }
  40. else {
  41. todaysTotal=LocalTime.parse(rs.getString(i));
  42. System.out.println(todaysTotal);
  43. }
  44. }
  45. }
  46. }
  47.  
  48. }catch(Exception e) {
  49. System.out.println(e);
  50. }
  51.  
  52. }
  53. }
Add Comment
Please, Sign In to add comment