English 中文(简体)
从Tomcat 服务器服务器到 MSSQL 2012 服务器快递的查询问题
原标题:Query problems from Tomcat servlets to MSSQL Server Express 2012

这里是我的设置。 在虚拟机器( Fusion) 中运行 MSSQL 服务器 Express 2012 。 虚拟机器运行在 OS X 狮子服务器上。 在 OS X 狮子 上运行了一个 Tomcat 7 服务器。 我从独立 Java 尝试以下代码 :

static {
    try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

private final static String host = "ip-address:1433";
private final static String database = "databasename";
private final static String user = "user";
private final static String passwd = "password";

public static void main(String[] args) {
    try {
        String connectionUrl = "jdbc:sqlserver://"+host+";database="+database+";integratedSecurity=true;";
        Connection con = DriverManager.getConnection(connectionUrl,user,passwd);
        PreparedStatement pstm = con.prepareStatement("select * from sektor");
        ResultSet res = pstm.executeQuery();
        while(res.next())
            System.out.println("Sektor: " +res.getString("sektornavn"));
    } catch (SQLException e) {
        System.out.println(e.getMessage());
    }
}

这工作非常成功, 并打印了表的内容。 现在使用相同的代码( 仅改变输出到系统响应. out ) 。

static {
    try {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

private final static String host = "ip-address:1433";
private final static String database = "databasename";
private final static String user = "user";
private final static String passwd = "password";


@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setContentType("text/plain");
    resp.setCharacterEncoding("UTF-8");
    // Write response
    PrintWriter writer = resp.getWriter();
    try {
        String connectionUrl = "jdbc:sqlserver://"+host+";database="+database+";";
        writer.println("Connecting<br/>");
        Connection con = DriverManager.getConnection(connectionUrl,user,passwd);
        writer.println("Preparing statement<br/>");
        PreparedStatement pstm = con.prepareStatement("select * from sektor");
        writer.println("Executing statement<br/>");
        ResultSet res = pstm.executeQuery();
        while(res.next())
            writer.println("Sektor: " +res.getString("sektornavn")+"<br/>");
    } catch (SQLException e) {
        writer.println(e.getMessage());
    }
    writer.flush();
    writer.close();

}

我的问题是, 服务器在到达 pstm. executeQuery () 时停止运行。 我通过删除代码的( commenting out) 部分缩小到此点, 这表明它会创建连接( DriverManager. getConnection.) 并准备声明。 只有在添加 pstm. executeQuery () 时, 服务器才会做出响应 。

我使用来自微软的 JDBC 连接器, 罐子既可用于独立 Java, 也可以用于服务器 。 我找不到任何例外来帮助查找错误 。

真正令我印象深刻的是,它会获得连接,但只是不执行查询。所有建议都非常受欢迎。

最佳回答

我不知道这是否有用,但你是否考虑过使用JTDS jdbc驱动器?我使用他的jtds驱动器已有一段时间了,我发现这比MS jdbc驱动器的问题小。我曾经有过一些无法用MS驱动器解释的事情。

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签