Java - Error al insertar datos ala base de datos desde netbeans

 
Vista:

Error al insertar datos ala base de datos desde netbeans

Publicado por Carlos (4 intervenciones) el 07/07/2020 19:59:37
Me sale este error al momento de guardar en la base de datos los datos de un usuario...espero su ayuda pronto...
jul 07, 2020 12:55:32 PM Formularios.frmPrincipal btnGuardarActionPerformed
GRAVE: null
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'td,documento,nombre,apellido,telefono,fecha_ing,hora_ing,motivo VALUES ('TI',...' at line 1

---------------------------------------------------------------------------------------------------------------------------------------------------------
Este es el codigo dentro del boton guardar:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
ConexionBD Con = new ConexionBD();
Con.ConectarBD();
String td = cbTD.getSelectedItem().toString();
String doc = txtDocumento.getText();
String nom = txtNombre.getText();
String ape = txtApellido.getText();
String tel = txtTelefono.getText();
String dir = txtDireccion.getText();
String fi = txtFechaI.getText();
String hi = txtHoraI.getText();
String mot = txtMotivo.getText();
if (!td.isEmpty()) {
    if (!doc.isEmpty()) {
        if (!nom.isEmpty()) {
            if (!ape.isEmpty()) {
                if (!tel.isEmpty()) {
                    if (!dir.isEmpty()) {
                       if (!fi.isEmpty()) {
                           if (!hi.isEmpty()) {
                               if (!mot.isEmpty()) {
                                   try {
                                       String SQL = "INSERT INTO personas "
                                               + "td,documento,nombre,apellido,telefono,fecha_ing,hora_ing,motivo"
                                               + " VALUES "
                                               + "('"+td+"','"+doc+"','"+nom+"','"+ape+"','"+tel+"','"+dir+"','"+fi+"','"+hi+"','"+mot+"',);";
                                       Con.sentencia.execute(SQL);
                                       JOptionPane.showMessageDialog(null, "¡Los datos fueron guardados con exito!");
                                   } catch (SQLException ex) {
                                       Logger.getLogger(frmPrincipal.class.getName()).log(Level.SEVERE, null, ex);
                                   }
                               } else {
                                   JOptionPane.showMessageDialog(null, "Por favor digite el motivo de ingreso");
                               }
                            } else {
                                JOptionPane.showMessageDialog(null, "Por favor digite la hora de ingreso");
                            }
                        }else {
                           JOptionPane.showMessageDialog(null, "Por favor digite la fecha de ingreso");
                        }
                    } else {
                        JOptionPane.showMessageDialog(null, "Por favor ingrese una direccion");
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "Por favor ingrese su numero de telefonico");
                }
            } else {
                JOptionPane.showMessageDialog(null, "Por favor ingrese su apellido");
            }
        } else {
            JOptionPane.showMessageDialog(null, "Por favor ingrese su nombre");
        }
    } else {
        JOptionPane.showMessageDialog(null, "Por favor ingrese su documento ");
    }
} else {
    JOptionPane.showMessageDialog(null, "Por favor escoja una opcion TI, CC, RC, CE");
}


-------------------------------------------------------------------------------------------------------------------------------------------------------
Este es el codigo de la clase ConexioBD:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class ConexionBD {
 
    public Connection conexion;
    public Statement sentencia;
    public ResultSet resultado;
 
    public void ConectarBD() {
        try {
            final String Controlador = "com.mysql.jdbc.Driver";
            Class.forName(Controlador);
            final String url_bd = "jdbc:mysql://localhost:3306/registro2";
            conexion = DriverManager.getConnection(url_bd, "root", "root");
            sentencia = conexion.createStatement();
        } catch (ClassNotFoundException | SQLException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
 
    public void DesconectarBD() {
        try {
            if (conexion != null) {
                if (sentencia != null){
                    sentencia.close();
                }
                conexion.close();
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
            System.exit(1);
        }
    }
 
    public Connection getConnection() {
        return conexion;
    }
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Billy Joel
Val: 2.665
Oro
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

Error al insertar datos ala base de datos desde netbeans

Publicado por Billy Joel (876 intervenciones) el 07/07/2020 21:58:17
Tienes un error en la sentencia sql. También estas intentando insertar la dirección, pero no está declarado en la sentencia.
Lo que hice fue quitar el parámetro (variable) dir.
También cerré la conexión al final de la sentencia.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
ConexionBD Con = new ConexionBD();
Con.ConectarBD();
String td = cbTD.getSelectedItem().toString();
String doc = txtDocumento.getText();
String nom = txtNombre.getText();
String ape = txtApellido.getText();
String tel = txtTelefono.getText();
String dir = txtDireccion.getText();
String fi = txtFechaI.getText();
String hi = txtHoraI.getText();
String mot = txtMotivo.getText();
if (!td.isEmpty()) {
    if (!doc.isEmpty()) {
        if (!nom.isEmpty()) {
            if (!ape.isEmpty()) {
                if (!tel.isEmpty()) {
                    if (!dir.isEmpty()) {
                        if (!fi.isEmpty()) {
                            if (!hi.isEmpty()) {
                                if (!mot.isEmpty()) {
                                    try {
                                        String SQL = "INSERT INTO personas "
                                                + "(td, documento, nombre, apellido, telefono, fecha_ing, hora_ing, motivo)"
                                                + " VALUES "
                                                //+ "('" + td + "', '" + doc + "', '" + nom + "', '" + ape + "', '" + tel + "', '" + dir + "', '" + fi + "', '" + hi + "', '" + mot + "')";
                                                + "('" + td + "', '" + doc + "', '" + nom + "', '" + ape + "', '" + tel + "', '" + fi + "', '" + hi + "', '" + mot + "')";
                                        Con.sentencia.execute(SQL);
                                        JOptionPane.showMessageDialog(null, "¡Los datos fueron guardados con exito!");
                                    } catch (SQLException ex) {
                                        Logger.getLogger(frmPrincipal.class.getName()).log(Level.SEVERE, null, ex);
                                    } finally {
                                        try{
                                            if(Con.getConnection() != null && !Con.getConnection().isClosed()){
                                                Con.getConnection().close();
                                            }
                                        }catch(SQLException ex){
                                            ex.printStackTrace(System.out);
                                        }
                                    }
                                } else {
                                    JOptionPane.showMessageDialog(null, "Por favor digite el motivo de ingreso");
                                }
                            } else {
                                JOptionPane.showMessageDialog(null, "Por favor digite la hora de ingreso");
                            }
                        } else {
                            JOptionPane.showMessageDialog(null, "Por favor digite la fecha de ingreso");
                        }
                    } else {
                        JOptionPane.showMessageDialog(null, "Por favor ingrese una direccion");
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "Por favor ingrese su numero de telefonico");
                }
            } else {
                JOptionPane.showMessageDialog(null, "Por favor ingrese su apellido");
            }
        } else {
            JOptionPane.showMessageDialog(null, "Por favor ingrese su nombre");
        }
    } else {
        JOptionPane.showMessageDialog(null, "Por favor ingrese su documento ");
    }
} else {
    JOptionPane.showMessageDialog(null, "Por favor escoja una opcion TI, CC, RC, CE");
}

PD: Creo que necesitas añadir la dirección a la sentencia.

Saludos,
Billy Joel
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
2
Comentar

Error al insertar datos ala base de datos desde netbeans

Publicado por Carlos (4 intervenciones) el 08/07/2020 02:03:17
Gracias, Billly joel me sirvio de gran ayuda!..
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar