Can someone help me with my first datatable?

G

gbattine

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....
 
B

balusc

gbattine said:
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.
 
G

gbattine

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..
 
G

gbattine

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.............
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top