recortando bases de datos
This commit is contained in:
parent
9955ee422b
commit
707161c72b
@ -4,25 +4,14 @@ import java.sql.DriverManager;
|
|||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.sql.Statement;
|
import java.sql.Statement;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
public class Modelo {
|
public class Modelo {
|
||||||
private Connection connection;
|
private Connection connection;
|
||||||
private static final String crearDB = "create database if not exists gestionGastos;\n";
|
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" +
|
private static final String crearTranasccion = "create table if not exists transacciones (\n" +
|
||||||
" id int primary key,\n" +
|
" id int primary key auto_increment,\n" +
|
||||||
" id_gestion int,\n" +
|
" nombre varchar(30),\n"+
|
||||||
" dinero float,\n" +
|
" dinero float,\n" +
|
||||||
" fecha date\n" +
|
" fecha date\n" +
|
||||||
");";
|
");";
|
||||||
@ -31,10 +20,7 @@ public class Modelo {
|
|||||||
try {
|
try {
|
||||||
connection = DriverManager.getConnection("jdbc:mariadb://localhost:3306/gestionGastos?user=root&password=1234");
|
connection = DriverManager.getConnection("jdbc:mariadb://localhost:3306/gestionGastos?user=root&password=1234");
|
||||||
Statement crearDB = connection.createStatement();
|
Statement crearDB = connection.createStatement();
|
||||||
//crearDB.executeQuery(Modelo.scriptInicial);
|
|
||||||
crearDB.executeQuery(Modelo.crearDB);
|
crearDB.executeQuery(Modelo.crearDB);
|
||||||
crearDB.executeUpdate(Modelo.crearMes);
|
|
||||||
crearDB.executeUpdate(Modelo.crearGestion);
|
|
||||||
crearDB.executeUpdate(Modelo.crearTranasccion);
|
crearDB.executeUpdate(Modelo.crearTranasccion);
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
// TODO Auto-generated catch block
|
// 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user