【发布时间】:2018-06-27 11:00:05
【问题描述】:
我有这个基本代码来连接 oracle 数据库并从名为 temp_employee 的表中检索一列。但是,当我编译时,它不会在网页上显示数据。它没有显示错误消息。只是一个空白页。你能帮助使这段代码正常工作吗?
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import ="java.sql.*" %>
<%@ page import=" javax.sql.DataSource" %>
<html>
<head>
<title>Database</title>
</head>
<body>
<%
Connection connection = null;
try {
// Load the JDBC driver
String driverName = "oracle.jdbc.driver.OracleDriver";
Class.forName(driverName);
connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:dev", "root", "root");
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT aadName from temp_employee");
while (resultSet.next()) {
String code = resultSet.getString("aadName");
};
} catch (ClassNotFoundException e) {
out.println("Driver caused Exception : " + e.getMessage() + "");
// Could not find the database driver
} catch (SQLException e) {
// Could not connect to the database
out.println("Database caused Exception : " + e.getMessage() + "");
}
%>
</body>
</html>
【问题讨论】:
-
不相关,但是:不要将类似的代码放入 JSP 页面。创建一个 Servlet 或 JSP 标记来处理数据检索。 SQL 代码或对
DriverManager.getConnection()的调用不应出现在 JSP 页面中。