简体   繁体   English

如何从servlet中的数据库检索数据

[英]how to retrieve data from database in servlet

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 我以不同的方式尝试了很多,但没有运气。...你能告诉我我错了吗

I see some problems there. 我在那里看到了一些问题。 First i think your select method should return a List of users. 首先,我认为您的select方法应该返回用户列表。 So you can implement that as bellow 所以你可以像下面这样实现

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. 希望能有所帮助。

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

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