[RESOLVIDO] Erro ao capturar um int do mysql e jogar em label java

Alguém poderia me ajudar com esse código.

package reconhecimento;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.ParseException;

import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.text.MaskFormatter;

import connection.ConnectionFactory;

public class registrarUsuario extends JFrame {

	private JPanel contentPane;
	private JTextField lblId = new JTextField();
	private JTextField lblPNome;
	private JTextField lblUltNome;
	private JTextField lblProf;
	JFormattedTextField lblDatNasc = new JFormattedTextField(new MaskFormatter("##/##/####"));

	ConnectionFactory con = new ConnectionFactory();

	public registrarUsuario() throws ParseException {
		iniciarComponentes();
		showIdUser();
	}

	public void iniciarComponentes() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 275, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		lblId.setEditable(false);
		lblId.setFont(new Font("Arial", Font.BOLD, 15));
		lblId.setBounds(90, 11, 80, 25);
		contentPane.add(lblId);
		lblId.setColumns(10);

		JLabel lblNewLabel_1 = new JLabel("Primeiro Nome");
		lblNewLabel_1.setFont(new Font("Arial", Font.BOLD, 11));
		lblNewLabel_1.setBounds(10, 90, 105, 14);
		contentPane.add(lblNewLabel_1);

		JLabel lblUltimoNome = new JLabel("Ultimo Nome");
		lblUltimoNome.setFont(new Font("Arial", Font.BOLD, 11));
		lblUltimoNome.setBounds(145, 90, 105, 14);
		contentPane.add(lblUltimoNome);

		lblPNome = new JTextField();
		lblPNome.setBounds(10, 115, 105, 20);
		contentPane.add(lblPNome);
		lblPNome.setColumns(10);

		lblUltNome = new JTextField();
		lblUltNome.setBounds(145, 115, 105, 20);
		contentPane.add(lblUltNome);
		lblUltNome.setColumns(10);

		JLabel lblNewLabel_2 = new JLabel("Data de Nascimento");
		lblNewLabel_2.setFont(new Font("Arial", Font.BOLD, 11));
		lblNewLabel_2.setBounds(10, 160, 105, 14);
		contentPane.add(lblNewLabel_2);

		lblDatNasc.setBounds(10, 185, 105, 20);
		contentPane.add(lblDatNasc);

		lblProf = new JTextField();
		lblProf.setBounds(145, 183, 105, 20);
		contentPane.add(lblProf);
		lblProf.setColumns(10);

		JLabel lblNewLabel_3 = new JLabel("Profiss\u00E3o");
		lblNewLabel_3.setFont(new Font("Arial", Font.BOLD, 11));
		lblNewLabel_3.setBounds(145, 160, 105, 14);
		contentPane.add(lblNewLabel_3);

		JButton btnRegistrar = new JButton("Registrar");
		btnRegistrar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				int id = Integer.parseInt(lblId.getText().replace("ID: ", "" ));
				String PNome = lblPNome.getText();
				String UltNome = lblUltNome.getText();
				String Prof = lblProf.getText();
				String DatNasc = lblDatNasc.getText();

				new captura(id, PNome, UltNome, Prof, DatNasc).setVisible(true);
			}
		});
		btnRegistrar.setFont(new Font("Arial", Font.BOLD, 13));
		btnRegistrar.setBounds(90, 225, 90, 25);
		contentPane.add(btnRegistrar);
	}

	public static String SQL_SHOWID = "SELECT * FROM usuarioregistro ORDEM BY id DESC LIMIT 1";

	public int showIdUser() {

		int id = 0;
		Connection con = ConnectionFactory.getConnection();
		PreparedStatement stmt = null;
		ResultSet rs = null;
		
		try {
			stmt = con.prepareStatement(SQL_SHOWID);
			rs.first();
			lblId.setText(String.valueOf(rs.getInt("id")));
			id = Integer.parseInt(lblId.getText());
			id++;
			lblId.setText(String.valueOf(id));
		} catch (Exception ex) {
			
		}
		return id;
	}
}

Ele gera esse erro:

Vc tá tentando converter uma String vazia em número.

Linha 94 da classe registrarUsuario.


Só uma dúvida, vc tá fazendo reconhecimento facial né?

1 curtida

Sim,estou usando seus videos como referencia :smile:
Eu segui corretamento o vídeo,mas esta dando erro, esse o label ID do registrar,não esta puxando.

1 curtida

Como o colega @rodriguesabner já sinalizou, você está tentando fazer um parseInt de uma String vazia, isso está errado.
:man_shrugging:

1 curtida

Será q pode me ajudar,eu realmente não sei oq fazer…

Você vai ter que rever sua lógica, não faz sentido você querer converter uma String vazia para um int.