Search code examples
javamysqlweb-servicessoapnetbeans-8

I can't make an Insert in MYSQL with a SOAP web service in Java using JDBC driver


Hi im trying to test a web service Soap java which is connected to mysql localhost (xampp) but i can't insert any row in the data base Here is my code

Conexion.java

import java.sql.Connection
import java.sql.DriverManager

public class Conexion {

public static final Connection ConectarMySQL() throws SQLException
{
    Connection conn;
    try
    {          
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/controldecalidad", "root", "");            
    }
    catch(SQLException ex)
    {
        throw new SQLException(ex.getMessage());
    }
    return conn;
}

}

and here is the webservice

 @WebMethod(operationName= "registra_rechazo")   
public void registra_rechazo(@WebParam(name = "SKU")String SKU,@WebParam(name = "fecha")String fecha,
        @WebParam(name = "num_captura")String num_captura,@WebParam(name = "pesoCaptura") int pesoCaptura) 
{
    try
    {
        Connection conn = Conexion.ConectarMySQL();
        String sql = "INSERT INTO registra_rechazo VALUES(?,?,?,?)";
        PreparedStatement pst = conn.prepareStatement(sql);
        pst.setString(1, SKU);   
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");            
        pst.setString(2, dateFormat.format(fecha));
        pst.setString(3, num_captura);
        pst.setInt(4, pesoCaptura);
        pst.execute();
        pst.close();
        conn.close();

    }
    catch(SQLException Ex)
    {

    } 
}

and here is the error https://i.sstatic.net/aWO16.png

UPDATE!! i checked my proyect and i got this new error https://i.sstatic.net/uHRQW.png

what im missing? please help!!!!


Solution

  • I FINALLY SOLVED IT !!

    in the class Conexion y remove the static final and i create a constructor for this class

    public class Conexion {
    
    public Conexion(){
    
    }    
    public Connection getConecction()
    {
        Connection con = null;
        try{
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/controldecalidad", "root", "");                        
        }catch(SQLException ex){            
        }
        catch(Exception ex){
        }
        return con;
        }    
    }
    

    then i created a class who have all the operations

    public class Operaciones 
    {
       Conexion conexion;
    
    public Operaciones()
    {
        conexion = new Conexion();
    }
    
    
    public boolean registra_rechazo(String SKU,String fecha,String num_captura,int pesoCaptura)
    {
        Boolean bandera=false;
         try
        {
            Connection conn = conexion.getConecction();
            java.util.Date utilStartDate = new java.util.Date(fecha);
            java.sql.Date sqlStartDate = new java.sql.Date(utilStartDate.getTime());           
            String sql = "INSERT INTO registra_rechazo VALUES(?,?,?,?)";
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1, SKU);   
            pst.setDate(2, sqlStartDate);
            pst.setString(3, num_captura);
            pst.setInt(4, pesoCaptura);
            if(pst.executeUpdate()!=-1){
                bandera = true;
                return bandera;
            }
            else{
                bandera = false;
                return bandera;
    
            }
    
        }
        catch(SQLException Ex)
        {
    
        }
         return bandera;
       }
    }
    

    then i call the method in the web service, but first the web method must be created by add operation in the web service like the image

    https://i.sstatic.net/HjAHk.png

    i dont know why but the method only works if you created the method with that wizard

    @WebMethod(operationName = "operation")
    public boolean operation(@WebParam(name = "SKU") String SKU, @WebParam(name = "fecha") String fecha, @WebParam(name = "num_captura") String num_captura, @WebParam(name = "peso_captura") int peso_captura) {
        //TODO write your implementation code here:
         Operaciones op = new Operaciones();
        return op.registra_rechazo(SKU,fecha,num_captura,peso_captura);
    }
    

    and then the web method must return something, with void will not work