无法在addProduct方法中加载create coonection

时间:2017-01-26 19:32:17

标签: java jdbc java-web-start

package com.Foodmart;

import javax.jws.*;

import java.sql.*;

@WebService(name = "FoodMart", serviceName = "FoodMartService", portName = "FoodMartHtt)
public class FoodmartWS {

    ProductDetails prod = new ProductDetails();

    @WebMethod(operationName = "check")
    public boolean Authenticate(String user, String pass) {
        PreparedStatement psmt = null;
        Connection c = null;
        try {
            c = ConnectionDB.getConnection();
            String selectSQL = "select * from Employee where Username=? and Password=?";
            psmt = c.prepareStatement(selectSQL);
            psmt.setString(1, user);
            psmt.setString(2, pass);
            psmt.executeQuery();
            c.close();
            return true;
        } catch (SQLException e) {
            return false;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

在AddProduct

中的此方法错误中
    @WebMethod(operationName = "AddProduct")
    public ProductDetails ProdcutAdd(int prodid, double qty) {
        PreparedStatement preparedStatement = null;
        Connection con = null;
        try {
            System.out.println("try");

//here i am not able to create another one connection
            con = ConnectionDB.getConnection();
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Food", "root", "manager1");
            String selectSQL = "select Product_Id,Product_Name,Product_Price,Product_Qty from Products where Product_Id=?;";
            System.out.println("bbefore prepared");
            preparedStatement = con.prepareStatement(selectSQL);
            preparedStatement.setInt(1, prodid);
            System.out.println("before result set");
            ResultSet rs = preparedStatement.executeQuery();
            System.out.println("query Executed");
            rs.next();
            prod.setProductId(rs.getInt("Product_Id"));
            prod.setProductName(rs.getString("Product_Name"));
            prod.setProductPrice(rs.getDouble("Product_Price"));
            prod.setProductQty(rs.getInt("Product_Qty"));
            System.out.println("obj set");
            con.close();
            System.out.println("in" + prod);
            return prod;
        } catch (SQLException e) {
            System.out.println(e);
            return null;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

1 个答案:

答案 0 :(得分:0)

为什么在获得连接后添加此行:

con = ConnectionDB.getConnection();
//con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Food", "root", "manager1");

所以只需将其删除或评论即可。

注意 你最后错过了"),所以这也会产生问题:

@WebService(name = "FoodMart", serviceName = "FoodMartService", portName = "FoodMartHtt)

希望这可以帮到你。