To establish a connection you need to have the appropriate driver connect to the DBMS. The following line of code illustrates the general idea:
String url = “jdbc:odbc:Fred”; Connection con = DriverManager.getConnection(url, “Fernanda”, “J8?);
A Statement object is what sends your SQL statement to the DBMS. You simply create a Statement object and then execute it, supplying the appropriate execute method with the SQL statement you want to send. For a SELECT statement, the method to use is executeQuery. For statements that create or modify tables, the method to use is executeUpdate. It takes an instance of an active connection to create a Statement object. In the following example, we use our Connection object con to create the Statement object
Statement stmt = con.createStatement();
JDBC returns results in a ResultSet object, so we need to declare an instance of the class ResultSet to hold our results. The following code demonstrates declaring the ResultSet object rs.
ResultSet rs = stmt.executeQuery(”SELECT COF_NAME, PRICE FROM COFFEES”); String s = rs.getString(”COF_NAME”);
The method getString is invoked on the ResultSet object rs, so getString() will retrieve (get) the value stored in the column COF_NAME in the current row of rs.
Regular statement (use createStatement method), prepared statement (use prepareStatement method) and callable statement (use prepareCall)
The life cycle of a servlet consists of the following phases: