簡體   English   中英

我無法使用JDBC驅動程序通過Java中的SOAP Web服務在MYSQL中插入

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

嗨,我正在嘗試測試連接到mysql localhost(xampp)的Web服務Soap java,但是我無法在數據庫中插入任何行,這是我的代碼

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

}

這是網絡服務

 @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

更新! 我檢查了我的proyect,並收到了這個新錯誤https://i.stack.imgur.com/uHRQW.png

我缺少什么? 請幫忙!!!!

我終於解決了!

在Conexion類中,刪除靜態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服務(如圖像)中的添加操作來創建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無法工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM