Database Reference
In-Depth Information
}
System . out . println ();
}
rs . close (); // close result set
s . close (); // close statement
2.8. Techniques for Obtaining Connection Parameters
Problem
You need to obtain connection parameters for a script so that it can connect to a MySQL
server.
Solution
There are several ways to do this. Take your pick from the alternatives described here.
Discussion
Any program that connects to MySQL specifies connection parameters such as the
username, password, and hostname. The recipes shown so far have put connection
parameters directly into the code that attempts to establish the connection, but that is
not the only way for your programs to obtain the parameters. This discussion briefly
surveys some of the available techniques:
Hardwire the parameters into the program
The parameters can be given either in the main source file or in a library file used
by the program. This technique is convenient because users need not enter the
values themselves, but it's also inflexible. To change parameters, you must modify
your program.
Ask for the parameters interactively
In a command-line environment, you can ask the user a series of questions. In a
web or GUI environment, you might do this by presenting a form or dialog. Either
way, this becomes tedious for people who use the application frequently, due to the
need to enter the parameters each time.
Get the parameters from the command line
You can use this method either for commands run interactively or from within a
script. Like the method of obtaining parameters interactively, you must supply pa‐
rameters for each command invocation. (A factor that mitigates this burden is that
many shells enable you to easily recall commands from your history list for re-
execution.)
Search WWH ::




Custom Search