Professional Documents
Culture Documents
Driver Names
Establish a Connection
A connection to the database is established using the
static method getConnection(databaseUrl) of the
DriverManager class.
The DriverManager class is class for managing JDBC
drivers.
The database URL takes the following shape
jdbc:subprotocol:subname.
If any problem occurs during accessing the database,
an SQLException is generated, else a Connection object
is returned which refers to a connection to a database.
Connection is actually an interface in java.sql package.
Connection
con=DriverManager.getConnection(databaseUrl);
Create Statement
The connection is used to send SQL statements to
the database.
Three interfaces are used for sending SQL
statements to databases
Statement and its two sub-interfaces,
PreparedStatement
and
Callable
Statement.
Three methods of the Connection object are used
to return objects of these three statements.
A Statement object is used to send a simple SQL
statement to the database with no parameters.
Statement stmt = con.createStatement();
Execute Query
Three methods are used
ResultSet executeQuery(String sqlQuery) throws SQLException
int executeUpdate(String sqlQuery) throws SQLException
boolean execute(String sqlQuery) throws SQLException
Closing Connection
To terminate the connection with the
database, close() method is used.
connection.close();