Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to show MySQL data by a managed bean?
    primarykey
    data
    text
    <p>I am trying to manage to work a simply xhtml page with Java Server Faces 2.0 that shows entries from a database JDBC-MySQL For the moment I simply get a blank page (no data) The <code>main</code> method in <code>Database.java</code> works well, so the SQL query works.</p> <p>Now I am basing my programming onto <a href="http://horstmann.com/corejsf/" rel="nofollow">http://horstmann.com/corejsf/</a> examples.</p> <p>I am using Eclipse with web development plugin, Tomcat 7 and MySQL 5.5.14</p> <p><b>index.xhtml</b></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"&gt; &lt;h:head&gt; &lt;title&gt;pageTitle&lt;/title&gt; &lt;/h:head&gt; &lt;h:body&gt; &lt;h:form&gt; &lt;h:dataTable value="#{database.all}" var="db"&gt; &lt;h:column&gt; #{db.OriginalToken} &lt;/h:column&gt; &lt;h:column&gt; #{db.Probability} &lt;/h:column&gt; &lt;h:column&gt; #{db.StandardToken} &lt;/h:column&gt; &lt;h:column&gt; #{db.Lemma} &lt;/h:column&gt; &lt;h:column&gt; #{db.Notes} &lt;/h:column&gt; &lt;/h:dataTable&gt; &lt;/h:form&gt; &lt;/h:body&gt; &lt;/html&gt; </code></pre> <p><b>faces-config.xml</b></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;faces-config xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd" version="2.0"&gt; &lt;managed-bean&gt; &lt;managed-bean-name&gt;database&lt;/managed-bean-name&gt; &lt;managed-bean-class&gt;classes.Database&lt;/managed-bean-class&gt; &lt;managed-bean-scope&gt;request&lt;/managed-bean-scope&gt; &lt;/managed-bean&gt; &lt;/faces-config&gt; </code></pre> <p><b>web.xml</b></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"&gt; &lt;display-name&gt;cancTestDatabaseExNovo&lt;/display-name&gt; &lt;servlet&gt; &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt; &lt;servlet-class&gt;javax.faces.webapp.FacesServlet&lt;/servlet-class&gt; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt; &lt;url-pattern&gt;/faces/*&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;context-param&gt; &lt;param-name&gt;javax.servlet.jsp.jstl.fmt.localizationContext&lt;/param-name&gt; &lt;param-value&gt;resources.application&lt;/param-value&gt; &lt;/context-param&gt; &lt;context-param&gt; &lt;description&gt;State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2&lt;/description&gt; &lt;param-name&gt;javax.faces.STATE_SAVING_METHOD&lt;/param-name&gt; &lt;param-value&gt;client&lt;/param-value&gt; &lt;/context-param&gt; &lt;context-param&gt; &lt;description&gt; This parameter tells MyFaces if javascript code should be allowed in the rendered HTML output. If javascript is allowed, command_link anchors will have javascript code that submits the corresponding form. If javascript is not allowed, the state saving info and nested parameters will be added as url parameters. Default is 'true'&lt;/description&gt; &lt;param-name&gt;org.apache.myfaces.ALLOW_JAVASCRIPT&lt;/param-name&gt; &lt;param-value&gt;true&lt;/param-value&gt; &lt;/context-param&gt; &lt;context-param&gt; &lt;description&gt; If true, rendered HTML code will be formatted, so that it is 'human-readable' i.e. additional line separators and whitespace will be written, that do not influence the HTML code. Default is 'true'&lt;/description&gt; &lt;param-name&gt;org.apache.myfaces.PRETTY_HTML&lt;/param-name&gt; &lt;param-value&gt;true&lt;/param-value&gt; &lt;/context-param&gt; &lt;context-param&gt; &lt;param-name&gt;org.apache.myfaces.DETECT_JAVASCRIPT&lt;/param-name&gt; &lt;param-value&gt;false&lt;/param-value&gt; &lt;/context-param&gt; &lt;context-param&gt; &lt;description&gt; If true, a javascript function will be rendered that is able to restore the former vertical scroll on every request. Convenient feature if you have pages with long lists and you do not want the browser page to always jump to the top if you trigger a link or button action that stays on the same page. Default is 'false' &lt;/description&gt; &lt;param-name&gt;org.apache.myfaces.AUTO_SCROLL&lt;/param-name&gt; &lt;param-value&gt;true&lt;/param-value&gt; &lt;/context-param&gt; &lt;listener&gt; &lt;listener-class&gt;org.apache.myfaces.webapp.StartupServletContextListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;/web-app&gt; </code></pre> <p><b>Database.java</b></p> <pre><code>package classes; import javax.faces.bean.ManagedBean; import javax.faces.bean.RequestScoped; import java.io.FileInputStream; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; import javax.sql.DataSource; import javax.sql.rowset.CachedRowSet; import javax.annotation.Resource; @ManagedBean @RequestScoped public class Database { private static Statement statement; private static Connection connection; private ResultSet result; public static void main(String[] args) { Database database = new Database(); try { ResultSet result = database.getAll(); while(result.next()) { System.out.println(result.getString(1)); } /*System.out.println("\n"); ResultSet result2 = database.getAll(); while(result2.next()) { System.out.println(result2.getString(1)); }*/ } catch (SQLException e) { e.printStackTrace(); System.out.println("SQLException"); } catch(IOException e) { e.printStackTrace(); System.out.println("IOException"); } catch (InstantiationException e) { e.printStackTrace(); System.out.println("InstantiationException"); } catch (IllegalAccessException e) { e.printStackTrace(); System.out.println("IllegalAccessException"); } catch (ClassNotFoundException e) { e.printStackTrace(); System.out.println("class NOT FOUND"); } } public ResultSet getAll() throws SQLException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException { try { connection = getConnection(); statement = connection.createStatement(); Statement stmt = connection.createStatement(); ResultSet result = stmt.executeQuery("SELECT * FROM LAIDict"); CachedRowSet crs = new com.sun.rowset.CachedRowSetImpl(); crs.populate(result); return crs; } finally { connection.close(); } } /*public String[] getAll() throws SQLException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException { String[] string = {"ciao", "come", "stai"}; return string; }*/ public void setAll() { } /** Gets a connection from the properties specified in the file database.properties @return the database connection * @throws ClassNotFoundException * @throws IllegalAccessException * @throws InstantiationException */ public static Connection getConnection() throws SQLException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException { Properties props = new Properties(); FileInputStream in = new FileInputStream("/home/caterpillar/workspace/cancTestDatabaseExNovo/database.properties"); props.load(in); in.close(); String drivers = props.getProperty("jdbc.drivers"); if (drivers != null) System.setProperty("jdbc.drivers", drivers); String url = props.getProperty("jdbc.url"); String username = props.getProperty("jdbc.username"); String password = props.getProperty("jdbc.password"); Class.forName(drivers).newInstance(); return DriverManager.getConnection(url, username, password); } private PreparedStatement insertStatement; private PreparedStatement rispostaStatement; private static final String insertString = "INSERT INTO (?) VALUES (?, ?)"; private static final String insertStringV2 = "INSERT INTO (?) VALUES (?)"; private static final String risposta = "SELECT * FROM LAIDict"; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload