Introduction to Java Database Connectivity JDBC
Introduction to Java Database Connectivity JDBC
• To find a driver for the database you want to use, simply do a web search for your database and JDBC.
• For instance, typing in "mysql jdbc driver" will turn up a driver for MySQL
The steps for connecting to a
database with JDBC
In general, to process any SQL statement with JDBC, you follow these
steps:
1-Establishing a connection.
2-Create a statement.
3-Execute the query.
4-Process the ResultSet object.
5-Close the connection.
1-Establishing a connection
2-Create a statement.
2-Create a statement. Cont
• To execute a query, call an execute method from Statement such as the following:
• execute: Returns true if the first object that the query returns is a ResultSet object. Use this method if
the query could return one or more ResultSet objects. Retrieve the ResultSet objects returned from
the query by repeatedly calling Statement.getResultSet.
• executeQuery: Returns one ResultSet object.
• executeUpdate: Returns an integer representing the number of rows affected by the SQL statement.
Use this method if you are using INSERT, DELETE, or UPDATE SQL statements.
• For example,
• CoffeesTables.viewTable executed a Statement object with the following code:
• ResultSet rs = stmt.executeQuery(query);
4-Process the ResultSet object.
5-Close the connection.
When you are finished using a Connection, Statement, or ResultSet object, call its close
method to immediately release the resources it's using.