Do not send the sql query from eclipse

0

I have this code that is the form of my window to enter a server to a database that is created from wamp .

The problem is that he does not send me the query, the connection seems to be fine

I leave the code here

package Ventanas;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import Clases.BaseDatos;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.Color;
import javax.swing.JTextPane;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;

public class ingresoMozo extends JFrame {

    private JPanel contentPane;
    private BaseDatos Palomos=null;
    private JTextField txtCed;
    private JTextField txtNom;
    private JTextField txtApe;

    private JTextField txtSueldo;
    private JTextField txtTel;
    private BaseDatos coco;

    /**
     * Launch the application.
     */

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ingresoMozo frame = new ingresoMozo();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public ingresoMozo() {
        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        setBounds(100, 100, 650, 650);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblCedula = new JLabel("C\u00E9dula");
        lblCedula.setBounds(23, 99, 46, 14);
        contentPane.add(lblCedula);

        JLabel lblNombre = new JLabel("Nombre");
        lblNombre.setBounds(23, 124, 46, 14);
        contentPane.add(lblNombre);

        JLabel lblApellido = new JLabel("Apellido");
        lblApellido.setBounds(23, 150, 46, 14);
        contentPane.add(lblApellido);

        JLabel lblTelfono = new JLabel("Tel\u00E9fono");
        lblTelfono.setBounds(23, 176, 46, 14);
        contentPane.add(lblTelfono);

        JLabel lblMozoAadidoCorrectamenteerror = new JLabel("Mozo a\u00F1adido correctamente/error");
        lblMozoAadidoCorrectamenteerror.setFont(new Font("Tahoma", Font.PLAIN, 14));
        lblMozoAadidoCorrectamenteerror.setBounds(23, 239, 219, 20);
        contentPane.add(lblMozoAadidoCorrectamenteerror);

        JLabel lblIngresarMozo = new JLabel("Datos de mozo");
        lblIngresarMozo.setFont(new Font("Tahoma", Font.BOLD, 14));
        lblIngresarMozo.setBounds(49, 52, 112, 20);
        contentPane.add(lblIngresarMozo);

        JLabel lblIngresoDeMozo = new JLabel("Ingreso de mozo");
        lblIngresoDeMozo.setFont(new Font("Tahoma", Font.BOLD, 16));
        lblIngresoDeMozo.setBounds(260, 11, 160, 28);
        contentPane.add(lblIngresoDeMozo);

        txtCed = new JTextField();
        txtCed.setBounds(78, 100, 129, 20);
        contentPane.add(txtCed);
        txtCed.setColumns(10);

        txtNom = new JTextField();
        txtNom.setBounds(78, 125, 129, 20);
        contentPane.add(txtNom);
        txtNom.setColumns(10);

        txtApe = new JTextField();
        txtApe.setBounds(78, 150, 129, 20);
        contentPane.add(txtApe);
        txtApe.setColumns(10);

        String cedula, telefono;
        String nombre, apellido;
        nombre=txtNom.getText().toString();
        apellido=txtApe.getText().toString();
        cedula=txtCed.getText().toString();
        Integer ced=Integer.parseInt(cedula);
        telefono=txtTel.getText().toString();
        Integer tel=Integer.parseInt(telefono);

        String consulta1 = "INSERT INTO 'personas'('nombre', 'apellido', 'telefono', 'ci') VALUES ('" + nombre + "', '" + apellido + "' ,'" + tel + "', '" + ced + "');";


        JButton btnAceptar = new JButton("Aceptar");
        btnAceptar.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {



                    coco.ejecutar(consulta1);




        }});


        btnAceptar.setBounds(104, 285, 89, 23);
        contentPane.add(btnAceptar);

        JButton btnLimpiarDatos = new JButton("Limpiar datos");
        btnLimpiarDatos.setBounds(260, 285, 112, 23);
        contentPane.add(btnLimpiarDatos);

        JLabel lblSueldo = new JLabel("Sueldo");
        lblSueldo.setBounds(23, 201, 46, 14);
        contentPane.add(lblSueldo);

        txtSueldo = new JTextField();
        txtSueldo.setBounds(78, 198, 129, 20);
        contentPane.add(txtSueldo);
        txtSueldo.setColumns(10);

        txtTel = new JTextField();
        txtTel.setBounds(75, 173, 86, 20);
        contentPane.add(txtTel);
        txtTel.setColumns(10);

    }
}

I leave my database class that I have the connection and the function to execute a query

public Connection getConexion() {
    return conexion;
}    

public void setConexion(Connection conexion) {
        this.conexion = conexion;
} 

public boolean ejecutar(String sql) {
    try {
        Statement sentencia = getConexion().createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
        sentencia.executeUpdate(sql);
        sentencia.close();
    } catch (SQLException e) {
        e.printStackTrace();
        return false;
    }     
    return true;
}

The error that appears on the screen is

  

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""       at java.lang.NumberFormatException.forInputString (Unknown Source)       at java.lang.Integer.parseInt (Unknown Source)       at java.lang.Integer.parseInt (Unknown Source)       at Ventanas.ingresoMozo. (incomeMozo.java:144)       at Windows. Index $ 1.mouseClicked (Index.java:53)       at java.awt.AWTEventMulticaster.mouseClicked (Unknown Source)       at java.awt.Component.processMouseEvent (Unknown Source)       at javax.swing.JComponent.processMouseEvent (Unknown Source)       at java.awt.Component.processEvent (Unknown Source)       at java.awt.Container.processEvent (Unknown Source)       at java.awt.Component.dispatchEventImpl (Unknown Source)       at java.awt.Container.dispatchEventImpl (Unknown Source)       at java.awt.Component.dispatchEvent (Unknown Source)       at java.awt.LightweightDispatcher.retargetMouseEvent (Unknown Source)       at java.awt.LightweightDispatcher.processMouseEvent (Unknown Source)       at java.awt.LightweightDispatcher.dispatchEvent (Unknown Source)       at java.awt.Container.dispatchEventImpl (Unknown Source)       at java.awt.Window.dispatchEventImpl (Unknown Source)       at java.awt.Component.dispatchEvent (Unknown Source)       at java.awt.EventQueue.dispatchEventImpl (Unknown Source)       at java.awt.EventQueue.access $ 500 (Unknown Source)       at java.awt.EventQueue $ 3.run (Unknown Source)       at java.awt.EventQueue $ 3.run (Unknown Source)       at java.security.AccessController.doPrivileged (Native Method)       at java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege (Unknown Source)       at java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege (Unknown Source)       at java.awt.EventQueue $ 4.run (Unknown Source)       at java.awt.EventQueue $ 4.run (Unknown Source)       at java.security.AccessController.doPrivileged (Native Method)       at java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege (Unknown Source)       at java.awt.EventQueue.dispatchEvent (Unknown Source)       at java.awt.EventDispatchThread.pumpOneEventForFilters (Unknown Source)       at java.awt.EventDispatchThread.pumpEventsForFilter (Unknown Source)       at java.awt.EventDispatchThread.pumpEventsForHierarchy (Unknown Source)       at java.awt.EventDispatchThread.pumpEvents (Unknown Source)       at java.awt.EventDispatchThread.pumpEvents (Unknown Source)       at java.awt.EventDispatchThread.run (Unknown Source)

    
asked by Facundo Lupacchino 26.10.2016 в 07:40
source

3 answers

0

Your error is from the Java code and not from the insert in your database ... the Logcat clearly says:

  

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString (Unknown Source) at java.lang.Integer.parseInt (Unknown Source) at java.lang.Integer.parseInt (Unknown Source) at Ventanas.ingresoMozo. (incomeMozo.java:144)

This means that somewhere in your code (specifically line 144) you are doing a% wrong% co

try{
   int numTelefono = Integer.parseInt(telefono);
}catch(NumberFormatException ex){ 
   System.out.println("Los datos que ingresaste a telefono son incorrectos...");
}

You have to verify well your data type for your fields, I see a Integer.parseInt and telefono , these fields are usually set as cedula (in BD) and / or varchar (In code) and that their values are not suitable for mathematical calculations, if not that they are to identify and differentiate from each other ....

    
answered by 26.10.2016 / 14:11
source
0

Try writing the query with single quotes instead of with an apostrophe:

String consulta1 = "INSERT INTO 'personas'('nombre', 'apellido', 'telefono', 'ci') VALUES ('" + nombre + "', '" + apellido + "' ,'" + tel + "', '" + ced + "');";
    
answered by 26.10.2016 в 08:51
0

According to the stack trace that you have, it seems that your error comes from here:

cedula=txtCed.getText().toString();
Integer ced=Integer.parseInt(cedula); // <-- aquí
telefono=txtTel.getText().toString();
Integer tel=Integer.parseInt(telefono); // <-- o aquí

You can identify more precisely which of the 2 is by looking more carefully at the stack trace and see what is line 144:

  

Ventanas.ingresoMozo. (incomeMozo.java:144) at

As the error message says:

  

java.lang.NumberFormatException: For input string: ""

... apparently the value of cedula or telefono is an empty string, so you can not convert it to an integer.

You will have to handle that special case to avoid the error.

By the way, since I see that you are concatenating the values directly in your SQL, I suggest you review the following information: What Is SQL injection and how can I avoid it?

    
answered by 26.10.2016 в 12:14