1<?xml version="1.0" encoding="UTF-8" standalone="no"?> 2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3<html xmlns="http://www.w3.org/1999/xhtml"> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 6 <title>Administrative Methods</title> 7 <link rel="stylesheet" href="gettingStarted.css" type="text/css" /> 8 <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" /> 9 <link rel="start" href="index.html" title="Getting Started with Berkeley DB" /> 10 <link rel="up" href="DB.html" title="Chapter��7.��Databases" /> 11 <link rel="prev" href="DBConfig.html" title="Database Properties" /> 12 <link rel="next" href="dbErrorReporting.html" title="Error Reporting Functions" /> 13 </head> 14 <body> 15 <div class="navheader"> 16 <table width="100%" summary="Navigation header"> 17 <tr> 18 <th colspan="3" align="center">Administrative Methods</th> 19 </tr> 20 <tr> 21 <td width="20%" align="left"><a accesskey="p" href="DBConfig.html">Prev</a>��</td> 22 <th width="60%" align="center">Chapter��7.��Databases</th> 23 <td width="20%" align="right">��<a accesskey="n" href="dbErrorReporting.html">Next</a></td> 24 </tr> 25 </table> 26 <hr /> 27 </div> 28 <div class="sect1" lang="en" xml:lang="en"> 29 <div class="titlepage"> 30 <div> 31 <div> 32 <h2 class="title" style="clear: both"><a id="DBAdmin"></a>Administrative Methods</h2> 33 </div> 34 </div> 35 </div> 36 <p> 37 Both the <code class="classname">Environment</code> and 38 <code class="classname">Database</code> classes provide methods that are useful 39 for manipulating databases. These methods are: 40 </p> 41 <div class="itemizedlist"> 42 <ul type="disc"> 43 <li> 44 <p> 45 <code class="methodname">Database.getDatabaseName()</code> 46 </p> 47 <p>Returns the database's name.</p> 48 <a id="je_db3.1"></a> 49 <pre class="programlisting">String dbName = myDatabase.getDatabaseName();</pre> 50 </li> 51 <li> 52 <p> 53 <code class="methodname">Database.rename()</code> 54 </p> 55 <p> 56 Renames the specified database. If no value is given for the 57 <em class="parameter"><code>database</code></em> parameter, then the entire file 58 referenced by this method is renamed. 59 </p> 60 <p> 61 Never rename a database that has handles opened for it. Never rename a file that 62 contains databases with opened handles. 63 </p> 64 <a id="java_db9"></a> 65 <pre class="programlisting">import java.io.FileNotFoundException; 66... 67myDatabase.close(); 68try { 69 myDatabase.rename("mydb.db", // Database file to rename 70 null, // Database to rename. Not used so 71 // the entire file is renamed. 72 "newdb.db", // New name to use. 73 null); // DatabaseConfig object. 74 // None provided. 75} catch (FileNotFoundException fnfe) { 76 // Exception handling goes here 77}</pre> 78 </li> 79 <li> 80 <p> 81 <code class="methodname">Environment.truncateDatabase()</code> 82 </p> 83 <p> 84 Deletes every record in the database and optionally returns the 85 number of records that were deleted. Note that it is much less 86 expensive to truncate a database without counting the number of 87 records deleted than it is to truncate and count. 88 </p> 89 <a id="je_db5"></a> 90 <pre class="programlisting">int numDiscarded = 91 myEnv.truncate(null, // txn handle 92 myDatabase.getDatabaseName(), // database name 93 true); // If true, then the 94 // number of records 95 // deleted are counted. 96System.out.println("Discarded " + numDiscarded + 97 " records from database " + 98 myDatabase.getDatabaseName()); </pre> 99 </li> 100 </ul> 101 </div> 102 </div> 103 <div class="navfooter"> 104 <hr /> 105 <table width="100%" summary="Navigation footer"> 106 <tr> 107 <td width="40%" align="left"><a accesskey="p" href="DBConfig.html">Prev</a>��</td> 108 <td width="20%" align="center"> 109 <a accesskey="u" href="DB.html">Up</a> 110 </td> 111 <td width="40%" align="right">��<a accesskey="n" href="dbErrorReporting.html">Next</a></td> 112 </tr> 113 <tr> 114 <td width="40%" align="left" valign="top">Database Properties��</td> 115 <td width="20%" align="center"> 116 <a accesskey="h" href="index.html">Home</a> 117 </td> 118 <td width="40%" align="right" valign="top">��Error Reporting Functions</td> 119 </tr> 120 </table> 121 </div> 122 </body> 123</html> 124