Connect to database using jsf

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?

Answers


To get connected to 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

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

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

This was working successfully.


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

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.

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.


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.


Need Your Help

Creating iOS/OSX Frameworks: is it necessary to codesign them before distributing to other developers?

ios code-signing ios-frameworks

I am learning how to create iOS and OSX frameworks. Let's take iOS for example, the following steps work for me so far:

can't get Meteor slingshot to work

javascript meteor meteor-slingshot

I am trying to get slingshot to work but having a hard time, I am attaching here the code I have.