Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package IntroduccionJDBC;
- import java.sql.*;
- import java.util.TimeZone;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author marti
- */
- public class IntroduccionJDBC {
- public static void main(String[] args) {
- try {
- String url = "jdbc:mysql://localhost:3306/sga?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=" + TimeZone.getDefault().getID();
- Class.forName("com.mysql.cj.jdbc.Driver");
- Connection conexion=(Connection)DriverManager.getConnection(url, "root", "admin");
- Statement sentencia=conexion.createStatement();
- String sql="select id_persona, nombre ,apellido from persona";
- ResultSet resultado=sentencia.executeQuery(sql);
- while(resultado.next()){
- System.out.println("Id: "+resultado.getInt(1));
- System.out.println("Nombre: "+ resultado.getString(2));
- System.out.println("Apellido: "+resultado.getString(3));
- System.out.println("____________");
- }
- resultado.close();
- sentencia.close();
- conexion.close();
- } catch (ClassNotFoundException | SQLException ex) {
- ex.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment