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.62.4" /> 9 <link rel="home" href="index.html" title="Getting Started with Berkeley DB" /> 10 <link rel="up" href="DB.html" title="Chapter��7.��Databases" /> 11 <link rel="previous" 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></div> 36 </div> 37 <p> 38 Both the <tt class="classname">Environment</tt> and 39 <tt class="classname">Database</tt> classes provide methods that are useful 40 for manipulating databases. These methods are: 41 </p> 42 <div class="itemizedlist"> 43 <ul type="disc"> 44 <li> 45 <p> 46 <tt class="methodname">Database.getDatabaseName()</tt> 47 </p> 48 <p>Returns the database's name.</p> 49 <a id="je_db3.1"></a> 50 <pre class="programlisting">String dbName = myDatabase.getDatabaseName();</pre> 51 </li> 52 <li> 53 <p> 54 <tt class="methodname">Database.rename()</tt> 55 </p> 56 <p> 57 Renames the specified database. If no value is given for the 58 <i class="parameter"><tt>database</tt></i> parameter, then the entire file 59 referenced by this method is renamed. 60 </p> 61 <p> 62 Never rename a database that has handles opened for it. Never rename a file that 63 contains databases with opened handles. 64 </p> 65 <a id="java_db9"></a> 66 <pre class="programlisting">import java.io.FileNotFoundException; 67... 68myDatabase.close(); 69try { 70 myDatabase.rename("mydb.db", // Database file to rename 71 null, // Database to rename. Not used so 72 // the entire file is renamed. 73 "newdb.db", // New name to use. 74 null); // DatabaseConfig object. 75 // None provided. 76} catch (FileNotFoundException fnfe) { 77 // Exception handling goes here 78}</pre> 79 </li> 80 <li> 81 <p> 82 <tt class="methodname">Environment.truncateDatabase()</tt> 83 </p> 84 <p> 85 Deletes every record in the database and optionally returns the 86 number of records that were deleted. Note that it is much less 87 expensive to truncate a database without counting the number of 88 records deleted than it is to truncate and count. 89 </p> 90 <a id="je_db5"></a> 91 <pre class="programlisting">int numDiscarded = 92 myEnv.truncate(null, // txn handle 93 myDatabase.getDatabaseName(), // database name 94 true); // If true, then the 95 // number of records 96 // deleted are counted. 97System.out.println("Discarded " + numDiscarded + 98 " records from database " + 99 myDatabase.getDatabaseName()); </pre> 100 </li> 101 </ul> 102 </div> 103 </div> 104 <div class="navfooter"> 105 <hr /> 106 <table width="100%" summary="Navigation footer"> 107 <tr> 108 <td width="40%" align="left"><a accesskey="p" href="DBConfig.html">Prev</a>��</td> 109 <td width="20%" align="center"> 110 <a accesskey="u" href="DB.html">Up</a> 111 </td> 112 <td width="40%" align="right">��<a accesskey="n" href="dbErrorReporting.html">Next</a></td> 113 </tr> 114 <tr> 115 <td width="40%" align="left" valign="top">Database Properties��</td> 116 <td width="20%" align="center"> 117 <a accesskey="h" href="index.html">Home</a> 118 </td> 119 <td width="40%" align="right" valign="top">��Error Reporting Functions</td> 120 </tr> 121 </table> 122 </div> 123 </body> 124</html> 125