我无法使用jdbc驱动程序在mysql中使用soapweb服务进行插入

x8diyxa7  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(341)

嗨,我正在尝试测试一个连接到mysql localhost(xampp)的web服务soapjava,但是我不能在数据库中插入任何行,这是我的代码
连接.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;
}

}
这是网络服务

@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)
    {

    } 
}

这里是错误https://i.stack.imgur.com/awo16.png
更新!!我检查了我的程序,发现了一个新的错误https://i.stack.imgur.com/uhrqw.png
我错过了什么?请帮忙!!!!

ffvjumwh

ffvjumwh1#

我终于解决了!!
在类conexion y中删除静态final,然后我为这个类创建一个构造函数

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;
    }    
}

然后我创建了一个拥有所有操作的类

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;
   }
}

然后我在web服务中调用这个方法,但是首先必须在web服务中通过add操作创建这个web方法,如图所示
https://i.stack.imgur.com/hjahk.png
我不知道为什么,但是这个方法只有在你用那个向导创建了这个方法的情况下才有效

@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);
}

然后web方法必须返回一些内容,而void将不起作用

相关问题