Download presentation
Presentation is loading. Please wait.
Published byChad Morrison Modified over 8 years ago
1
JSP/Database Connectivity Instructor: Dr. M. Anwar Hossain
2
Accessing a Database: Processing Steps 1. Load a driver which is compatible with the database that is to be processed. 2. Define and establish a connection to the database. 3. Associate an SQL statement with this connection. 4. Execute the SQL statement. 5. The SQL statement which has been executed will produce a table which is stored in a ResultSet object. This object will contain a reference to the rows of the table that has been formed by the execution of the SQL statement. 6. Execute further SQL statements as above. 7. When the processing associated with the database is complete the database is closed and the connection to the database is also closed.
3
Accessing a Database: Processing Steps Example 1. Class.forName("org.gjt.mm.mysql.Driver"); 2. Connection conn = DriverManager.getConnection( "jdbc:mysql://mysql0.ee.surrey.ac.uk:3306/webtech", "webtech", "webtech"); 3. Statement st = conn.createStatement(); 4. ResultSet rs=st.executeQuery("SELECT * FROM images"); 5. while(rs.next()){ anInteger = rs.getInteger(1); aString = rs.getString(2); } 7. st.close(); rs.close(); conn.close();
4
Define and Establish the Connection Create a connection object Connection conn = null; Load the JDBC driver and connect to the database Class.forName("driver..."); conn = DriverManager.getConnection("url...", [Username], [Password]); NOTE: All functions of connecting and using a database should be enclosed within a try-catch block. NOTE: A database connection should always be closed after the code has finished using the database.
5
Create a Statement Object and Execute Create a Statement object and execute the SQL Statement st = conn.createStatement(); // for selecting records ResultSet rs = st.executeQuery("query..."); or // for inserting, deleting or updating records int numRows = st.executeUpdate("query..."); NOTE: Capturing exceptions is important and should not be ignored
6
Process the Results Process the ResultSetMetaData ResultSetMetaData rm = rs.getMetaData(); rm.getColumnCount(); // Number of columns rm.getColumnName(1); // Name of column rm.getColumnType(1); // Data type of column Process the ResultSet rs.next(); // move to next record, returns boolean rs.getXxx(); rs.getString(1); // using column id rs.getString("name"); // using column name rs.getInteger(2); Rs.getObject(3);
7
Example: Displaying results on a Table First (based on previous 3 slides): Define and Establish the Connection Create a Statement Object and Execute the SQL Get the ResultSetMetaData and ResultSet Then, get number of columns and build a list of column names int columns = rm.getColumnCount(); result = " "; for ( int i = 1; i <= columns; i++){ result += " " + rm.getColumnLabel(i) + " "; } result += " ";
8
Example: Displaying results on a Table Then, get the actual data while(rs.next()) { result += " "; for ( int i = 1; i <= columns; i++) { result += " " + rs.getObject(i).toString() + " "; } result += " "; } Close the Statement, ResultSet, and Connection st.close(); rs.close(); conn.close(); Eventually, you print the results on a JSP page
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.