Tomcat无法连接到localhost上的windows sql

时间:2017-04-27 06:57:35

标签: sql-server tomcat jdbc

我是Java和servlet编程的新手,所以这个问题可能真的很愚蠢! 我有Windows 10,SQL Server 2008和sql jdbc驱动程序用于java和Apache Tomcat 9,我正在使用Eclipse for Java进行编程。

我可以连接到sql并使用java applet设置/获取数据,同时我在localhost上的tomcat服务器也能正常工作。但是当我尝试通过tomcat(在servlet上)从sql获取相同的数据时,我遇到了大量错误。

以下是代码:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter printwriter = response.getWriter();

    java.sql.Connection conn = null;
    StringBuilder output= new StringBuilder();
    output.append("<html><body><table>");
    try{
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        conn = DriverManager.getConnection("jdbc:sqlserver://localhost; integratedSecurity=true; databaseName=baessland; user=sa; password=1234;");
        ResultSet result = conn.createStatement().executeQuery("Select name,features from products");
        System.out.println(result.toString());
        while (result.next()){
            output.append("<tr><td>" + result.getString(1) + "</td><td>" + result.getString(2) + "</td></tr>");
        }
    }
    catch (ClassNotFoundException e){System.out.println(e.getMessage());}
    catch (SQLException e){}
    catch (Exception e) {}

    output.append("</table></body></html>");

    printwriter.println(output);
}

0 个答案:

没有答案