Database Reference
In-Depth Information
[client]
user=paul
[client]
host=127.0.0.1
[mysql]
no-auto-rehash
Standard MySQL option parsing considers both the
user
and
host
values part of the
[client]
group, whereas
parse_ini_file()
returns only the contents of the final
[client]
stanza; the
user
option is lost. Also,
parse_ini_file()
ignores options that
are given without a value, so the
no-auto-rehash
option is lost.
Java.
The JDBC MySQL Connector/J driver doesn't support option files. However, the
Java class library supports reading properties files that contain lines in
name=value
format. This is similar but not identical to MySQL option-file format (for example,
properties files do not permit
[
groupname
]
lines). Here is a simple properties file:
# this file lists parameters for connecting to the MySQL server
user=cbuser
password=cbpass
host=localhost
The following program,
ReadPropsFile.java
, shows one way to read a properties file
named
Cookbook.properties
to obtain connection parameters. The file must be in some
directory named in your
CLASSPATH
variable, or you must specify it using a full path‐
name (the example shown here assumes that the file is in a
CLASSPATH
directory):
import
java.sql.*
;
import
java.util.*
;
// need this for properties file support
public
class
ReadPropsFile
{
public
static
void
main
(
String
[]
args
)
{
Connection
conn
=
null
;
String
url
=
null
;
String
propsFile
=
"Cookbook.properties"
;
Properties
props
=
new
Properties
();
try
{
props
.
load
(
ReadPropsFile
.
class
.
getResourceAsStream
(
propsFile
));
}
catch
(
Exception
e
)
{
System
.
err
.
println
(
"Cannot read properties file"
);
System
.
exit
(
1
);
}
try