recortando bases de datos

This commit is contained in:
Guillermo Roche 2019-12-17 22:52:02 +01:00
parent 9955ee422b
commit 707161c72b

View File

@ -4,25 +4,14 @@ import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.LocalDate;
public class Modelo {
private Connection connection;
private static final String crearDB = "create database if not exists gestionGastos;\n";
private static final String crearMes = "create table if not exists mes (\n" +
" id int primary key,\n" +
" anio int,\n" +
" mes int,\n" +
" total float\n" +
");\n";
private static final String crearGestion = "create table if not exists gestion (\n" +
" id int primary key,\n" +
" id_mes int,\n" +
" suma float,\n" +
" foreign key (id_mes) references mes(id)\n" +
");\n";
private static final String crearTranasccion = "create table if not exists transacciones (\n" +
" id int primary key,\n" +
" id_gestion int,\n" +
" id int primary key auto_increment,\n" +
" nombre varchar(30),\n"+
" dinero float,\n" +
" fecha date\n" +
");";
@ -31,10 +20,7 @@ public class Modelo {
try {
connection = DriverManager.getConnection("jdbc:mariadb://localhost:3306/gestionGastos?user=root&password=1234");
Statement crearDB = connection.createStatement();
//crearDB.executeQuery(Modelo.scriptInicial);
crearDB.executeQuery(Modelo.crearDB);
crearDB.executeUpdate(Modelo.crearMes);
crearDB.executeUpdate(Modelo.crearGestion);
crearDB.executeUpdate(Modelo.crearTranasccion);
} catch (SQLException e) {
// TODO Auto-generated catch block
@ -42,4 +28,19 @@ public class Modelo {
}
}
public void insertarTransaccion(String nombre, float dinero, LocalDate fecha) {
String query = "insert into transacciones(nombre, dinero, fecha) values(?, ?, ?);";
try {
PreparedStatement stmt = this.connection.prepareStatement(query);
stmt.setString(1, nombre);
stmt.setFloat(2, dinero);
stmt.setString(3, fecha.toString());
stmt.execute();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}