<%@ page import="java.sql.*" %> JSP Database Operations
Create, load and query a table

<% Connection conn = null; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); conn = DriverManager.getConnection("jdbc:odbc:orcl","java","gosling"); Statement stmt = conn.createStatement(); String createString = "CREATE TABLE STAFF " + "(ID DECIMAL(3), " + "NAME VARCHAR(10), " + "OFFICE CHAR(7), " + "FEES DECIMAL (6,2))"; stmt.executeUpdate(createString); stmt.executeUpdate ("INSERT INTO STAFF VALUES(33,'Huerter','BA 311',901.23)"); stmt.executeUpdate ("INSERT INTO STAFF VALUES(407,'McWhorter','BA 310',2345.67)"); ResultSet rs = stmt.executeQuery ("SELECT * FROM STAFF ORDER BY NAME"); //Print start of table and column headers out.println(""); out.println("" + ""); //Loop through results of query. while(rs.next()) { out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); } out.println("
IdNameOfficeFees
" + rs.getInt("ID") + "" + rs.getString("NAME") + "" + rs.getString("OFFICE") + "" + rs.getBigDecimal("FEES") + "
"); stmt.executeUpdate ("DROP TABLE STAFF"); conn.close(); } catch(SQLException e) { out.println("SQLException: " + e.getMessage() + "
"); // while((e = e.getNextException()) != null) // out.println(e.getMessage() + "
"); } catch(ClassNotFoundException e) { out.println("ClassNotFoundException: " + e.getMessage() + "
"); } %>