Aniadido loggin

This commit is contained in:
Guillermo Roche 2019-12-18 16:40:10 +01:00
parent 72e3f4fc28
commit 29d603bf94
5 changed files with 84 additions and 2 deletions

43
src/Configuracion.java Normal file
View File

@ -0,0 +1,43 @@
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
public class Configuracion {
private static String direccion=".config";
/**
* Guarda la configuracion del menu en un fichero oculto
*
* @param menu Menu del que se guardan los datos
*/
public static void guardarConfiguracion(String passwd) {
Properties configuracion = new Properties();
configuracion.setProperty("passwd", passwd);
try {
configuracion.store(new FileWriter(Configuracion.direccion), "Configuracion general");
} catch (IOException e1) {
e1.printStackTrace();
}
}
/**
* Carga la configuracion del menu
*
* @param menu menu en el que carga la configuracio
* @throws IOException
*/
public static String cargarConfiguracion() throws IOException {
Properties configuracion = new Properties();
//"jdbc:mariadb://localhost:3306/gestionGastos?user=root&password=1234"
String ret = "jdbc:mariadb://localhost:3306/gestionGastos?user=root&password=";
configuracion.load(new FileReader(Configuracion.direccion));
ret+=configuracion.getProperty("passwd");
return ret;
}
}

View File

@ -28,6 +28,8 @@ public class Controlador implements ActionListener{
this.vista.eliminar.setActionCommand("eliminar"); this.vista.eliminar.setActionCommand("eliminar");
this.vista.buscar.addActionListener(this); this.vista.buscar.addActionListener(this);
this.vista.buscar.setActionCommand("buscar"); this.vista.buscar.setActionCommand("buscar");
this.vista.cargarPasswd.addActionListener(this);
this.vista.cargarPasswd.setActionCommand("pass");
} }
@Override @Override
@ -88,6 +90,10 @@ public class Controlador implements ActionListener{
} }
break; break;
} }
case "pass":{
Configuracion.guardarConfiguracion(String.valueOf(this.vista.pass.getPassword()));
break;
}
} }
} }

View File

@ -12,7 +12,7 @@ public class Main {
Modelo modelo = new Modelo(); Modelo modelo = new Modelo();
Controlador controlador = new Controlador(vista, modelo); Controlador controlador = new Controlador(vista, modelo);
JFrame frame = new JFrame(); JFrame frame = new JFrame();
frame.setSize(200, 500); frame.setSize(500, 500);
frame.add(vista); frame.add(vista);
frame.setVisible(true); frame.setVisible(true);
} }

View File

@ -1,4 +1,5 @@
import java.io.IOException;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
@ -19,7 +20,19 @@ public class Modelo {
Modelo(){ Modelo(){
try { try {
connection = DriverManager.getConnection("jdbc:mariadb://localhost:3306/gestionGastos?user=root&password=1234"); String configuracion = null;
try {
configuracion = Configuracion.cargarConfiguracion();
}catch (Exception e) {
Configuracion.guardarConfiguracion("1234");
try {
configuracion = Configuracion.cargarConfiguracion();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
connection = DriverManager.getConnection(configuracion);
Statement crearDB = connection.createStatement(); Statement crearDB = connection.createStatement();
crearDB.executeQuery(Modelo.crearDB); crearDB.executeQuery(Modelo.crearDB);
crearDB.executeUpdate(Modelo.crearTranasccion); crearDB.executeUpdate(Modelo.crearTranasccion);

View File

@ -1,8 +1,10 @@
import java.awt.Dimension;
import java.awt.GridBagConstraints; import java.awt.GridBagConstraints;
import java.awt.GridBagLayout; import java.awt.GridBagLayout;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField; import javax.swing.JTextField;
public class Vista extends JPanel{ public class Vista extends JPanel{
@ -19,6 +21,9 @@ public class Vista extends JPanel{
JButton buscar; JButton buscar;
JTextField textoNombreViejo; JTextField textoNombreViejo;
JTextField insertarNombreViejo; JTextField insertarNombreViejo;
JTextField titulopPasswd;
JPasswordField pass;
JButton cargarPasswd;
public Vista() { public Vista() {
this.setLayout(new GridBagLayout()); this.setLayout(new GridBagLayout());
@ -82,5 +87,20 @@ public class Vista extends JPanel{
constrain.gridx = 1; constrain.gridx = 1;
constrain.gridy = 4; constrain.gridy = 4;
this.add(this.insertarNombreViejo, constrain); this.add(this.insertarNombreViejo, constrain);
this.titulopPasswd = new JTextField("Cambiar DB passwd");
this.titulopPasswd.setEditable(false);
constrain.gridx = 0;
constrain.gridy = 5;
this.add(this.titulopPasswd,constrain);
this.pass = new JPasswordField();
this.pass.setPreferredSize(new Dimension(80, 17));
constrain.gridx = 1;
constrain.gridy = 5;
this.add(this.pass,constrain);
this.cargarPasswd = new JButton("cambiar");
constrain.gridx = 2;
constrain.gridy = 5;
this.add(this.cargarPasswd,constrain);
} }
} }