%@ page import="java.sql.*" %>
JSP Database Connectivity
Database: SQL on Server
<%
Connection conn = null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection(
"jdbc:odbc:orcl","java","gosling");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM dept order by dname");
//Print start of table and column headers
out.println("");
out.println("Name | Phone | Address |
");
//Loop through results of query.
while(rs.next())
{
out.println("");
out.println("" + rs.getString("deptno") + " | ");
out.println("" + rs.getString("dname") + " | ");
out.println("" + rs.getString("loc") + " | ");
out.println("
");
}
out.println("
");
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() + "
");
}
%>