I'm doing a java project with forms which I have to save and delete for this I think a class that is the class connection a different class for each of the forms in which the different methods of saving modify and eliminate and the forms.
but the save function does not work for me and I do not know why
Save Class
package clasesAdministrador;
import Conexion.conexion1;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.table.DefaultTableModel;
import formAdministrador.usuarios1ad;
import javax.swing.JOptionPane;
public class usuario1ad {
Conexion.conexion1 conn=new Conexion.conexion1();
public PreparedStatement sql;
public ResultSet datos, rs;
DefaultTableModel tabla = new DefaultTableModel();
Statement sent;
public void Guardar(int num_usu, String nom_usu, String con1,String tipus)
{
try
{
conn.Conectar();
sql=conn.con.prepareStatement("insert into usuario (id_usu,nom_usu,con_usu,tip_usu) values ("+num_usu+",'"+nom_usu+"','"+con1+"','"+tipus+"')");
sql.execute();
conn.cerrar();
}
catch(SQLException ex)
{
System.out.println("ERROR AL GUARDAR...");
}
} // FIN METODO GUARDAR
} // FIN DE TODO
Conexion Class
package Conexion;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class conexion1{
public PreparedStatement sql;
public ResultSet datos;
public Connection con=null;
public void Conectar(){
String db="jdbc:mysql://localhost:3306/sena";
String usuario="root";
String password="salomeaa";
try
{
String controlador="com.mysql.jdbc.Driver";
System.out.println("CONEXION CON BASE DE DATOS...." +db);
Class.forName(controlador);
con=DriverManager.getConnection(db,usuario,password);
System.out.println("CONEXION EXITOSA");
}
catch(Exception ex)
{
System.out.println("Error "+ex.getMessage());
}
}
public void cerrar(){
if(con==null)
{
try
{ con.close();}
catch(Exception ex)
{ }
}
}
}
Java Form
public class usuarios1ad extends javax.swing.JFrame {
usuario1ad gg = new usuario1ad();
// too much code to put everything (buttons and others)
public void GuardarUsuario(){
this.tipUsu();
gg.Guardar(Integer.parseInt(num_usu.getText()),nom_usu.getText(),con1.getText(),tipus);
// JOptionPane.showMessageDialog(null, "DATOS ALMACENADOS CORRECTAMENTE", "GUARDAR", JOptionPane.INFORMATION_MESSAGE);
}
num_usu: user id
nom_usu: username
con1: user's password
tipus: user type (roles for permissions within the program)
DATABASE (The table)
This is what I get when trying to save information
This is the form in which I keep the information
the combo I keep it like that
public void tipUsu(){
if(tip_usu1.getSelectedItem()=="...")
{
JOptionPane.showMessageDialog(null, "Ingrese el tipo de usuario");
}
else if(tip_usu1.getSelectedItem()=="usuario")
{
tipus="usuario";
}
else if(tip_usu1.getSelectedItem()=="administrador")
{
tipus="administrador";
}
then the combo is called "tip_usu1" but it's only to know what kind of user it is because what I send to the database is "tipus"
and for the passwords to validate them two
private boolean isPasswordCorrect(char[] con1,char[] con2){
boolean valor = true;
int puntero = 0;
if (con1.length != con2.length){
valor = false;
}
else{
while((valor)&&(puntero < con1.length)){
if (con1[puntero] != con2[puntero]){
valor = false;
}
puntero++;
}
}
return valor;
}
the two fields of password is only to confirm that it is not wrong to enter the one that I send to the database in "con1" "con2" is only to compare it and see that it's the same