resultSet中没有数据-Jsp数据库

时间:2018-06-27 11:00:06

标签: java oracle jsp jdbc

我有这个基本代码来获取与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>

1 个答案:

答案 0 :(得分:0)

在您的html正文中进行一些更改, 在连接声明之后,声明String code = null; 并将现有代码更改为code = resultSet.getString("aadName"); 然后在html正文中添加以下行, <h2><%= code %></h2>

实际上,脚本表达式用于将代码值插入输出流。