Oracle 5.0 Reference Manual page 2108

Table of Contents

Advertisement

} catch (Exception ex) {
}
}
}
After the driver has been registered with the DriverManager, you can obtain a
that is connected to a particular database by calling DriverManager.getConnection():
Example 20.1. Connector/J: Obtaining a connection from the
If you have not already done so, please review the section
Using the JDBC
This example shows how you can obtain a
are a few different signatures for the
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?" +
// 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
objects, as well as retrieve metadata about the database. This is explained in the following sections.
20.3.6.2. Using JDBC
Statement
ResultSet
To create a
Connection
DataSource.getConnection()
Once you have a
executeQuery(String)
To update data in the database, use the
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
then you can use the
was a SELECT, or false if it was an UPDATE, INSERT, or
query, you can retrieve the results by calling the
SELECT
was an UPDATE, INSERT, or
getUpdateCount()
Class.forName("com.mysql.jdbc.Driver").newInstance();
// handle the error
DriverManager
is established, it can be used to create
Objects to Execute SQL
Statement
objects allow you to execute basic SQL queries and retrieve the results through the
class, which is described later.
instance, you call the
Statement
object you have retrieved using one of the
instance, you can execute a
Statement
method with the SQL you want to use.
execute(String SQL)
DELETE
on the
Statement
JDBC Concepts
Interface"
before working with these examples.
instance from the DriverManager. There
Connection
getConnection()
"user=monty&password=greatsqldb");
createStatement()
DriverManager.getConnection()
methods described earlier.
executeUpdate(String SQL)
method. This method will return true if the SQL query
DELETE
getResultSet()
statement, you can retrieve the affected rows count by calling
instance.
2088
DriverManager
Section 20.3.6.1, "Connecting to MySQL
method. Consult the API documentation that
and
Statement
PreparedStatement
method on the
query by calling the
SELECT
method. This method
or an UPDATE/INSERT,
SELECT
statement. If the statement was a
method. If the statement
instance
Connection
or

Advertisement

Table of Contents
loading

This manual is also suitable for:

Mysql 5.0

Table of Contents