99 lines
4.4 KiB
Plaintext
99 lines
4.4 KiB
Plaintext
import java.sql.Connection;
|
|
import java.sql.DriverManager;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.ResultSet;
|
|
import java.sql.Statement;
|
|
import java.io.File;
|
|
|
|
public class Main {
|
|
public static void main(String[] args) {
|
|
// Mensaje de bienvenida
|
|
System.out.printf("Hello and welcome!\n");
|
|
|
|
// Crear la base de datos DAM, tablas, insertar registros y realizar consulta
|
|
String dbName = "dam.db";
|
|
String url = "jdbc:sqlite:" + dbName; // Ruta de la base de datos
|
|
|
|
try (Connection conn = DriverManager.getConnection(url)) {
|
|
if (conn != null) {
|
|
System.out.println("Conexión establecida con la base de datos DAM");
|
|
|
|
// Mostrar la ruta completa de la base de datos
|
|
File dbFile = new File(dbName);
|
|
System.out.println("Ruta de la base de datos: " + dbFile.getAbsolutePath());
|
|
|
|
// Crear las tablas si no existen
|
|
String createProfesoresTable = "CREATE TABLE IF NOT EXISTS profesores ("
|
|
+ "id INTEGER PRIMARY KEY AUTOINCREMENT, "
|
|
+ "nombre TEXT NOT NULL, "
|
|
+ "asignatura TEXT NOT NULL);";
|
|
|
|
String createAlumnosTable = "CREATE TABLE IF NOT EXISTS alumnos ("
|
|
+ "id INTEGER PRIMARY KEY AUTOINCREMENT, "
|
|
+ "nombre TEXT NOT NULL, "
|
|
+ "edad INTEGER NOT NULL, "
|
|
+ "id_profesor INTEGER, "
|
|
+ "FOREIGN KEY (id_profesor) REFERENCES profesores(id));";
|
|
|
|
try (Statement stmt = conn.createStatement()) {
|
|
stmt.execute(createProfesoresTable);
|
|
stmt.execute(createAlumnosTable);
|
|
System.out.println("Tablas creadas exitosamente.");
|
|
}
|
|
|
|
// Insertar registros en la tabla profesores
|
|
String insertProfesorSQL = "INSERT INTO profesores (nombre, asignatura) VALUES (?, ?)";
|
|
try (PreparedStatement pstmt = conn.prepareStatement(insertProfesorSQL)) {
|
|
pstmt.setString(1, "Pedro García");
|
|
pstmt.setString(2, "Matemáticas");
|
|
pstmt.executeUpdate();
|
|
|
|
pstmt.setString(1, "Laura Martínez");
|
|
pstmt.setString(2, "Física");
|
|
pstmt.executeUpdate();
|
|
|
|
pstmt.setString(1, "José Rodríguez");
|
|
pstmt.setString(2, "Química");
|
|
pstmt.executeUpdate();
|
|
}
|
|
|
|
// Insertar registros en la tabla alumnos
|
|
String insertAlumnoSQL = "INSERT INTO alumnos (nombre, edad, id_profesor) VALUES (?, ?, ?)";
|
|
try (PreparedStatement pstmt = conn.prepareStatement(insertAlumnoSQL)) {
|
|
pstmt.setString(1, "Carlos Pérez");
|
|
pstmt.setInt(2, 20);
|
|
pstmt.setInt(3, 1); // Asignar el profesor con id 1 (Pedro García)
|
|
pstmt.executeUpdate();
|
|
|
|
pstmt.setString(1, "Ana López");
|
|
pstmt.setInt(2, 22);
|
|
pstmt.setInt(3, 2); // Asignar el profesor con id 2 (Laura Martínez)
|
|
pstmt.executeUpdate();
|
|
|
|
pstmt.setString(1, "Luis Fernández");
|
|
pstmt.setInt(2, 21);
|
|
pstmt.setInt(3, 3); // Asignar el profesor con id 3 (José Rodríguez)
|
|
pstmt.executeUpdate();
|
|
}
|
|
|
|
// Realizar consulta para obtener todos los alumnos con sus profesores
|
|
String query = "SELECT a.nombre AS alumno, p.nombre AS profesor "
|
|
+ "FROM alumnos a "
|
|
+ "JOIN profesores p ON a.id_profesor = p.id";
|
|
try (PreparedStatement pstmt = conn.prepareStatement(query);
|
|
ResultSet rs = pstmt.executeQuery()) {
|
|
System.out.println("Alumnos y sus profesores:");
|
|
while (rs.next()) {
|
|
String nombreAlumno = rs.getString("alumno");
|
|
String nombreProfesor = rs.getString("profesor");
|
|
System.out.println("Alumno: " + nombreAlumno + " | Profesor: " + nombreProfesor);
|
|
}
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|