gpt4 book ai didi

java - Jersey REST 客户端未创建数据库

转载 作者:行者123 更新时间:2023-11-29 20:36:33 25 4
gpt4 key购买 nike

我一直在尝试以编程方式将 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;
}

我希望创建数据库和表,然后通过发布请求填充表。但什么也没发生。

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

@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);
}

}

我在异常日志中没有得到任何信息,所以这让我非常困惑。

最佳答案

成为新人很困难,但这里有一些可以帮助你继续前进的事情。提供的代码存在几个问题。我会仔细检查你的连接 mysql 的 url。我觉得不太对劲。您还尝试在创建数据库 restaurants 之前连接该数据库。

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

关于java - Jersey REST 客户端未创建数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38780439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com