[jboss-user] [Persistence, JBoss/CMP, Hibernate, Database] - edit data in dataTable

croco do-not-reply at jboss.com
Wed Feb 18 10:25:30 EST 2009


Hello. I'm new at JSF and this thing and I have a problem. I have a dataTable which in filled with data (userid, first name, last name and email) read from the database and on the userid is a commandLink that opens (with ajax) inputFiels to edit this user (first last name and email). Now the problem I have is that i don't know how to send to the bean this userid, so the bean would know in which row of the database fit the new data.

Here is the code:

jsp file:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
  | <%@ taglib prefix="f"  uri="http://java.sun.com/jsf/core"%>
  | <%@ taglib prefix="h"  uri="http://java.sun.com/jsf/html"%>
  | <%@ taglib uri="https://ajax4jsf.dev.java.net/ajax" prefix="a4j"%>
  | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  | <html>
  | <head>
  | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  | <title>Database handle</title>
  | </head>
  | <body>
  | <f:view>
  | <center>
  | <table border="5">
  | <tr><th>Database Handle</th></tr>
  | </table>
  | <p />
  | 
  | <h:dataTable value="#{databaseBean.linkedList}" var="linkedList" border="3">
  | 
  | <h:column>
  | <f:facet name="header">
  | <h:outputText value="UserID" />
  | </f:facet>
  | <h:form>
  | <a4j:commandLink action="#{databaseBean.enableEdit}" reRender="enableEdit" >
  | <h:outputText value="#{linkedList.userID}" />
  | </a4j:commandLink>
  | </h:form>
  | </h:column>
  | 
  | <h:column>
  | <f:facet name="header">
  | <h:outputText value="First Name" />
  | </f:facet>
  | <h:outputText value="#{linkedList.firstName}" />
  | </h:column>
  | 
  | <h:column>
  | <f:facet name="header">
  | <h:outputText value="Last Name" />
  | </f:facet>
  | <h:outputText value="#{linkedList.lastName}" />
  | </h:column>
  | 
  | <h:column>
  | <f:facet name="header">
  | <h:outputText value="Email" />
  | </f:facet>
  | <h:outputText value="#{linkedList.email}" />
  | </h:column>
  | 
  | </h:dataTable>
  | <p />
  | <h:form>
  | <h:panelGrid id="enableEdit" columns="2" >
  | <h:outputText value="Insert new first name: " rendered="#{not empty databaseBean.editData}" />
  | <h:inputText value="#{databaseBean.newFirstName}" rendered="#{not empty databaseBean.editData}" />
  | <h:outputText value="Insert new last name: " rendered="#{not empty databaseBean.editData}" />
  | <h:inputText value="#{databaseBean.newLastName}" rendered="#{not empty databaseBean.editData}" />
  | <h:outputText value="Insert new email: " rendered="#{not empty databaseBean.editData}" />
  | <h:inputText value="#{databaseBean.newEmail}" rendered="#{not empty databaseBean.editData}" />
  | <a4j:commandButton action="#{databaseBean.fireAjax}" value="Send new data" rendered="#{not empty databaseBean.editData}" />
  | </h:panelGrid>
  | </h:form>
  | 
  | </center>
  | </f:view>
  | </body>
  | </html>

Bean:
package ...;
  | import java.sql.*;
  | import java.util.LinkedList;
  | 
  | public class DatabaseBean {
  | 	private LinkedList<User> linkedList;
  | 	private String editData;
  | 	private String newFirstName;
  | 	private String newLastName;
  | 	private String newEmail;
  | 		
  | 	public DatabaseBean() {
  | 		try {
  | 			Class.forName("com.mysql.jdbc.Driver").newInstance();
  | 			String host = "...";
  | 			String database = "...";
  | 			String username = "...";
  | 			String password = "...";
  | 			String prefix = "jdbc:mysql://";
  | 			String URL = prefix + host + "/" + database;
  | 			Connection connection = DriverManager.getConnection(URL, username, password);
  | 			Statement statement = connection.createStatement();
  | 			String query = "SELECT * FROM Podatki";
  | 			ResultSet resultSet = statement.executeQuery(query);
  | 			handleData(resultSet);
  | 			connection.close();
  | 			editData = "";
  | 			newFirstName = "";
  | 			newLastName = "";
  | 			newEmail = "";
  | 		}
  | 		catch (ClassNotFoundException cnfe) {
  | 			System.out.println("Error loading driver: " + cnfe);
  | 		} catch (InstantiationException ie) {
  | 			System.out.println("Instantiation error: " + ie);
  | 		} catch (IllegalAccessException iae) {
  | 			System.out.println("Illegal access: " + iae);
  | 		} catch (SQLException sqle) {
  | 			System.out.println("SQL exception: " + sqle);
  | 		}
  | 	}
  | 	
  | 	private void handleData(ResultSet rs) {
  | 		try {
  | 			linkedList = new LinkedList<User>();
  | 			while(rs.next()) {
  | 				linkedList.add(new User(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4)));
  | 			}
  | 		}
  | 		catch(SQLException sqle) {
  | 			System.out.println("Error in sql result fied: " + sqle);
  | 		}
  | 	}
  | 	
  | 	public LinkedList<User> getLinkedList() {
  | 		return this.linkedList;
  | 	}
  | 
  | 	public String getEditData() {
  | 		return editData;
  | 	}
  | 
  | 	public void setEditData(String editData) {
  | 		this.editData = editData;
  | 	}
  | 	
  | 	/*
  | 	 * Ajax
  | 	 */
  | 	public String enableEdit() {
  | 		this.editData = "enableEdit";
  | 		this.newFirstName = "...insert new name...";
  | 		this.newLastName = "...insert new last name...";
  | 		this.newEmail = "...insert new email...";
  | 		return null;
  | 	}
  | 
  | 	public String getNewFirstName() {
  | 		return newFirstName;
  | 	}
  | 
  | 	public void setNewFirstName(String newFirstName) {
  | 		this.newFirstName = newFirstName;
  | 	}
  | 
  | 	public String getNewLastName() {
  | 		return newLastName;
  | 	}
  | 
  | 	public void setNewLastName(String newLastName) {
  | 		this.newLastName = newLastName;
  | 	}
  | 
  | 	public String getNewEmail() {
  | 		return newEmail;
  | 	}
  | 
  | 	public void setNewEmail(String newEmail) {
  | 		this.newEmail = newEmail;
  | 	}
  | 	
  | 	/*
  | 	 * Ajax
  | 	 */
  | 	public String fireAjax() {
  | 		// here is supposed to be the code to set the new values in the db
  | 		System.out.println("hello!");
  | 		return null;
  | 	}
  | 	
  | }
  | 

View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4211130#4211130

Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4211130



More information about the jboss-user mailing list