java 使用jsf连接数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/319936/
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-10-29 11:56:43  来源:igfitidea点击:

Connect to database using jsf

javamysqldatabasemaven-2

提问by Warrior

How do I connect to the database(MYSQL) in connection bean using JSF to retrieve its contents. Also please let me know how do I configure the web.xml file?

如何使用 JSF 连接到连接 bean 中的数据库(MYSQL)以检索其内容。另外请让我知道如何配置 web.xml 文件?

采纳答案by Warrior

To get connected to mysql:

要连接到 mysql:

public void open() {
        try {
            String databaseName = "custom";
            String userName = "root";
            String password = "welcome";

            // 
            String url = "jdbc:mysql://localhost/" + databaseName;

            Class.forName("com.mysql.jdbc.Driver").newInstance();
            connection = DriverManager.getConnection(url, userName, password);
        } catch (Exception e) {
            System.out.println("Not able to connect");
        }
    }

In this case there is nothing to change in web.xml.But you to add this in pom.xml

在这种情况下,web.xml 中没有什么可更改的。但是您可以在 pom.xml 中添加它

 <dependency>
            <groupId>groupId = mysql</groupId>

            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
</dependency> 

This was working successfully.

这是成功的工作。

回答by alexmeia

Here is a very good tutorial on how to use DAO with JSF in the best way:

这是一个关于如何以最佳方式将 DAO 与 JSF 结合使用的非常好的教程:

http://balusc.blogspot.com/2008/07/dao-tutorial-use-in-jsf.html

http://balusc.blogspot.com/2008/07/dao-tutorial-use-in-jsf.html

If you are using JSF, that website can be a good place to find solutions for common problems. There are great and complete examples.

如果您使用的是 JSF,那么该网站可能是查找常见问题解决方案的好地方。有很好的和完整的例子。

Anyway, JSF is a framework that manages the view and the controller layers. For the model layer and the access to a database there are not big difference if you use JSF or any other java web framework that manages the view/controller part of your application.

总之,JSF 是一个管理视图层和控制器层的框架。对于模型层和对数据库的访问,如果您使用 JSF 或任何其他管理应用程序视图/控制器部分的 Java Web 框架,则没有太大区别。

回答by Chris Kimpton

Here is an example using Hibernate and HSQL- but the basic ideas of separating the db stuff out should be valid and it includes a configured web.xml.

这是一个使用 Hibernate 和 HSQL示例- 但将 db 内容分离出来的基本思想应该是有效的,它包括一个配置好的 web.xml。