
package ejerciciojdbc1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;

public class EjercicioJDBC1 {

    public static void main(String[] args) {
        try {
              //    CONEXION MySQL
              Class.forName("com.mysql.jdbc.Driver");
              Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/biblioteca", "root", "");  
/*              
              //    CONEXION Oracle
              Class.forName("oracle.jdbc.driver.OracleDriver");
              Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "scott", "tiger");  
*/
              String sql = "SELECT * FROM libros";
              Statement statement = connection.createStatement();
              ResultSet resultSet = statement.executeQuery(sql);
              while (resultSet.next()) {
                  System.out.println("id_libro :       " + resultSet.getString(1));
                  System.out.println("título :         " + resultSet.getString(2));
                  System.out.println("género :         " + resultSet.getString(3));
                  System.out.println("fecha edición :  " + new SimpleDateFormat("yyyy-MM-dd").format(resultSet.getDate(4)));
                  System.out.println("numero páginas : " + resultSet.getInt(5));
                  System.out.println("premiado:        " + resultSet.getInt(6));
                  System.out.println("--------------------------------------------------");
              }    
              connection.close();
         } catch(Exception e){
            e.printStackTrace();           
         }     
    }    
}
