我无法使用 JDBC 驱动程序在 Java 中使用 SOAP Web 服务在 MYSQL 中插入
I can't make an Insert in MYSQL with a SOAP web service in Java using JDBC driver
您好,我正在尝试测试连接到 mysql 本地主机 (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
更新!! 我检查了我的项目,我得到了这个新错误
https://i.stack.imgur.com/uHRQW.png
我缺少什么?请帮助!!!!
我终于解决了!!
在 class Conexion y 中删除静态 final,我为此创建一个构造函数 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;
}
}
然后我创建了一个 class 拥有所有操作
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 方法必须 return 东西,用 void 将不起作用
您好,我正在尝试测试连接到 mysql 本地主机 (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
更新!! 我检查了我的项目,我得到了这个新错误 https://i.stack.imgur.com/uHRQW.png
我缺少什么?请帮助!!!!
我终于解决了!!
在 class Conexion y 中删除静态 final,我为此创建一个构造函数 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;
}
}
然后我创建了一个 class 拥有所有操作
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 方法必须 return 东西,用 void 将不起作用