728x90

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="java.sql.*, java.util.*,java.text.*,javax.sql.*,javax.naming" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Contents-Type" content="text/html; charset=UTF-8">
<title>JSP Oracle JDBC CONN TEST</title>
</head>
<body>
<%
Connection conn = null;
Statement stmt;
ResultSet rs;
final String dataSource = "[jndi name]";

try {
Context context = new InitialContext();
DataSource ds = (DataSource)context.lookup(dataSource);

conn = ds.getConnection();
stmt = conn.createStatement();

rs = stmt.executeQuery( "SELECT sys_context('USERENV', 'DB_NAME') from dual");

out.println("WAS "+dataSource+" connect ok");

ResultSetMetaData rsmd = rs.getMetaData();
int colCnt = rsmd.getColumnCount();

StringBuffer sb = new StringBuffer();
sb.append("<table border='1' >");
sb.append("tr style='font-size : 13px; '>");
for(int i = 1; i <= colCnt; i++)
sb.append("<th>"+ rsmd.getColumnName(i) + "</th>");
sb.append("</tr>");

boolean row_flag = false;
while (rs.next()) {
row_flag = true;
sb.append("<tr style=font-size=12px;'>);
for(int i=1; i <= colCnt; i++)
sb.append("<td>" + rs.getString(rsmd.getColumnName(i)) + "</td>");
  sb.append("</tr>");
}
if(row_flag == false)
   sb.append("td colspan='" +  colCnt  + "</td>");
   sb.append("</table>");
   out.print(sb.toString());

rs.close();
stmt.close();
conn.close();
} catch(Exception e) {
       out.println(e.getMessage());
}
%>
</body>
</html>

728x90

+ Recent posts