Can someone help me with my first datatable?

Discussion in 'Java' started by gbattine, Jul 24, 2006.

  1. gbattine

    gbattine Guest

    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....
    gbattine, Jul 24, 2006
    #1
    1. Advertising

  2. gbattine

    Guest

    gbattine wrote:
    > Not everythings are for me clear...
    > I've to show in a datatable a mysql table
    > 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?



    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.
    , Jul 24, 2006
    #2
    1. Advertising

  3. gbattine

    gbattine Guest

    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, Jul 25, 2006
    #3
  4. gbattine

    gbattine Guest

    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.............
    gbattine, Jul 25, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J. Babe
    Replies:
    1
    Views:
    8,316
    Kevin Spencer
    Aug 15, 2003
  2. Fredrik Rodin

    Sort a datatable and create a new datatable

    Fredrik Rodin, Feb 16, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    9,028
    paul.mariotti
    Sep 28, 2007
  3. =?Utf-8?B?Qmx1ZWZsb3dlcg==?=

    Casting DataTable to class inherit from DataTable

    =?Utf-8?B?Qmx1ZWZsb3dlcg==?=, Apr 2, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    987
    =?Utf-8?B?Qmx1ZWZsb3dlcg==?=
    Apr 2, 2004
  4. jg
    Replies:
    1
    Views:
    713
    =?Utf-8?B?U2hhaWxlc2ggSmFubnU=?=
    Aug 17, 2004
  5. Ersin Gençtürk
    Replies:
    1
    Views:
    3,466
    Ersin Gençtürk
    Oct 6, 2004
Loading...

Share This Page