Get Date from the Database

1

I need to get the date (in particular the year) from the database to integrate it into Java

    String now = "SELECT DATE_FORMAT(NOW( ), '%Y' )";
          try {
        java.sql.Statement sp=cn.createStatement();
        ResultSet RS = sp.executeQuery(now);
        while(RS.next()){
            ahora = RS.getInt("Now");  
            System.out.println(ahora);
        }

however the "Now" does not recognize it inside the table ... (since it does not exist) how can I get that data to implement it to the variable "now"

    
asked by Juan Pablo 20.11.2018 в 18:03
source

1 answer

5

Try aliasing the column like this:

String now = "SELECT DATE_FORMAT(NOW( ), '%Y' ) AS ahora";
      try {
    java.sql.Statement sp=cn.createStatement();
    ResultSet RS = sp.executeQuery(now);
    while(RS.next()){
        ahora = RS.getInt("ahora");  
        System.out.println(ahora);
    }
    
answered by 20.11.2018 / 18:06
source