Web services c # + java

1

I am trying to use a web service created in c # from visual java. I'm trying to use them, but I get the following error:

  

incompatible when trying to convert from int to string type

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
       int matricula=Integer.parseInt(jTextField1.getText());
       jTextField2.setText(Integer.toString(consultaAlumno(matricula)));

    } 
[WebMethod]
    public string RegistrarAlumno(int matricula, String nombre, String apellidos, String edad, String carrera, String instituto)
    {
        SqlConnection cnn;
        String connetionString = "Data source=LPG\MGPEXPRESS; Initial Catalog=escuela;Integrated Security=True";
        cnn = new SqlConnection(connetionString);


        try
        {
            cnn.Open();
        }
        catch
        {
            return ("Error al abrir conexion");
        }

        SqlCommand command = new SqlCommand("SELECT * FROM alumno " + "WHERE matricula=" + matricula, cnn);
        SqlDataReader reader = command.ExecuteReader();

        String Query;
        if (reader.HasRows)
        {
            return ("El alumno ya esta registrado");
        }
        else
        {
            Query = "Insert Into alumno(matricula, nombre, apellidos, edad, carrera, instituto)" + "values(" + matricula + ",'" + nombre + "','" + apellidos + "','" + edad + "','" + carrera + "','" + instituto + "')";
        }
        reader.Close();

        try
        {
            SqlCommand mycommand = new SqlCommand(Query, cnn);
            mycommand.ExecuteNonQuery();
            return ("Alumno registrado");
        }
        catch (Exception ex)
        {
            return ("No se pudo realizar consulta" + ex.Message);
        }
    }

    [WebMethod]
    public string ConsultaAlumno(int matricula)
    {
        SqlConnection cnn;
        String connetionString = "Data source=LPG\MGPEXPRESS; Initial Catalog=escuela;Integrated Security=True";
        cnn = new SqlConnection(connetionString);
        try
        {
            cnn.Open();
            SqlCommand command = new SqlCommand("Select * from alumno " + "where matricula=" + matricula, cnn);
            SqlDataReader reader = command.ExecuteReader();

            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    return (reader["nombre"].ToString() + "" +
                            reader["apellidos"].ToString() + "" +
                            reader["edad"].ToString() + "" +
                            reader["carrera"].ToString() + "" +
                            reader["instituto"].ToString());
                }
            }

            else
            {
                return ("No se encontre el alumno");
            }
            reader.Close();
            cnn.Close();
        }
        catch (Exception ex)
        {
            return ("No se pudo realizar consulta");
        }
        return matricula + "Hola";
    }


}
    
asked by senseilex 29.11.2017 в 05:26
source

1 answer

1

Good senseilex,

You are doing Integer.toString() when you really do not need it, since the consultaAlumno(matricula) function returns a String .

The code should look like this:

jTextField2.setText(consultaAlumno(matricula));

Be careful because in the function ConsultaAlumno you directly do return , without closing conexion a la BD (cnn) or DataReader (reader) and that can bring you problems later. You should assign the value that you read to a variable and once you have closed everything put the return , for example:

[WebMethod]
public string ConsultaAlumno(int matricula)
{
    String resultado = "";
    SqlConnection cnn;
    String connetionString = "Data source=LPG\MGPEXPRESS; Initial Catalog=escuela;Integrated Security=True";
    cnn = new SqlConnection(connetionString);
    try
    {
        cnn.Open();
        SqlCommand command = new SqlCommand("Select * from alumno " + "where matricula=" + matricula, cnn);
        SqlDataReader reader = command.ExecuteReader();

        if (reader.HasRows)
        {
            while (reader.Read())
            {
                resultado = (reader["nombre"].ToString() + "" +
                        reader["apellidos"].ToString() + "" +
                        reader["edad"].ToString() + "" +
                        reader["carrera"].ToString() + "" +
                        reader["instituto"].ToString());
            }
        }

        else
        {
            return ("No se encontre el alumno");
        }
        reader.Close();
        cnn.Close();
    }
    catch (Exception ex)
    {
        return ("No se pudo realizar consulta");
    }
    return resultado;
}
    
answered by 29.11.2017 / 10:45
source