Jsf bean in the constructor function in the Database Access

  Abstract: jsf bean in the constructor function in the Database Access 

  </ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width="750" border="0"> <tr> <td> </ td> </ tr> </ table> 
  JSP pages initialization, the JSF will be initialized counterparts in the bean, a bean Riga constructor function in the constructor function in the information from the database, so that JSP pages can be displayed on the data in the database. 



  Affixed to a section of the code you see: 
  Package com.jack; 
  Import java.sql .*; 
  Import java.sql.DriverManager; 
  / ** 
*
  * @ Author jack 
  * / 
  (Public class DBConnect 
  Private static Connection conn = null; 
  / ** Creates a new instance of Conn * / 
  Public DBConnect () 
  ( 
  OpenConnection (); 
  ) 

  Public void Close () 
  ( 
  Try 
  ( 
  Conn.close (); 
  ) 
  Catch (SQLException e) 
  ( 
  System.out.println (e); 
  ) 
  ) 

  Public static Connection getConnection () 
  ( 
  If (conn == null) 
  OpenConnection (); 
  Return conn; 
  ) 

  Public static void OpenConnection () 
  ( 
  If (conn == null) 
  ( 
  Try 
  ( 
  Java.lang.Class.forName ( "sun.jdbc.odbc.JdbcOdbcDriver"); 
  Conn = DriverManager.getConnection ( "jdbc: odbc: localServer", "sa ",""); 
  System.out.println ( "get connection success!"); 
  ) 
  Catch (SQLException e) 
  ( 
  System.out.println (e); 
  ) 
  Catch (ClassNotFoundException e) 
  ( 
  System.out.println ( "class not found!"); 
  ) 
  ) 
  ) 
  ) 
  Package com.jack; 
  Import java.sql.ResultSet; 
  Import java.sql.Connection; 
  Import java.sql.Statement; 
  Import java.sql.SQLException; 
  Import javax.servlet.jsp.jstl.sql.Result; 
  Import javax.servlet.jsp.jstl.sql.ResultSupport; 
  / ** 
  * @ Author jack 
*
  * TODO To change the template for this generated type comment go to 
  * Window - Preferences - Java - Code Style - Code Templates 
  * / 
  Public class CustomerBean 
  ( 
  Public Result getAll () throws SQLException 
  ( 
  System.out.println ( "begin get Connection"); 
  Connection conn = DBConnect.getConnection (); 
  System.out.println ( "end get Connection"); 
  Statement stmt = conn.createStatement (); 
  System.out.println ( "create Statement"); 
  ResultSet result = stmt.executeQuery ( "select JobNo, name, DepartmentNo, HomeAddress, Telphone, ClassName from EmployEmpTable"); 
  System.out.println ( "get Resultset"); 
  Return ResultSupport.toResult (result); 
  ) 
  ) 


  Index.jsp: 

  <% @ Taglib uri = "http://java.sun.com/jsf/core" prefix = "f"%> 
  <% @ Taglib uri = "http://java.sun.com/jsf/html" prefix = "h"%> 

<head>
  <link> Href = "styles.css" rel = "stylesheet" type = "text / css" /> 


  </ Head> 
<body>

  StyleClass = "customers" 
  HeaderClass = "customersHeader" columnClasses = "custid, the name"> 






































  </ Body> 


  Faces-config.xml file: 
  <? Xml version = "1.0"> 
  Faces-config PUBLIC 
  "- / / Sun Microsystems, Inc / / DTD JavaServer Faces Config 1.0 / / EN" 
  "Http://java.sun.com/dtd/web-facesconfig_1_0.dtd"> 


  Customer 
  Com.jack.CustomerBean 
  Session 


  Function TempSave (ElementID) (CommentsPersistDiv.setAttribute ( "CommentContent" document.getElementById (ElementID). Value); CommentsPersistDiv.save ( "CommentXMLStore");) function Restore (ElementID) (CommentsPersistDiv.load ( "CommentXMLStore"); document . getElementById (ElementID). CommentsPersistDiv.getAttribute value = ( "CommentContent");) </ td> </ tr> <tr> 

  ↑ Back 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags: , , ,

Releated Java Articles

Comments

Leave a Reply