Search code examples
javamysqlweb-servicesjax-ws

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!?!?

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. 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

Somebody can help me please???

THANKS FOR ALL..


Solution

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

    }