This short tutorial provides an introduction to the JDBC interface, a driver for connecting Java programs with SQL-based databases, giving a sequence of steps to follow. In this course, we will work with an Access database.
Important: if you do not see Microsoft Access Driver (*.mdb, *accdb) in the list, go to the following folder C:\Windows\SysWOW64\odbcad.exe.
Step 1
Step 2
In the Java file:
import java.sql.*;
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
Connection connection = null;
String url="jdbc:odbc:northbrick";
connection = DriverManager.getConnection(url);
Statement stmt;
stmt = connection.createStatement();
The Statement interface defines 2 methods for executing SQL queries:
1) public ResultSet executeQuery(String sql) returns a ResultSet that includes all rows and columns which match the SELECT query
ResultSet rs;
String query = "SELECT * FROM Authors";
// We recommend you to print the query before executing it
System.out.println("SQL: " + query);
rs = stmt.executeQuery( query );
2) public int executeUpdate(String sql) executeUpdate returns the number of rows changed by the update statement. This is used for insert statements, update statements and delete statements
String deleteStr = “DELETE FROM Authors WHERE LastName=‘Follet’";
int delnum = stmt.executeUpdate(deleteStr);
Remember you have to use the next() function to point the cursor to the first row.
while (rs.next()) {
String s = rs.getString(“LastName”);
int y = rs.getInt(“YearBorn”);
System.out.println(s+” “+y);
}
rs.close();
stmt.close();
Close the connection when you no longer need to access the database. The same connection object can be used to create further statements.
connection.close();
This is the final code:
import java.sql.*; //Step 2
class basicJDBC {
public static void main(String args[]) throws ClassNotFoundException, SQLException {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //Step 3
Connection connection = DriverManager.getConnection("jdbc:odbc:northbrick"); //Step 4
Statement stmt = connection.createStatement(); // Step 5
ResultSet rs = stmt.executeQuery("Select * from Shippers"); //Step 6
while(rs.next()) { //Step 7
System.out.print( rs.getString("CompanyName")+", ");
System.out.println(rs.getString("Phone"));
}
//Step 8
rs.close();
stmt.close();
//Step 9
connection.close();
}
}