Preparedstatement Not Initialize(stm = >"stm" is not a known variable in the Current context.<)

Asked

Viewed 158 times

-1

Preparedstatement declared not initialize You have an error on line 83, stm declared on line 21 is not initialized.

import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;

public class Rascunho {

    // DAO
    private static String URL;
    private static Connection conn;
    private static PreparedStatement stm;
    private static ResultSet rs;
    private static String USER;
    private static String PASSWORD;

    public static void main(String[] args) {
        URL = "jdbc:firebirdsql://localhost:3050/c:\\Users\\Vagner\\Desktop\\Java2014\\DB\\CLINICA.FDB";
        USER = "SYSDBA";
        PASSWORD = "masterkey";

        WDB_Conecta();

        //****************************************************************
        if (Cliente_Existe(1) == 0) {
//Rotinas de salvamento
            JOptionPane.showMessageDialog(null, "Cliente NÂO Existe");
        } else {
//Rotinas de Atualização
            JOptionPane.showMessageDialog(null, "Cliente Existe");
        }
        //****************************************************************


        try {
            conn.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            StringWriter errors = new StringWriter();
            e.printStackTrace(new PrintWriter(errors));

            JOptionPane.showMessageDialog(null, errors.toString(), e.getMessage(), JOptionPane.ERROR_MESSAGE);
        }
    }

    private static void WDB_Conecta() {

        JOptionPane.showMessageDialog(null, "Iniciando Conecção");

        try {
            conn = DriverManager.getConnection(URL, USER, PASSWORD);
            JOptionPane.showMessageDialog(null, "Conectado com Sucesso");

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            StringWriter errors = new StringWriter();
            e.printStackTrace(new PrintWriter(errors));

            JOptionPane.showMessageDialog(null, errors.toString(), e.getMessage(), JOptionPane.ERROR_MESSAGE);
        }

    }



    private static int Cliente_Existe(int iCodigo) {
        int bol = 0;
        String SQL;

        SQL = "SELECT COUNT(*) AS TOTAL FROM TBCLIENTE WHERE TBCLIENTE.CODIGO = ?)";

        try {
            stm = conn.prepareStatement(SQL);
            stm.setInt(1, iCodigo);
            rs = stm.executeQuery();

            if (rs.getInt("TOTAL") > 0) {
                bol = 1;
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            StringWriter errors = new StringWriter();
            e.printStackTrace(new PrintWriter(errors));

            JOptionPane.showMessageDialog(null, errors.toString(), e.getMessage(), JOptionPane.ERROR_MESSAGE);
        }

        return bol;
    }


}//FIM
  • 2

    Which is line 83? Which error gives?

  • There is the possibility that the query is in error, not returning a valid statement.

  • My compiler accepts your code. For me, line 83 is stm.setInt(1, iCodigo); and there are 6 lines preceding the first import, correct? You’ve already been shown the remaining parentheses in your SQL, and without knowing more details about the structure of your database, you can’t get a much better answer than that. In addition, you are procedural programming with global variables, which is not a popular programming style, especially in java. Try to study a little more object-oriented programming. And your URL will only work on your PC, which is bad.

  • Thank you Victor, it was the same query, MUTLEY printed the Query for me.

1 answer

1


SQL = "SELECT COUNT(*) AS TOTAL FROM TBCLIENTE WHERE TBCLIENTE.CODIGO = ?)";

The ) really should be at the end of the query? I think this is the problem.

  • It was the same parentheses, thank you very much, had not noticed, problem solved.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.