-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #236 from CodeSystem2022/develop
Cerramos Semana 12
- Loading branch information
Showing
13 changed files
with
574 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
target/ | ||
!.mvn/wrapper/maven-wrapper.jar | ||
!**/src/main/**/target/ | ||
!**/src/test/**/target/ | ||
|
||
### IntelliJ IDEA ### | ||
.idea/modules.xml | ||
.idea/jarRepositories.xml | ||
.idea/compiler.xml | ||
.idea/libraries/ | ||
*.iws | ||
*.iml | ||
*.ipr | ||
|
||
### Eclipse ### | ||
.apt_generated | ||
.classpath | ||
.factorypath | ||
.project | ||
.settings | ||
.springBeans | ||
.sts4-cache | ||
|
||
### NetBeans ### | ||
/nbproject/private/ | ||
/nbbuild/ | ||
/dist/ | ||
/nbdist/ | ||
/.nb-gradle/ | ||
build/ | ||
!**/src/main/**/build/ | ||
!**/src/test/**/build/ | ||
|
||
### VS Code ### | ||
.vscode/ | ||
|
||
### Mac OS ### | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>UTN</groupId> | ||
<artifactId>Estudiante</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<dependencies> | ||
<!-- https://mvnrepository.com/artifact/com.mysql/mysql-connector-j --> | ||
<dependency> | ||
<groupId>com.mysql</groupId> | ||
<artifactId>mysql-connector-j</artifactId> | ||
<version>8.0.33</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<properties> | ||
<maven.compiler.source>17</maven.compiler.source> | ||
<maven.compiler.target>17</maven.compiler.target> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
</project> |
19 changes: 19 additions & 0 deletions
19
Java/Semana 12/Leccion 12/SistemaEstudiantes/src/main/java/UTN/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package UTN; | ||
|
||
// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`, | ||
// then press Enter. You can now see whitespace characters in your code. | ||
public class Main { | ||
public static void main(String[] args) { | ||
// Press Alt+Intro with your caret at the highlighted text to see how | ||
// IntelliJ IDEA suggests fixing it. | ||
System.out.printf("Hello and welcome!"); | ||
|
||
// Press Mayús+F10 or click the green arrow button in the gutter to run the code. | ||
for (int i = 1; i <= 5; i++) { | ||
|
||
// Press Mayús+F9 to start debugging your code. We have set one breakpoint | ||
// for you, but you can always add more by pressing Ctrl+F8. | ||
System.out.println("i = " + i); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
target/ | ||
!.mvn/wrapper/maven-wrapper.jar | ||
!**/src/main/**/target/ | ||
!**/src/test/**/target/ | ||
|
||
### IntelliJ IDEA ### | ||
.idea/modules.xml | ||
.idea/jarRepositories.xml | ||
.idea/compiler.xml | ||
.idea/libraries/ | ||
*.iws | ||
*.iml | ||
*.ipr | ||
|
||
### Eclipse ### | ||
.apt_generated | ||
.classpath | ||
.factorypath | ||
.project | ||
.settings | ||
.springBeans | ||
.sts4-cache | ||
|
||
### NetBeans ### | ||
/nbproject/private/ | ||
/nbbuild/ | ||
/dist/ | ||
/nbdist/ | ||
/.nb-gradle/ | ||
build/ | ||
!**/src/main/**/build/ | ||
!**/src/test/**/build/ | ||
|
||
### VS Code ### | ||
.vscode/ | ||
|
||
### Mac OS ### | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
-- comencemos con CRUD: (create(insertar),read(leer),update(actualizar),delete(eliminar) | ||
-- listar los estudiantes (read) | ||
USE estudiantes; | ||
SELECT * FROM estudiantes2022; | ||
-- Insertar estudiante "Carla","Roldan","2614561235","roldan@mail.com","Carlos","Peralta","2617894658","peralta@mail.com"; | ||
|
||
INSERT INTO estudiantes2022(nombre,apellido,telefono,email) VALUES("Juan","Perez","2614545456","juan@mail.com"); | ||
-- Update (modificar- actualizar) | ||
UPDATE estudiantes2022 SET nombre="Juan Carlos" ,apellido="Garcia" WHERE idestudiantes2022=1; | ||
|
||
-- Delete | ||
DELETE FROM estudiantes2022 WHERE idestudiantes2022=4; | ||
-- Para modificar el idestudiantes2022 y comience en 1; | ||
ALTER TABLE estudiantes2022 AUTO_INCREMENT = 1; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>UTN</groupId> | ||
<artifactId>estudiante</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<dependencies> | ||
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> | ||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
<version>8.0.33</version> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
<properties> | ||
<maven.compiler.source>17</maven.compiler.source> | ||
<maven.compiler.target>17</maven.compiler.target> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
</project> |
13 changes: 13 additions & 0 deletions
13
Java/Semana 12/SistemaEstudiantes/src/main/java/UTN/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package UTN; | ||
|
||
import UTN.conexion.Conexion; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
var conexion = Conexion.getConnection(); | ||
if(conexion != null) | ||
System.out.println("Conexion Exitosa: "+conexion); | ||
else | ||
System.out.println("Error al conectarse"); | ||
}// Fin main | ||
}// Fin Clase |
25 changes: 25 additions & 0 deletions
25
Java/Semana 12/SistemaEstudiantes/src/main/java/UTN/conexion/Conexion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package UTN.conexion; | ||
|
||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.SQLException; | ||
|
||
public class Conexion { | ||
public static Connection getConnection(){ | ||
Connection conexion = null; | ||
// variables para conectarnos a la base de datos | ||
var baseDatos = "estudiantes"; | ||
var url = "jdbc:mysql://localhost:3306/"+baseDatos; | ||
var usuario = "root"; | ||
var password = "admin"; | ||
|
||
// cargamos la clase del driver de mysql en memoria | ||
try { | ||
Class.forName("com.mysql.cj.jdbc.Driver"); | ||
conexion = DriverManager.getConnection(url, usuario, password); | ||
} catch (ClassNotFoundException | SQLException e){ | ||
System.out.println("Ocurrio un error en la conexion: "+e.getMessage()); | ||
}// fin de catch | ||
return conexion; | ||
}// fin metodo Connection | ||
} |
159 changes: 159 additions & 0 deletions
159
Java/Semana 12/SistemaEstudiantes/src/main/java/UTN/datos/EstudianteDAO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
package UTN.datos; | ||
|
||
import UTN.dominio.Estudiante; | ||
import static UTN.conexion.Conexion.getConnection; | ||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class EstudianteDAO { | ||
// Método listar | ||
public List<Estudiante> listarEstudiantes() { | ||
List<Estudiante> estudiantes = new ArrayList<>(); | ||
// Creamos algunos objetos que son necesarios para comunicarnos con la base de datos | ||
PreparedStatement ps; // Envía la sentencia a la base de datos | ||
ResultSet rs; // Obtenemos el resultado de la base de datos | ||
// Creamos un objeto de tipo conexión | ||
Connection con = getConnection(); | ||
String sql = "SELECT * FROM estudiantes2022 ORDER BY idestudiantes2022"; | ||
try { | ||
ps = con.prepareStatement(sql); | ||
rs = ps.executeQuery(); | ||
while (rs.next()) { | ||
var estudiante = new Estudiante(); | ||
estudiante.setIdEstudiante(rs.getInt("idestudiantes2022")); | ||
estudiante.setNombre(rs.getString("nombre")); | ||
estudiante.setApellido(rs.getString("apellido")); | ||
estudiante.setTelefono(rs.getString("telefono")); | ||
estudiante.setEmail(rs.getString("email")); | ||
// Agregamos el estudiante a la lista | ||
estudiantes.add(estudiante); | ||
} // Fin while | ||
} catch (Exception e) { | ||
System.out.println("Ocurrió un error al seleccionar datos: " + e.getMessage()); | ||
} finally { | ||
try { | ||
con.close(); | ||
} catch (Exception e) { | ||
System.out.println("Ocurrió un error al cerrar la conexión: " + e.getMessage()); | ||
} | ||
return estudiantes; | ||
} // Fin finally | ||
} // Fin método listarEstudiantes | ||
|
||
// Método por ID | ||
public boolean buscarEstudiantePorId(Estudiante estudiante) { | ||
PreparedStatement ps; | ||
ResultSet rs; | ||
Connection con = getConnection(); | ||
String sql = "SELECT * FROM estudiantes2022 WHERE idestudiantes2022=?"; | ||
try { | ||
ps = con.prepareStatement(sql); | ||
ps.setInt(1, estudiante.getIdEstudiante()); | ||
rs = ps.executeQuery(); | ||
if (rs.next()) { | ||
estudiante.setNombre(rs.getString("nombre")); | ||
estudiante.setApellido(rs.getString("apellido")); | ||
estudiante.setTelefono(rs.getString("telefono")); | ||
estudiante.setEmail(rs.getString("email")); | ||
return true; // Se encontró un registro | ||
} | ||
} catch (Exception e) { | ||
System.out.println("Ocurrió un error al buscar estudiante: " + e.getMessage()); | ||
} finally { | ||
try { | ||
con.close(); | ||
} catch (Exception e) { | ||
System.out.println("Ocurrió un error al cerrar la conexión: " + e.getMessage()); | ||
} | ||
} | ||
return false; | ||
} // Fin método buscarEstudiantePorId | ||
|
||
// Método para agregar un nuevo estudiante | ||
public boolean agregarEstudiante(Estudiante estudiante) { | ||
PreparedStatement ps; | ||
Connection con = getConnection(); | ||
String sql = "INSERT INTO estudiantes2022(nombre, apellido, telefono, email) VALUES(?, ?, ?, ?)"; | ||
try { | ||
ps = con.prepareStatement(sql); | ||
ps.setString(1, estudiante.getNombre()); | ||
ps.setString(2, estudiante.getApellido()); | ||
ps.setString(3, estudiante.getTelefono()); | ||
ps.setString(4, estudiante.getEmail()); | ||
ps.execute(); | ||
return true; | ||
} catch (Exception e) { | ||
System.out.println("Ocurrió un error al agregar estudiante: " + e.getMessage()); | ||
} finally { | ||
try { | ||
con.close(); | ||
} catch (Exception e) { | ||
System.out.println("Error al cerrar la conexión: " + e.getMessage()); | ||
} | ||
} | ||
return false; | ||
} // Fin método agregarEstudiante | ||
|
||
// Método para modificar un estudiante | ||
public boolean modificarEstudiante(Estudiante estudiante) { | ||
PreparedStatement ps; | ||
Connection con = getConnection(); | ||
String sql = "UPDATE estudiantes2022 SET nombre=?, apellido=?, telefono=?, email=? WHERE idestudiantes2022=?"; | ||
try { | ||
ps = con.prepareStatement(sql); | ||
ps.setString(1, estudiante.getNombre()); | ||
ps.setString(2, estudiante.getApellido()); | ||
ps.setString(3, estudiante.getTelefono()); | ||
ps.setString(4, estudiante.getEmail()); | ||
ps.setInt(5, estudiante.getIdEstudiante()); | ||
ps.execute(); | ||
return true; | ||
} catch (Exception e) { | ||
System.out.println("Error al modificar estudiante: " + e.getMessage()); | ||
} finally { | ||
try { | ||
con.close(); | ||
} catch (Exception e) { | ||
System.out.println("Error al cerrar la conexión: " + e.getMessage()); | ||
} | ||
} | ||
return false; | ||
} // Fin método modificarEstudiante | ||
|
||
public static void main(String[] args) { | ||
var estudianteDao = new EstudianteDAO(); | ||
// Modificar estudiante | ||
var estudianteModificado = new Estudiante(1, "Juan Carlos", "Juarez", "55545665", "jjuarez@mail.com"); | ||
var modificado = estudianteDao.modificarEstudiante(estudianteModificado); | ||
if (modificado) | ||
System.out.println("Estudiante modificado: " + estudianteModificado); | ||
else | ||
System.out.println("No se modificó el estudiante: " + estudianteModificado); | ||
|
||
/*// Agregar estudiante | ||
var nuevoEstudiante = new Estudiante("Carlos", "Lara", "5555555", "carlosl@mail.com"); | ||
var agregado = estudianteDao.agregarEstudiante(nuevoEstudiante); | ||
if (agregado) | ||
System.out.println("Estudiante agregado: " + nuevoEstudiante); | ||
else | ||
System.out.println("No se ha agregado el estudiante: " + nuevoEstudiante);*/ | ||
|
||
// Listar los estudiantes | ||
System.out.println("Listado de estudiantes:"); | ||
List<Estudiante> estudiantes = estudianteDao.listarEstudiantes(); | ||
estudiantes.forEach(System.out::println); // Función lambda para imprimir | ||
|
||
/*// Buscar por ID | ||
var estudiante1 = new Estudiante(1); | ||
System.out.println("Estudiante antes de la búsqueda: " + estudiante1); | ||
System.out.println("Estudiantes antes de la búsqueda: " + estudiante1); | ||
var encontrado = estudianteDao.buscarEstudiantePorId(estudiante1); | ||
if (encontrado) | ||
System.out.println("Estudiante encontrado: " + estudiante1); | ||
else | ||
System.out.println("No se encontró el estudiante: " + estudiante1.getIdEstudiante());*/ | ||
} | ||
} |
Oops, something went wrong.