简体   繁体   English

当使用JDBC连接到数据库时,Rest API忽略数据库中带来的数据时返回一个空列表

[英]Rest API returns an empty list when connecting to database using JDBC ignoring the data brought from the database

Im doing a project on my own and the point of the project is to make an a rest API in java and bring data from database using JDBC. 我自己完成一个项目,该项目的重点是用Java创建一个rest API,并使用JDBC从数据库中获取数据。 The problem is when i make the connection and retrieve the data using main method everything works fine but when i try to retrieve the data through a get request, it returns an empty list. 问题是当我建立连接并使用main方法检索数据时,一切正常,但是当我尝试通过get请求检索数据时,它返回一个空列表。 This is the endpoint 这是终点

@Path("/users")
public class UserResource {
UserService userService = new UserService();

@GET
@Path("/all")
@Produces(MediaType.APPLICATION_JSON)
public Response getAllUsers() throws InterruptedException {
    List<User> users = userService.getAllUsers();
    GenericEntity<List<User>> wrapper = new GenericEntity<List<User>>(users) {
    };
    return Response.ok(wrapper).build();
}

} }

And this is the class that connects to database using JDBC 这是使用JDBC连接到数据库的类

public class ConnectionJDBC {

private static String USERNAME = "root";
private static String PASSWORD = "root";
private static String CONSTRING = "jdbc:mysql://localhost:3306/";
private static String DATABASE = "users_db";

static Connection connection;

public ConnectionJDBC() {
}

public static Connection getConnectionToDB() {
    try {
        String url = CONSTRING + DATABASE;
        connection = DriverManager.getConnection(url, USERNAME, PASSWORD);
        if (connection != null) {
            System.out.println("Connected");
        } else {
            System.out.println("Connection faild");
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Something went wrong before establishing the connection");
    }
    return connection;

}

public List<User> getUsers() {
    connection = getConnectionToDB();
    String query = "SELECT * FROM users_db.user_info;";
    List<User> userListRetrived = new ArrayList<>();

    try {
        PreparedStatement statement = connection.prepareStatement(query);
        ResultSet dataRetrieved = statement.executeQuery();
        while (dataRetrieved.next()) {
            User user = new User(dataRetrieved.getInt("id"), 
                    dataRetrieved.getString(2),
                    dataRetrieved.getString(3));
            userListRetrived.add(user);
            return userListRetrived;

        }
    } catch (Exception e) {
        e.printStackTrace();
        System.err.println("Something went wrong when retriving the data");
    }
    return userListRetrived;

}

} }

UserService class is here UserService类在这里

public class UserService {
private ConnectionJDBC connectionJDBC = new ConnectionJDBC();

public UserService() {
}

public List<User> getAllUsers() {
    ConnectionJDBC connectionJDBC = new ConnectionJDBC();
    List<User> usersList = connectionJDBC.getUsers();
    return usersList;
}

} }

The user class is a normal java bean. 用户类是普通的Java bean。 It feels like getUsers method is ignoring bringing the data from the database and instead it returns the instantiated empty list instead. 感觉像是getUsers方法正在忽略从数据库中获取数据,而是返回实例化的空列表。 I'm using jersey and deploying the project on Glassfish. 我正在使用jersey并将项目部署在Glassfish上。 Any clues on how i can fix that or explanation for this situation? 关于如何解决此问题或对此情况进行解释的任何线索?

Many thanks in advance! 提前谢谢了!

You didn't invoke userService.getUsers() method from userResource() class. 您没有从userResource()类调用userService.getUsers()方法。 Please update your code in userResource() class as follows 请按以下方式更新userResource()类中的代码

GenericEntity<List<User>> wrapper = new GenericEntity<List<User>>(userService.getUsers());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM