Help! Database+MySQL Simple Web App

Hi everybody!

I am a Vaadin newbie, in the last weeks I’ve been learing about UI components, now I decided that I want to create a simple web app with Database connections using MySQL and Apache Tomcat.

I searched in Vaadin Forum about it but I didn’t find or I still don’t understand how it works.


I have the next simplee web app:


the next picture shows the “Hello Vaadin user” Label and the ComboBox with empty fields.


now I want to create a Database with a table named “Numbers”:

[b]

My question is: How can I add those numbers from my Database to “pick” ComboBox ?

I know that Query would be something like this: (simple)
[/b]

[b]
But I don’t know how to! and I don’t know all the configurations I have to do to make it works :confused:

Do I have to modify the META-INF > context.xml archive?

Please I would be so grateful if you help me!!

I use Netbeans. I followed this tutorial:
Netbeans mysql web app
with JSP and I understood it. I thought Vaadin was similar but not!

Thanks!!! :slight_smile:

[/b]

Hi, You can use SQLContainer. Or some other container that works with MYSQL. I use SQLContainer and satisfied with this container. IT’s very simple to use. it has tutorial and Manual documentation. So I think you’ll not have problems with UI and Database connections.

Hi…Maria,

I am also working on a web App using Vaadin framework. I have almost completed it. :slight_smile: I’m using
eclipse IDE
,
MySQL database
,
eclipselink(download jars)
, and
JPA container
.
JPA container is very useful and simple.

As i got your requirement, JPA container can be useful for you. Get the data from the database into JPA container and bind it to the UI (Table, Tree, ComboBox etc.).

Here is the following piece of code to get data into container -

    static EntityProvider<Allvendoritem> vitemprov;
public EntityContainer<Allvendoritem> vitemcontainer;
static EntityManagerFactory emf;
static EntityManager em;

emf = Persistence.createEntityManagerFactory("vendorapp");
em=emf.createEntityManager();
vitemcontainer=new JPAContainer<Allvendoritem>(Allvendoritem.class);
vitemprov = new BatchableLocalEntityProvider<Allvendoritem>(Allvendoritem.class,em);
vitemcontainer.setEntityProvider(vitemprov);
vitemcontainer.commit();

“Allvendoritem” is Entity class name and i’m creating container for it. As you add JPA libraries you can create Entity classes (bean classes with setter getter methods).
Right click on the project → click on JPA tools → click on Create entity from table
. It’ll create entity classes corresponding to the all tables in the database…

You have to create JPA project and do the database connection part in “persistence.xml” file.

I hope it’ll help you. Ask me again if you find difficulty. Though it was my first Vaadin web app and a lot of thing are more to learn. But i’ll try my best to help. :slight_smile:

Good Luck…!

Hi Aatif,

Is there any link where I can see some VAADIN web app which is integrated with some databese? Can you post the source code of your working example of web app?

Thanks,
Vikash

Using a database with Vaadin is not different from using a database in any Vaadin application. Keep your UI and database stuff separate and it will save you some pain. Then follow any tutorial on how to connect to a database through java.
Google search: Mysql java example
.

Hi Jens,

Thanks for reply.

Can you please let me know any url where I can see samples of Vaadin application using JPA/JDO.
I am novice to Vaadin and JPA/JDO. And still searching for how to start with JPA/JDO together with Vaadin.

I was trying to understand and run the AddressBook Application from the SVN source. Initially I got some compilation issues. later on I was succesfully able to run/debug it.
However, I am getting NullPointerApplication at the following line number 2 in DatabaseHelper class:

[b]

                Object id = cityContainer.addItem();
                cityContainer.getContainerProperty(id, "NAME").setValue(cities[i]
);

[/b]

After debugging I found that cityContainer.getContainerProperty(id, “NAME”) gives null. Please suggest.

Thanks,
Vikash

The container that you are using most likely doesn’t contain a property called “NAME”.

Hi Jens,

I didn’t get the statement "The container that you are using most likely doesn’t contain a property called “NAME”. "

Do you mean, that database table has no property like name, that’s why I am getting this error?
But, I using a table which has the name property.

Thanks,
Vikash

I’m not sure about your setup, so I can’t say for sure where your problem is. It depends on if you are using a standard container like IndexedContainer or something that fetches directly from the database, like SQLContainer.

In Vaadin, containers have a set of properties (or call them columns if that makes it clearer). What your example says is that you are trying to fetch a property called NAME from the row ‘id’. You container for the table doesn’t contain a property NAME, so it returns null. If you use a indexedContainer then you haven’t added that property. If you use a SQLContainer then either contacting the database went wrong, the database table doesn’t contain a column NAME or you are not using the correct container in your vaadin table.

Hi Maria, I make a example using jdbc + mysql and it works perfectly. Here´s the code:

Class that conect with the database

public class Conexao {
    private final static String driver = "com.mysql.jdbc.Driver";
    private final static String path = "jdbc:mysql://localhost:3306/vaadindatabase";
    private final static String usuario = "root";
    private final static String senha = "root";
    private static Connection conexao;

    public static Connection getConnection() {
        try {
            Class.forName(driver);
            conexao = DriverManager.getConnection(path, usuario, senha);
        } 
        catch (SQLException sqle) {
            //JOptionPane.showMessageDialog(null, "Impossivel se conectar a base de dados", "Login", JOptionPane.ERROR_MESSAGE);
        } 
        catch (ClassNotFoundException cnfe) {
            //JOptionPane.showMessageDialog(null, "Impossivel encontrar a classe driver", "Login", JOptionPane.ERROR_MESSAGE);
        }
        return conexao;
    }    
}

Class that have the method to view the data tha is in the database

public class PessoaDao {
    Connection conexao;
    PreparedStatement ps;
    ResultSet rs;   
    
    public PessoaDao() {
    }
    public List<Pessoa> visualizar() {
        List<Pessoa> lp = new ArrayList<Pessoa>();
    
        try {
            conexao = Conexao.getConnection();
            ps = conexao.prepareStatement("SELECT *FROM pessoa");
            rs = ps.executeQuery();
            if (!rs.next()) {
                //JOptionPane.showMessageDialog(null, "Não existem armazens cadastrados", "Armazem",
                        //JOptionPane.ERROR_MESSAGE);
            } else {
                rs.beforeFirst();
                while (rs.next()) {
                    Pessoa p = new Pessoa();
                    p.setCodigo(rs.getInt("codigo"));
                    p.setNome(rs.getString("nome"));
                    lp.add(p);
                }
            }
        } catch (SQLException exception) {
            
        }
        return lp;
    }
}

Main class

public class ComboBoxDinamica extends Application{
    List<Pessoa> lp = new ArrayList<Pessoa>();
    Pessoa p = new Pessoa();
    PessoaDao pd = new PessoaDao();
    @Override
    public void init() {
        final Window mainWindow = new Window("Dynamic combobox");
        ComboBox cboNomes = new ComboBox("Nomes");
        lp = pd.visualizar();
        for(int i = 0; i < lp.size(); i++){
            cboNomes.addItem(lp.get(i).getNome());
        }
        mainWindow.addComponent(cboNomes);
	setMainWindow(mainWindow);
    }
}

Object id = cityContainer.addItem();
cityContainer.getContainerProperty(id, “NAME”).setValue(cities[i]
);
[/code]
[/b]
heyy i am facing some how the same problem in database helper class,can you please tell me how can i solve it,

here is the code that giving me an error.

Object id = personContainer.addItem();
personContainer.getContainerProperty(id, “FIRSTNAME”)
.setValue(firstName);

this give me following error,
HTTP Status 500 - java.lang.NoClassDefFoundError: com/vaadin/data/Property$ConversionException


please help me ,regardz