简体   繁体   English

WebService(JAX-WS)中的创建方法以将数据发送到mysql DB

[英]Create method in WebService (JAX-WS) to send data to mysql DB

Hi everybody.. 大家好..

I'm new to web services so tried to create simple web service (jax-ws) on glass fish server 4.1.1, this service sending data to mysql database like this but I'm getting this error why I don't know!?!? 我是新来的网络服务,从而试图玻璃鱼服务器4.1.1创建简单的Web服务(JAX-WS),该服务发送数据等MySQL数据库这个 ,但我发现这个错误,为什么我不知道! ?!?

MY CODES: 我的代码:

    package com.me.coder;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import javax.jws.WebService;
    import javax.jws.WebMethod;
    import javax.jws.WebParam;

    /**
     *
     * @author MacbookPro
     */
    @WebService(serviceName = "GetWriteToDB")
    public class GetWriteToDB {

        /**
         * Web service operation
         */

        @WebMethod(operationName = "sendToDb")
        public void sendToDb(@WebParam(name = "id") int id, @WebParam(name = "name") String name, @WebParam(name = "location") double location, @WebParam(name = "date") String date) {
           String host = "jdbc:mysql://localhost/onurDB";
            String user = "onur";
            String pass = "onurdb958";

            try(Connection conn = DriverManager.getConnection(host,user,pass);
                Statement smt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);   
               ){
                String SQL = "INSERT INTO `GoogleLoc`(`ID`, `NAME`, `LOCATION`, `DATE`) VALUES ('" + id + "','" + name + "','"+ location + "','" + date + "')";
                smt.executeUpdate(SQL);

            }catch(SQLException ex){
                System.out.println("SQLException : " + ex);
            }  

        }
    }

For control,same codes tried using in java SE class worked perfectly. 为了进行控制,在Java SE类中尝试使用的相同代码非常有效。 Like this: 像这样:

package send;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author MacbookPro
 */
public class Send {

    /**
     * @param args the command line arguments
     */
    private static String name,date;
    private static int location,id;


    public static void main(String[] args) throws IOException {
         String host = "jdbc:mysql://localhost/onurDB";
        String user = "onur";
        String pass = "onurdb958";

        try(Connection conn = DriverManager.getConnection(host,user,pass);
            Statement smt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);   
           ){

            insertData(smt);
        }catch(SQLException ex){
            System.out.println("SQLException : " + ex);
        }  
    }
       private static void insertData(Statement smt) throws SQLException, IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter ID : ");
        String id = bf.readLine();
        System.out.print("Enter Name : ");
        String name = bf.readLine();
        System.out.print("Enter Location : ");
        String location = bf.readLine();
        System.out.print("Enter Date : ");
        String date = bf.readLine();
        String sql;
        sql = "INSERT INTO `GoogleLoc`(`ID`, `NAME`, `LOCATION`, `DATE`) VALUES ('" + id + "','" + name + "','"
                + location + "','" + date + "')";
        smt.executeUpdate(sql);
        System.out.println("Success! data inserted.");
    }
}

I can imagine my fault in @WebMethod 我可以想象我在@WebMethod中的错误

Somebody can help me please??? 有人可以帮我吗???

THANKS FOR ALL.. 谢谢大家..

Your NullPointerException is raised from the WebserviceTesterServlet class which is a GlassFish WS tool class for helping you to call a webservice. NullPointerException是从WebserviceTesterServlet类引发的,该类是GlassFish WS工具类,用于帮助您调用Web服务。 It's a graphical client for your webservice. 它是您的Web服务的图形客户端。 Maybe, the problem comes from this class, maybe from your implementation. 也许问题出在此类中,也许是您的实现引起的。 With this stracktrace, it is hard to guess. 使用此stracktrace,很难猜测。

The problem is that WebserviceTesterServlet doesn't give a helpful exception. 问题在于WebserviceTesterServlet没有给出有用的异常。 So, we cannot understand the cause. 因此,我们无法理解原因。 I propose you three solutions to solve your problem : 我为您提出三种解决方案来解决您的问题:

  • Download the source jar containing WebserviceTesterServlet (take the version you use), put a breakpoint on the line 342 of this class and run your server in debug mode. 下载包含WebserviceTesterServlet的源jar(采用您使用的版本),在此类的第342行上放置一个断点,然后以调试模式运行服务器。
  • Trying with SOAP UI for calling your operation. 尝试使用SOAP UI调用您的操作。
  • Create your own code for calling the webService and test your webservice operation. 创建您自己的代码以调用webService并测试您的webservice操作。 You can do it easily by generating the proxy classes. 您可以通过生成代理类轻松地做到这一点。 You can use for example use JAX-WS and do a wsimport. 例如,您可以使用JAX-WS并执行wsimport。

Solved ! 解决了 !

We need to change codes like this : 我们需要更改这样的代码:

    package com.me.coder;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.ws.rs.GET;

/**
 *
 * @author Coder ACJHP
 */
@WebService(serviceName = "GetWriteToDB")
public class GetWriteToDB {

    /**
     * Web service operation
     *
     * @param id
     * @param name
     * @param location
     * @param date
     * @return
     */
    @GET
    @WebMethod(operationName = "insertUser")
    public boolean insertUser(@WebParam(name = "id") int id, @WebParam(name = "name") String name,
            @WebParam(name = "location") double location, @WebParam(name = "date") String date) {
        boolean insertStatus = false;
        final String host = "jdbc:mysql://localhost/onurDB";
        final String user = "onur";
        final String pass = "onurdb958";
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
            Logger.getLogger(GetWriteToDB.class.getName()).log(Level.SEVERE, null, ex);
        }
        try (
                Connection conn = DriverManager.getConnection(host, user, pass);
                Statement smt = conn.createStatement();) {
            String SQL = "INSERT INTO `GoogleLoc`(`ID`, `NAME`, `LOCATION`, `DATE`) VALUES ('" + id + "','" + name + "','" + location + "','" + date + "')";
            int x = smt.executeUpdate(SQL);
            if (x > 0) {
                insertStatus = true;
            }

        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
        return insertStatus;
    }

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM