java 如何在servlet中从数据库中检索数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32000430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:29:54  来源:igfitidea点击:

how to retrieve data from database in servlet

javacrudservlet-3.0

提问by Asad

im beginners in java servlet... i working on curd operation... ...data not displaying please tell me where im wrong....

我是 Java servlet 的初学者……我正在研究豆腐操作…………数据不显示,请告诉我我错在哪里……

DAO.java

DAO.java

public void select(){

        try{
            Class.forName(jdbcDriver);
            Connection conn = DriverManager .getConnection(connectionString,username,password);
        PreparedStatement create = conn.prepareStatement("select * from student.users");

            ResultSet rs;
            rs = create.executeQuery();
            while(rs.next()){
                int id =rs.getInt("id");
                String name = rs.getString("fname");

            }
            }
            catch(Exception e){
                e.printStackTrace();
            }

    }

servlet.java

servlet.java

DAO user = new DAO();   /// i create object of DAO
user.select();  //calling select method

out.write("<td>");
                    out.write("<p>"+user.id+"</p>");    ///tried many from different way
            out.write("</td>");
            out.write("<td>"); 
                    out.write("<p>"+user.name+"</p>");    ///
            out.write("</td>"); 

i tried many from different way but no luck.... can you tell me where I'm wrong

我从不同的方式尝试了很多但没有运气....你能告诉我我错在哪里吗

采纳答案by Luciano Almeida

I see some problems there. First i think your select method should return a List of users. So you can implement that as bellow

我看到那里有些问题。首先,我认为您的 select 方法应该返回一个用户列表。所以你可以实现如下

public List<User> select(){
    List<User> result = new ArrayList<User>();
    try{
        Class.forName(jdbcDriver);
        Connection conn = DriverManager .getConnection(connectionString,username,password);
    PreparedStatement create = conn.prepareStatement("select * from student.users");

        ResultSet rs;
        rs = create.executeQuery();
        while(rs.next()){
            int id =rs.getInt("id");
            String name = rs.getString("fname");
            User user = new User(); // Creating a user object to fill with user data (I imagine that you have a user class in your model)
            user.setId(id);
            user.setName(name);
            //Add the retrived user to the list
            result.add(user);

        }
        //Returning the list of users.
        return result;
    }catch(Exception e){
        e.printStackTrace();
        return null;
    }

}

So in your servlet you can call this method to return this list and show it.

因此,在您的 servlet 中,您可以调用此方法来返回此列表并显示它。

 DAO daoUser = new DAO();   /// i create object of DAO
 List<User> users = daoUser.select();  //calling select to get list of users
 out.write("<table>");
 for(User user : users){ //Running through the list to show all users retrived
    out.write("<td>");
    out.write("<p>"+user.id+"</p>");    
    out.write("</td>");
    out.write("<td>"); 
    out.write("<p>"+user.name+"</p>");    ///
    out.write("</td>");
 }
 out.write("</table>"); 

So that's it. The logic it's that i'm not sure if it will work, may a forget some detail but this is the way to do. Hope that helps.

就是这样了。逻辑是我不确定它是否会起作用,可能会忘记一些细节,但这是方法。希望有帮助。