In the web application configuration HSQL database

  Abstract: In the web application configuration HSQL database 

  </ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> Thunder read some forum database, which is used HSQL database.    (Please download my entire directory test) 

  First, download from http://hsqldb.sourceforge.net/ HSQL1.7.3, which will be copied to your hsqldb.jar web applications WEB-INF/lib directory.    Write a document lightningboard.properties attributes, data will be stored in your web application TestHsql / hsqldb / lb_db under. 

  <table Width="681" border="0"> <tr> <td width="392"> # Database — — 
  DB.DRIVER = org.hsqldb.jdbcDriver 
  DB.URL = jdbc: hsqldb: webapps / TestHsql / hsqldb / lb_db 
  DB.USER sa = 
  DB.PASSWORD = 
  DB.MAX_CONNECTIONS = 30 

  Second, write configuration files used to read the above configuration database 
  Package lightningboard; 

  Import java.util.Properties; 
  Import java.io.InputStream; 
  Import java.io.IOException; 
  / ** 
  * LightningBoard configuration, the configuration file: 
  * "/ Lightningboard.properties." 
  * @ Version 0.3.5 
  * @ Author Xiaobo Liu 
  * / 
  </ Td> <td width="279" valign="top"> </ td> </ tr> </ table> 
  (Public class Configuration 
  Private Properties properties; 
  Private final static Configuration cfg = new Configuration (); 

  Private Configuration () ( 
  Properties = new Properties (); 
  InputStream is = null; 
  Try ( 
  Is = getClass (). GetResourceAsStream ( "lightningboard.properties"); 
  Properties.load (is); 
  ) Catch (Exception exception) ( 
  System.out.println ( "Can't read the properties file."); 
  Finally () 
  Try ( 
  If (is! = Null) 
  Is.close (); 
  ) Catch (IOException exception) ( 
  / / Ignored 
  ) 
  ) 
  ) 

  Public static Configuration getInstance () ( 
  Return cfg; 
  ) 

  Public String getValue (String key) ( 
  Return properties.getProperty (key); 
  ) 
  ) 
  Third, the database connection management, from a simple connection pool access connections. 
  Package lightningboard.db; 

  Import java.util.Vector; 
  Import java.sql.DriverManager; 
  Import java.sql.Connection; 
  Import java.sql.SQLException; 
  Import lightningboard.Configuration; 

  / ** 
  * Database Connection Manager 
  * @ Version 0.3.5 
  * @ Author Xiaobo Liu 
  * / 
  (Public class DBConnectionManager 
  Private final static DBConnectionManager instance = new DBConnectionManager (); 
  Private DBConnectionPool pool; 

  / ** 
  * Use singleton pattern, only return one instance of DBConnectionManager. 
  * @ Return DBConnectionManager 
  * / 
  Public static DBConnectionManager getInstance () ( 
  Return instance; 
  ) 
  / ** 
  * Get a connection 
  * @ Return Connection 
  * @ Throws SQLException this method 
  * / 
  Public Connection getConnection () (throws SQLException 
  Return pool.getConnection (); 
  ) 
  / ** 
  * Free a connection 
  * @ Param con connection 
  * @ Throws SQLException this method 
  * / Public void freeConnection (Connection con) throws SQLException ( 
  Pool.freeConnection (con); 

  ) 
  Private DBConnectionManager () ( 
  Init (); 
  ) 
  Private void init () ( 
  Configuration cfg = Configuration.getInstance (); 
  String db_driver = cfg.getValue ( "DB.DRIVER"); 
  String db_url = cfg.getValue ( "DB.URL"); 
  String db_user = cfg.getValue ( "DB.USER"); 
  String db_password = cfg.getValue ( "DB.PASSWORD"); 
  Int db_maxConn = Integer.parseInt (cfg.getValue ( "DB.MAX_CONNECTIONS")); 
  Try ( 
  Class.forName (db_driver); 
  ) 
  Catch (ClassNotFoundException ex) ( 
  System.out.println (ex); 
  ) 
  Pool = new DBConnectionPool (db_url, db_user, db_password, db_maxConn); 
  ) 

  / / Inner class 


  (Class DBConnectionPool 

  Private Vector freeConnections = new Vector (); 
  Private int maxConn; 
  Private int connNumb; 

  Private String URL; 
  Private String password; 
  Private String user; 

  Public DBConnectionPool (String URL, String user, String password, int maxConn) ( 
  This.URL = URL; 
  This.user = user; 
  This.password = password; 
  This.maxConn = maxConn; 
  ) 

  Public synchronized void freeConnection (Connection con) ( 
  FreeConnections.addElement (con); 
  ConnNumb -; 
  NotifyAll (); 
  ) 

  Public synchronized Connection getConnection () (throws SQLException 
  Connection con = null; 
  If (freeConnections.size ()> 0) ( 
  Con = (Connection) freeConnections.firstElement (); 
  FreeConnections.removeElementAt (0); 
  Try ( 
  If (con.isClosed ()) ( 
  Con = getConnection (); 
  ) 
  ) 
  Catch (SQLException e) ( 
  Con = getConnection (); 
  ) 
  ) 
  Else if (maxConn == 0 | | connNumb <maxConn) ( 
  Con = newConnection (); 
  ) 
  If (con! = Null) ( 
  ConnNumb + +; 
  ) 
  Return con; 
  ) 

  Private Connection newConnection () (throws SQLException 
  Connection con = DriverManager.getConnection (URL, user, password); 
  Return con; 
  ) 
  ) 

  ) 

  4, the following is the test jsp documents: Testhsql.jsp, Testhsql1.jsp (download) 

  <% @ Page contentType = "text / html; GBK charset ="%> 
  <% @ Page language = "java" import = "java.sql .*"%> 
  <% @ Page import = "lightningboard.db.DBConnectionManager"%> 

<%
  DBConnectionManager dbcm = DBConnectionManager.getInstance (); 
  Connection conn = null; 
  PreparedStatement ps = null; 
  ResultSet rs = null; 
  String sql = null; 
  Try ( 
  Conn = dbcm.getConnection (); 
  / / If there Table user to delete 
  Sql = "DROP TABLE IF EXISTS user"; 
  Ps = conn.prepareStatement (sql); 
  Ps.executeUpdate (); 
  / / Create Table 
  Sql = "CREATE TABLE USER (ID INTEGER, NAME VARCHAR (50), MOBILE CHAR (12))"; 
  Ps = conn.prepareStatement (sql); 
  Ps.executeUpdate (); 
  / / Insert data 
  Sql = "INSERT INTO user VALUES (?,?,?)"; 
  Ps = conn.prepareStatement (sql); 
  Ps.setInt (1, 1); 
  Ps.setString (2, "San"); 
  Ps.setString (3, "13912345678"); 
  Ps.executeUpdate (); 
  / / Data enquiries 
  Ps = conn.prepareStatement ( "SELECT * FROM user"); 
  Rs = ps.executeQuery (); 
  While (rs.next ()) ( 
  Out.println ( "ID:" + rs.getInt ( "id") + " 
");
  Out.println ( "Name:" + rs.getString ( "name") + " 
");
  Out.println ( "mobile phone number:" + rs.getString ( "mobile")); 
  ) 
  ) 
  Finally ( 
  If (rs! = Null) 
  Rs.close (); 
  If (ps! = Null) 
  Ps.close (); 
  If (conn! = Null) 
  Conn.close (); 
  ) 

%>

  OK!! 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