Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2012
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. package com.DX_57.SM_57;
  2. /* include default packages for Beans */
  3. import java.io.Serializable;
  4. import javax.enterprise.context.SessionScoped;
  5. // or import javax.faces.bean.SessionScoped;
  6. import javax.inject.Named;
  7. /* include SQL Packages */
  8. import java.sql.Connection;
  9. import java.sql.PreparedStatement;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. import java.util.HashMap;
  13. import javax.annotation.ManagedBean;
  14. import javax.annotation.PostConstruct;
  15. import javax.sql.DataSource;
  16. import javax.annotation.Resource;
  17. import javax.faces.bean.ViewScoped;
  18. import javax.faces.context.FacesContext;
  19. import javax.inject.Inject;
  20. import javax.servlet.http.HttpServletRequest;
  21. import javax.servlet.http.HttpSession;
  22. // or import javax.faces.bean.ManagedBean;
  23.  
  24. import org.glassfish.osgicdi.OSGiService;
  25.  
  26. @ManagedBean("ApplicationController")
  27. @ViewScoped
  28. public class Application implements Serializable {
  29.  
  30. /* This Hash Map will be used to store setting and value */
  31. private HashMap<String, String> settingsMap = null;
  32.  
  33. public Application(){
  34. }
  35.  
  36. /* Call the Oracle JDBC Connection driver */
  37. @Resource(name = "jdbc/Oracle")
  38. private DataSource ds;
  39.  
  40.  
  41. /* Hash Map
  42. * Send this hash map with the settings and values to the JSF page
  43. */
  44. public HashMap<String, String> getsettings(){
  45. return settingsMap;
  46. }
  47.  
  48. /* Get a Hash Map with settings and values. The table is genarated right
  49. * after the constructor is initialized.
  50. */
  51. @PostConstruct
  52. public void initSettings() throws SQLException
  53. {
  54. settingsMap = new HashMap<String, String>();
  55.  
  56. if(ds == null) {
  57. throw new SQLException("Can't get data source");
  58. }
  59. /* Initialize a connection to Oracle */
  60. Connection conn = ds.getConnection();
  61.  
  62. if(conn == null) {
  63. throw new SQLException("Can't get database connection");
  64. }
  65. /* With SQL statement get all settings and values */
  66. PreparedStatement ps = conn.prepareStatement("SELECT * from GLOBALSETTINGS");
  67.  
  68. try
  69. {
  70. //get data from database
  71. ResultSet result = ps.executeQuery();
  72. while (result.next())
  73. {
  74. /* Put the the data from Oracle into Hash Map */
  75. settingsMap.put(result.getString("SettingName"), result.getString("SettingValue"));
  76. }
  77. }
  78. finally
  79. {
  80. ps.close();
  81. conn.close();
  82. }
  83. }
  84.  
  85. /* JSF returns automatically the updated values into the HashMap */
  86.  
  87. /* Update Settings Values */
  88. public void updateDBSettings() throws SQLException {
  89.  
  90. String SQL_Statement = null;
  91.  
  92. if (ds == null) throw new SQLException();
  93. Connection conn = ds.getConnection();
  94. if (conn == null) throw new SQLException();
  95.  
  96. try {
  97. conn.setAutoCommit(false);
  98. boolean committed = false;
  99. try {
  100. /* Insert the new settings values with one SQL statement */
  101. SQL_Statement = "UPDATE GLOBALSETTINGS " +
  102. "SET \"SettingValue\" = " +
  103. "CASE " +
  104. "WHEN \"SettingName\" = 'SessionTTL' THEN ? " +
  105. "WHEN \"SettingName\" = 'MaxUsersActive' THEN ? " +
  106. "END " +
  107. "WHERE \"SettingName\" IN ('SessionTTL', 'MaxUsersActive')";
  108.  
  109. /* Execute the SQL statement */
  110. PreparedStatement updateQuery = conn.prepareStatement(SQL_Statement);
  111. updateQuery.setString(1, settingsMap.get("SessionTTL"));
  112. updateQuery.setString(2, settingsMap.get("MaxUsersActive"));
  113.  
  114. updateQuery.executeQuery();
  115. conn.commit();
  116. committed = true;
  117. } finally {
  118. if (!committed) conn.rollback();
  119. }
  120. }
  121. finally {
  122. /* Release the resource after all SQL queries are executed */
  123. conn.close();
  124. }
  125. /* Refresh Hash Map
  126. * Get again settings from Oracle
  127. */
  128. initSettings();
  129. }
  130.  
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement