primer commit
This commit is contained in:
+32
@@ -0,0 +1,32 @@
|
||||
# --- MAVEN ---
|
||||
target/
|
||||
pom.xml.tag
|
||||
pom.xml.releaseBackup
|
||||
pom.xml.versionsBackup
|
||||
pom.xml.next
|
||||
release.properties
|
||||
dependency-reduced-pom.xml
|
||||
buildNumber.properties
|
||||
.mvn/timing.properties
|
||||
.mvn/wrapper/maven-wrapper.properties
|
||||
|
||||
# --- ECLIPSE ---
|
||||
.metadata/
|
||||
bin/
|
||||
tmp/
|
||||
*.tmp
|
||||
*.bak
|
||||
*.swp
|
||||
.settings/
|
||||
.loadpath
|
||||
.project
|
||||
.classpath
|
||||
|
||||
# --- ARCHIVOS DEL SISTEMA OPERATIVO ---
|
||||
.DS_Store
|
||||
.DS_Store?
|
||||
._*
|
||||
.Spotlight-V100
|
||||
.Trashes
|
||||
ehthumbs.db
|
||||
Thumbs.db
|
||||
@@ -0,0 +1,30 @@
|
||||
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>conectores01</groupId>
|
||||
<artifactId>conectores01</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<dependencies>
|
||||
<!-- Source:
|
||||
https://mvnrepository.com/artifact/com.mysql/mysql-connector-j -->
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
<version>26.10.0</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<sourceDirectory>src</sourceDirectory>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.13.0</version>
|
||||
<configuration>
|
||||
<release>26</release>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,97 @@
|
||||
package conectores01;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
public class MainConcesionario {
|
||||
|
||||
// Parámetros de conexión directos (sin ficheros de configuración)
|
||||
private static final String URL = "jdbc:mysql://10.100.11.137:4306/concesionario?useSSL=false&serverTimezone=UTC";
|
||||
private static final String USER = "root";
|
||||
private static final String PASSWORD = "a123";
|
||||
|
||||
public static void main(String[] args) {
|
||||
try (Connection con = DriverManager.getConnection(URL, USER, PASSWORD)) {
|
||||
System.out.println("--- 1. CONSULTAR LA LISTA DE COCHES ---");
|
||||
consultarCoches(con);
|
||||
|
||||
System.out.println("\n--- 2. ACTUALIZAR EL COCHE CON CÓDIGO 5 (Ibiza Eco) ---");
|
||||
actualizarCoche(con, 5, "Ibiza Eco");
|
||||
consultarCoches(con); // Ver el cambio
|
||||
|
||||
System.out.println("\n--- 3. BORRAR REGISTROS ---");
|
||||
borrarCoche(con, 7); // Ejemplo borrando el coche 7
|
||||
consultarCoches(con);
|
||||
|
||||
System.out.println("\n--- 4. INSERTAR UN COCHE NUEVO ---");
|
||||
insertarCoche(con, 8, "Seat", "Arona", 1.0);
|
||||
consultarCoches(con);
|
||||
|
||||
} catch (SQLException e) {
|
||||
System.out.println("Error de base de datos:");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// Método para consultar e imprimir la lista de coches (Read)
|
||||
public static void consultarCoches(Connection con) {
|
||||
String sql = "SELECT identificador, marca, modelo, cilindrada FROM T_COCHE";
|
||||
try (Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(sql)) {
|
||||
|
||||
while (rs.next()) {
|
||||
int id = rs.getInt("identificador");
|
||||
String marca = rs.getString("marca");
|
||||
String modelo = rs.getString("modelo");
|
||||
double cilindrada = rs.getDouble("cilindrada");
|
||||
|
||||
System.out.println("ID: " + id + " | " + marca + " " + modelo + " | Cilindrada: " + cilindrada);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// Método para actualizar el modelo del coche con código 5 (Update)
|
||||
public static void actualizarCoche(Connection con, int id, String nuevoModelo) {
|
||||
String sql = "UPDATE T_COCHE SET modelo = ? WHERE identificador = ?";
|
||||
try (PreparedStatement pstmt = con.prepareStatement(sql)) {
|
||||
pstmt.setString(1, nuevoModelo);
|
||||
pstmt.setInt(2, id);
|
||||
int filas = pstmt.executeUpdate();
|
||||
System.out.println("Coche actualizado con éxito. Filas afectadas: " + filas);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// Método para borrar un coche por su identificador (Delete)
|
||||
public static void borrarCoche(Connection con, int id) {
|
||||
String sql = "DELETE FROM T_COCHE WHERE identificador = ?";
|
||||
try (PreparedStatement pstmt = con.prepareStatement(sql)) {
|
||||
pstmt.setInt(1, id);
|
||||
int filas = pstmt.executeUpdate();
|
||||
System.out.println("Coche con ID " + id + " borrado. Filas afectadas: " + filas);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// Método para insertar un coche (Create)
|
||||
public static void insertarCoche(Connection con, int id, String marca, String modelo, double cilindrada) {
|
||||
String sql = "INSERT INTO T_COCHE (identificador, marca, modelo, cilindrada) VALUES (?, ?, ?, ?)";
|
||||
try (PreparedStatement pstmt = con.prepareStatement(sql)) {
|
||||
pstmt.setInt(1, id);
|
||||
pstmt.setString(2, marca);
|
||||
pstmt.setString(3, modelo);
|
||||
pstmt.setDouble(4, cilindrada);
|
||||
pstmt.executeUpdate();
|
||||
System.out.println("Coche insertado correctamente.");
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user