JDBC Concepts
2088
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception ex) {
// handle the error
}
}
}
After the driver has been registered with the
DriverManager
, you can obtain a
Connection
instance
that is connected to a particular database by calling
DriverManager.getConnection()
:
Example 20.1. Connector/J: Obtaining a connection from the
DriverManager
If you have not already done so, please review the section
Section 20.3.6.1, “Connecting to MySQL
Using the JDBC
DriverManager
Interface”
before working with these examples.
This example shows how you can obtain a
Connection
instance from the
DriverManager
. There
are a few different signatures for the
getConnection()
method. Consult the API documentation that
comes with your JDK for more specific information on how to use them.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
Connection conn = null;
...
try {
conn =
DriverManager.getConnection("jdbc:mysql://localhost/test?" +
"user=monty&password=greatsqldb");
// Do something with the Connection
...
} catch (SQLException ex) {
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
Once a
Connection
is established, it can be used to create
Statement
and
PreparedStatement
objects, as well as retrieve metadata about the database. This is explained in the following sections.
20.3.6.2. Using JDBC
Statement
Objects to Execute SQL
Statement
objects allow you to execute basic SQL queries and retrieve the results through the
ResultSet
class, which is described later.
To create a
Statement
instance, you call the
createStatement()
method on the
Connection
object you have retrieved using one of the
DriverManager.getConnection()
or
DataSource.getConnection()
methods described earlier.
Once you have a
Statement
instance, you can execute a
SELECT
query by calling the
executeQuery(String)
method with the SQL you want to use.
To update data in the database, use the
executeUpdate(String SQL)
method. This method
returns the number of rows matched by the update statement, not the number of rows that were
modified.
If you do not know ahead of time whether the SQL statement will be a
SELECT
or an
UPDATE
/
INSERT
,
then you can use the
execute(String SQL)
method. This method will return true if the SQL query
was a
SELECT
, or false if it was an
UPDATE
,
INSERT
, or
DELETE
statement. If the statement was a
SELECT
query, you can retrieve the results by calling the
getResultSet()
method. If the statement
was an
UPDATE
,
INSERT
, or
DELETE
statement, you can retrieve the affected rows count by calling
getUpdateCount()
on the
Statement
instance.
Summary of Contents for 5.0
Page 1: ...MySQL 5 0 Reference Manual ...
Page 18: ...xviii ...
Page 60: ...40 ...
Page 396: ...376 ...
Page 578: ...558 ...
Page 636: ...616 ...
Page 844: ...824 ...
Page 1234: ...1214 ...
Page 1427: ...MySQL Proxy Scripting 1407 ...
Page 1734: ...1714 ...
Page 1752: ...1732 ...
Page 1783: ...Configuring Connector ODBC 1763 ...
Page 1793: ...Connector ODBC Examples 1773 ...
Page 1839: ...Connector Net Installation 1819 2 You must choose the type of installation to perform ...
Page 2850: ...2830 ...
Page 2854: ...2834 ...
Page 2928: ...2908 ...
Page 3000: ...2980 ...
Page 3122: ...3102 ...
Page 3126: ...3106 ...
Page 3174: ...3154 ...
Page 3232: ...3212 ...