Java Reference
In-Depth Information
See Also
Performance will suffer if a program repeatedly opens and closes JDBC connections, be-
cause getting a Connection object involves “logging in” to the database. One solution is to
use a connection pool : the pool preallocates a certain number of Connection objects, hands
them out on demand, and the application code returns its connection to the pool when done.
Writing a simple connection pool is easy, but writing a connection pool reliable enough to be
used in production is very hard. For this reason, JDBC 2 introduced the notion of having the
driver provide connection pooling. However, this feature is optional—check your driver's
documentation. Also, if you are running in a Java EE application server, the server will
provide connection pooling; for example, if a servlet is using EJBs and the servlet engine
runs in the same “application server” process, this can be a very efficient solution. See Enter-
prise JavaBeans (O'Reilly).
Sending a JDBC Query and Getting Results
Problem
You want to query a database using JDBC and get results back.
Solution
Having done the setup steps in Sending a JDBC Query and Getting Results , you can get a
Statement and use it to execute a query. You'll get a set of results, a ResultSet object.
Discussion
The Connection object can generate various kinds of statements. The simplest is a State-
ment created by createStatement() , which is used to send your SQL query as an arbitrary
string:
Statement stmt = conn.createStatement( );
stmt.executeQuery("select * from myTable");
Search WWH ::




Custom Search