import java.sql.*; class EmpList { public static void main (String args []) throws SQLException, ClassNotFoundException { // Load the Oracle JDBC driver Class.forName ("oracle.jdbc.driver.OracleDriver"); // Connect to the database Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@yourDB_URL:yourPort_Number:yourDB_NAME", "yourOracleUsername", "yourOraclePassword"); // Create a Statement Statement stmt = conn.createStatement (); // Select some columns from the EMP table ResultSet rset = stmt.executeQuery ("SELECT * FROM Emp ORDER BY deptno, ename"); // Get the MetaData ResultSetMetaData rsmd = rset.getMetaData(); int numberOfColumns = rsmd.getColumnCount(); System.out.println("Content-type: text/html\n\n"); System.out.println("<html>"); System.out.println("<head><title>Simple Java Program</title></head>"); System.out.println("<body><h2>List of Employees</h2>"); System.out.println("<table border=1 style=\"background-color: yellow\">"); System.out.println("<tr>"); // Print the column headings for (int i = 1; i <= numberOfColumns; i++) System.out.println("<th>"+rsmd.getColumnName(i)+"</th>"); System.out.println("</tr></tbody>"); // Iterate through the result and print the employee names while (rset.next ()) { System.out.println("<tr>"); for (int i = 1; i <= numberOfColumns; i++) { System.out.println("<td>"+rset.getString(i)+"</td>"); } System.out.println("</tr>"); } System.out.println("</tbody></table></body></html>"); rset.close(); conn.close(); } } |