Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

Can someone help me with my first datatable?

Thread view: 
gbattine - 24 Jul 2006 16:53 GMT
HI guys,
i'm a new user of JSF and now i'm descovering great possibilities
offered by datatable.
I read the article on
http://balusc.xs4all.nl/srv/dev-j2p-dat.html#RetrieveAndStoreData,

Not everythings are for  me clear...
I've to show in a datatable a mysql table

utenti(firstName,lastName,city)

which its relative values..
i've developed a User class with the tree values as private variables
and getter and setter methods...
For this class i've developed a method called userList() that should
return a List of all the users presents in the table,ordereb by name.
Can someone say me what are the successive steps?Can you help me with a
bit theory about wrapper class?
Can you help me step by step because i don't understand internet
articles and forum discussions?

public List userList() throws Exception {

            Context ctx = new InitialContext();

            if (ctx == null) {

                throw new Exception("Boom - No Context");

            }

            Context envCtx = (Context) ctx.lookup("java:comp/env");
            DataSource ds = (DataSource) ctx.lookup("java:comp/env/MysqlJNDI");
// "java:comp/env/jdbc/nomedb"
            Connection conn = ds.getConnection();
            Statement stm = conn.createStatement();
            ResultSet rst = stm.executeQuery("select * from utenti order by
username");

            List users = new ArrayList();
            while (rst.next()){

users.add(rst.getString("firstname"));
                users.add(rst.getString("lastname"));
                users.add(rst.getString("city"));
                            }
            return users;   
    }

Please help me....
balusc@xs4all.nl - 24 Jul 2006 19:49 GMT
> Not everythings are for  me clear...
> I've to show in a datatable a mysql table
[quoted text clipped - 4 lines]
> Can you help me step by step because i don't understand internet
> articles and forum discussions?

Hi,

It should look like:

// ------------------------------------------------------------
List users = new ArrayList();
while (rst.next()){
   MyUser myUser = new MyUser();
   myUser.setFirstName(rst.getString("firstname"));
   myUser.setLastName(rst.getString("lastname"));
   myUser.setCity(rst.getString("city"));
   users.add(myUser);
}
return users;
// ------------------------------------------------------------

Where MyUser.java represents the same as MyTable.java at my article.
gbattine - 25 Jul 2006 09:39 GMT
thanks very much...
i need still a little help.
I have a User class(like myTable) that has a method that returns a list
of User objects,called users.
Now i have to define a class like MyBean in the article that has to
load and get this List users.This is the piece less clear for me.....

package giu;
import giu.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

public class UsersTable {
    private List users;
                public void loadUsers() {

        setUsers(User.userList());

    }

    public List getUsers() {
                          loadUsers(); // Reload after every request.
              return users;
   }

   public void setUsers(List users) {
       this.users = users;
   }

where users cames from

package giu;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

/**
* <p>
* Title:
* </p>
* <p>
* Description:
* </p>
* <p>
* Copyright: Copyright (c) 2002
* </p>
* <p>
* Company:
* </p>
*
* @author unascribed
* @version 1.0
*/

public class User implements Serializable {
    /**
    *
    */
    private static final long serialVersionUID = 1L;

    /**
    *
    */

    private String firstName;

    private String lastName;

    private String date;

    private String city;

    private String address;

    private String profession;

    private String email;

    private String login;

    private String password;

    private String teamname;

    private String role;

    public User() {};

    public User(String login, String firstName, String lastName,
            String password, String teamname, String role) {
        this.login = login;
        this.firstName = firstName;
        this.lastName = lastName;
        this.password = password;
        this.teamname = teamname;
        this.role = role;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getLogin() {
        return login;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPassword() {
        return password;
    }

    public void setTeamname(String teamname) {
        this.teamname = teamname;
    }

    public String getTeamname() {
        return teamname;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public String getRole() {
        return role;
    }

    public String toString() {
        return firstName + " " + lastName;
    }

    public java.lang.String getAddress() {
        return address;
    }

    public void setAddress(java.lang.String address) {
        this.address = address;
    }

    public java.lang.String getCity() {
        return city;
    }

    public void setCity(java.lang.String city) {
        this.city = city;
    }

    public java.lang.String getDate() {
        return date;
    }

    public void setDate(java.lang.String date) {
        this.date = date;
    }

    public java.lang.String getEmail() {
        return email;
    }

    public void setEmail(java.lang.String email) {
        this.email = email;
    }

    public java.lang.String getProfession() {
        return profession;
    }

    public void setProfession(java.lang.String profession) {
        this.profession = profession;
    }
public static  List userList() throws Exception {

            Context ctx = new InitialContext();

            if (ctx == null) {

                throw new Exception("Boom - No Context");

            }

            Context envCtx = (Context) ctx.lookup("java:comp/env");
            DataSource ds = (DataSource) ctx.lookup("java:comp/env/MysqlJNDI");
// "java:comp/env/jdbc/nomedb"
            Connection conn = ds.getConnection();
            Statement stm = conn.createStatement();
            ResultSet rst = stm.executeQuery("select * from utenti order by
username");

            List users = new ArrayList();

            while (rst.next()){
               User myUser = new User();
               myUser.setFirstName(rst.getString("firstname"));
               myUser.setLastName(rst.getString("lastname"));
               myUser.setCity(rst.getString("city"));
               myUser.setPassword(rst.getString("password"));
               myUser.setLogin(rst.getString("username"));
               myUser.setTeamname(rst.getString("teamname"));
               myUser.setRole(rst.getString("type"));
               myUser.setDate(rst.getString("date"));
               myUser.setAddress(rst.getString("address"));
               myUser.setProfession(rst.getString("profession"));
               myUser.setEmail(rst.getString("email"));
               users.add(myUser);
            }
            return users;
    }

}

Please help me,only you can say me how can i do.
Have i to make static userList method?
Eclipse give me error if i didn't do it...
Is it correct?
What have i do now is only create a jsp page?
Please reply me,i'm bit confused..
gbattine - 25 Jul 2006 11:21 GMT
thanks very much,
i've solved my problem, i need a bit time but your article is
fantastic....
I've an only question.
I've retrieved a table from my db and i've shown it in my application
and its like:

name lastname city

i want to add at the end of each row of values a commandlink delete.

can you help me for code of backing bean to delete the selected row?
Thanks very much for your availability.............


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.