Jersey REST客户端无法创建数据库

时间:2016-08-05 03:03:39

标签: java mysql rest

我一直在尝试以编程方式将我的REST Web服务链接到数据库,但无法成功。我是新手,所以我按照一些教程观看了一些视频,但我没有做任何工作。

以下是我的数据库处理程序文件:

import com.feedme.model.Food;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class MyDBHandler {
// List of variables
public static final String DATABASE_NAME = "restaurants";

public static final String TABLE_FOOD = "food";
public static final String FOOD_COLUMN_ID = "_fid";
public static final String FOOD_COLUMN_NAME = "fname";
public static final String FOOD_COLUMN_PRICE = "fprice";
public static final String FOOD_COLUMN_ICON = "ficon";
public static final String MENU_COLUMN_ID = "_mid";
public static final String RESTO_COLUMN_ID = "_rid";
public static final String FOOD_COLUMN_SUPPLEMENTS = "fsupplements";

public static String strSeparator = "__,__";

// JDBC driver name and database URL  
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
static final String DB_URL = "jdbc:derby://localhost:1527/";

// Database credentials
static final String USER = "user";
static final String PASS = "password";

public static void main(String[] args) {
   System.out.println("In MyDBHandler.java");
   Connection conn = null;
   Statement db = null;

    try {

        // Register JDBC driver
        Class.forName(JDBC_DRIVER);

        // Open a connection
        conn = DriverManager.getConnection(DB_URL, USER, PASS);

        db = conn.createStatement();

        // Create the resto database
        String sql = "CREATE DATABASE " + DATABASE_NAME;
        db.executeUpdate(sql);

        // Create the food table
        String food = "CREATE TABLE " + TABLE_FOOD + "(" +
            FOOD_COLUMN_ID + " INTEGER PRIMARY KEY " + ", " +
            FOOD_COLUMN_NAME + " VARCHAR(20) " + ", " +
            FOOD_COLUMN_PRICE + " VARCHAR(20) " + ", " +
            FOOD_COLUMN_ICON + " VARCHAR(20) " + ", " +
            RESTO_COLUMN_ID + " VARCHAR(20) " + ", " +
            MENU_COLUMN_ID + " VARCHAR(20) " + ", " +
            FOOD_COLUMN_SUPPLEMENTS + " VARCHAR(20) " +
            ")";
        db.executeUpdate(food);

    }catch(SQLException se){
        //Handle errors for JDBC
        se.printStackTrace();
    }catch(Exception e){
        //Handle errors for Class.forName
        e.printStackTrace();
    }finally{
        //finally block used to close resources
        try{
            if(db != null)
                db.close();
        }catch(SQLException se){

        }
        try{
           if(conn != null)
              conn.close();
        }catch(SQLException se){
           se.printStackTrace();
        }//end finally try
    }//end try
}    

// Add a new food to the Food table
public Food addFood(Food food) {
    Connection conn = null;
    Statement db = null;

    try {
        // Register JDBC driver
        Class.forName(JDBC_DRIVER);

        // Open a connection
        conn = DriverManager.getConnection(DB_URL, USER, PASS);

        db = conn.createStatement();

        String suppString = convertArrayToString(food.getSupplements());

        String query = "INSERT INTO " + TABLE_FOOD + " VALUES(" + food.getFname() +
                ", " + food.getFprice() + ", " + food.getFicon() + ", " + food.getrId() +
                ", " + food.getmId() + ", " + suppString + ");";
        db.executeUpdate(query);
    } catch(SQLException se){
        //Handle errors for JDBC
        se.printStackTrace();
    }catch(Exception e){
        //Handle errors for Class.forName
        e.printStackTrace();
    }finally{
        //finally block used to close resources
        try{
            if(db != null)
                db.close();
        }catch(SQLException se){

        }
        try{
           if(conn != null)
              conn.close();
        }catch(SQLException se){
           se.printStackTrace();
        }            
    }

    return food;
}

我希望创建数据库和表,然后通过post请求填充表。但没有任何反应。

下面是应该调用数据库的资源文件(虽然它似乎没有这样做):

@Path("/foods/")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class FoodResource {

MyDBHandler dbHandler = new MyDBHandler();

@GET
public List<Food> getFoods() {
    List<Food> foodList = dbHandler.getAllFoods();

    return foodList;
}

@POST
public Food addFood(Food food) {
    return dbHandler.addFood(food);                
}

@PUT
@Path("/{foodId}")
public Food updateFood(@PathParam("messageId") long id, Food food) {
    return dbHandler.updateFood(id, food);
}

@DELETE
@Path("/{messageId}")
public void deleteMessage(@PathParam("messageId") long id) {
    dbHandler.deleteFood(id);
}

@GET
@Path("/{foodId}")
public Food getFood(@PathParam("foodId") long id) {        
    return dbHandler.getFood(id);        
}

}

我对例外日志一无所知,所以它让我感到非常困惑。

1 个答案:

答案 0 :(得分:0)

成为新人很难,但这里有一些事情可以帮助你。提供的代码存在一些问题。我会仔细检查你的网址连接到mysql。它看起来并不适合我。您还尝试在创建数据库之前连接到数据库restaurants

您正在捕捉几个例外并将其打印出来。我很惊讶你在运行代码时没有看到这些代码(我在运行你的代码时做了)。在问题中提供异常信息总是有用的。