使用netbeans执行Web服务

时间:2016-01-03 19:35:25

标签: java mysql web-services jdbc netbeans

我想制作一个与localhost上的一个数据库通信的Web服务。数据库的名称是" Reservation"并且只有一个名称为#34;座位"的矩阵。该表有4个字段:(int)ID,(varchar)位置,(varchar)类(int)。例如,一个记录具有以下字段:ID = 1,location =" Window",class =" Economy",take = 0。我希望我的Web服务更新一条记录,其中所采用的字段为零。

我的代码是:

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

@WebService( name = "Reservation", serviceName = "ReservationService" )
public class ReservationSOAP
{
   private static final String DATABASE_URL = "jdbc:mysql://localhost:8080/Reservation";
   private static final String USERNAME = "root";
   private static final String PASSWORD = "root";

   private Connection connection;
   private PreparedStatement lookupSeat;
   private PreparedStatement reserveSeat;

   // a WebMethod that can reserve a seat
   @WebMethod( operationName = "reserve" )
   public boolean reserve( @WebParam( name = "seatType" ) String seatType, @WebParam( name = "classType" ) String classType )
   {
      try
      {
         connection = DriverManager.getConnection( DATABASE_URL, USERNAME, PASSWORD );
         lookupSeat = connection.prepareStatement( "SELECT ID FROM seats WHERE taken = ? AND location = ? AND class = ?" );

         lookupSeat.setInt( 1, 0 );
         lookupSeat.setString( 2, seatType );
         lookupSeat.setString( 3, classType );

         ResultSet resultSet = lookupSeat.executeQuery();

         // if requested seat is available, reserve it
         while ( resultSet.next() )
         {
            int seat = resultSet.getInt( 1 );
            reserveSeat = connection.prepareStatement( "UPDATE seats SET taken = 1 WHERE ID = ?" );
            reserveSeat.setInt( 1, seat );
            reserveSeat.executeUpdate();
            return true;
         } // end if

         return false;
      } // end try
      catch ( SQLException e )
      {
         e.printStackTrace();
         return false;
      } // end catch
      catch ( Exception e )
      {
         e.printStackTrace();
         return false;
      } // end catch
      finally
      {
         try
         {
            lookupSeat.close();
            reserveSeat.close();
            connection.close();
         } // end try
         catch ( Exception e )
         {
            e.printStackTrace();
            return false;
         } // end catch
      } // end finally
   } // end WebMethod reserve
} // end class Reservation

问题是web方法返回False,因为数据库没有更新。

1 个答案:

答案 0 :(得分:0)

首先 - 您应该使用try-with-resource。您的reserveSeat语句null可能仍然在finally块内,抛出NullPointerException,导致返回false。 ResultSet 从不关闭。

您对//end if

上的右大括号发表评论while()

我建议你的代码使用局部变量。

考虑这个改变的实现。我还引用了一些System.out.println,使您能够检查收到的参数是否真的可以在您的SQL中使用。

@WebService(name = "Reservation", serviceName = "ReservationService")
public class ReservationSOAP {
    private static final String DATABASE_URL = "jdbc:mysql://localhost:8080/Reservation";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "root";

    // a WebMethod that can reserve a seat
    @WebMethod(operationName = "reserve")
    public boolean reserve(@WebParam(name = "seatType") String seatType,
            @WebParam(name = "classType") String classType) {
        try (Connection connection = DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD);
                PreparedStatement lookupSeat = connection
                        .prepareStatement("SELECT ID FROM seats WHERE taken = ? AND location = ? AND class = ?")) {
            System.out.println("seatType: " + seatType);
            System.out.println("classType: " + classType);
            lookupSeat.setInt(1, 0);
            lookupSeat.setString(2, seatType);
            lookupSeat.setString(3, classType);

            try (ResultSet resultSet = lookupSeat.executeQuery()) {

                // if requested seat is available, reserve it
                if (resultSet.next()) {
                    int seat = resultSet.getInt(1);
                    System.out.println(
                            "Found unreserved seat: " + seat + " in class " + classType + " and seatType " + seatType);
                    try (PreparedStatement reserveSeat = connection
                            .prepareStatement("UPDATE seats SET taken = 1 WHERE ID = ?")) {
                        reserveSeat.setInt(1, seat);
                        reserveSeat.executeUpdate();
                        return true;
                    }

                }
            }
            System.out.println("No Seat found.");
            return false;
        } // end try
        catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    } // end WebMethod reserve
} // end class Reservation