【问题标题】:JSP SQL SERVER ConnectionJSP SQL SERVER 连接
【发布时间】:2018-11-20 02:10:32
【问题描述】:

我在 C:\Program Files\Apache Software Foundation\Tomcat 9.0\webapps\webapp1 文件夹中有以下 JSP 页面。

当我尝试在 Google Chrome 中打开此页面时 http://localhost:7544/webapp1/DatabaseDB

我收到错误:本地主机拒绝连接。

<%@ page contentType="text/html; charset=UTF-8" language="java" errorPage="" %>
    <html>
       <head>
          <title>SELECT Operation</title>
       </head>
    <body>
    <%@ page import = "java.io.*" %>
    <%@ page import = "java.util.*" %>
    <%@ page import = "java.sql.*" %>
    <%@ page import = "javax.sql.*" %>
    <%@ page import = "java.sql.DriverManager" %>
    <%@ page import = "java.sql.ResultSet" %>
    <%@ page import = "java.sql.Connection" %>
    <%@ page import = "java.sql.Statement" %>
    <%@ page import = "java.sql.SQLException" %>
    <%@ page import = "java.sql.ClassNotFoundException" %>
    <%@ page import = "java.lang.System" %>
    <%@ page import = "java.lang.System.err" %>
    <%@ page import = "java.lang.System.exit" %>
    <%
    Connection conn = null;
    try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            conn = DriverManager.getConnection("jdbc:sqlserver://ASDFE;DatabaseName=PERSON","sa", "PWD");
            Statement stmt = conn.createStatement();
            stmt = conn.createStatement();
            ResultSet rs= stmt.executeQuery("SELECT TOP 1 PersonName FROM PersonDetails");

         %>
          <table border="1">
              <tr>
                  <th>ID</th>
              </tr>
         <%
         while ( rs.next() ) {%>
            <tr><td><%=rs.getString("PersonName") %></td></tr>

          <% 
          }
          rs.close();
          stmt.close();
          conn.close(); %>
          </table>
          <%
        }
        catch (ClassNotFoundException cnfe) {
                System.err.println(cnfe.getMessage());
                System.exit(1);
            }
            catch (SQLException e) {
                System.err.println(e.getMessage());
                System.exit(2);
            }
    %>
    </body>
    </html>

【问题讨论】:

    标签: java sql-server sql-server-2008 jsp


    【解决方案1】:
    1. 您确定端口是 7544 吗?
    2. 默认端口为 8080
    3. 先尝试http://localhost:8080/,看看是否显示了 Tomcat 主屏幕。
    4. 如果您确定 7544 正确,请尝试 http://localhost:7544
    5. 可能是它:http://localhost:7544/webapp1/DatabaseDB.jsp(后缀 .jsp 缺失)

    【讨论】:

    • 嗨 Rey 感谢您的回复,我已尝试使用 localhost:7544/webapp1/DatabaseDB.jsp。和端口号是 7544,现在我得到同样的错误。
    • 如果您尝试 3) 和 4),您会看到什么?
    • Hi Rey 端口号默认更改为 8080。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-11
    • 1970-01-01
    • 2022-12-24
    • 2015-11-20
    • 2015-01-14
    • 2016-11-22
    • 2011-08-08
    相关资源
    最近更新 更多