在SE mysql-connector-java-5.1.22-bin.jar工作时,EE的jdbc出错

时间:2012-12-03 03:05:50

标签: java jdbc

我在桌面应用程序中使用mysql-connector-java-5.1.22-bin.jar用于JAVASE 7的桌面应用程序(工作正常)但是对于JAVASE 6(使用与JAVASE 7 app相同的代码)不工作我既没有在控制台中看到任何异常,也没有看到我做的任何调试打印。什么可能是错的?

继承我的项目文件夹http://open-pages.com/temp.zip

1 个答案:

答案 0 :(得分:0)

我认为您的问题不涉及Java 6与Java 7.就我所读,您的代码中没有任何内容使用Coin语法。问题是java桌面应用程序与Web应用程序。 JDBC驱动程序是否包含在war文件中? (在库文件夹中)我通常在上下文中配置数据库连接。 xml并使用InitialContext获取dataSource。

只需编写一个简单的测试jsp或Servlet即可连接到您的数据库并显示一些查询结果。您可以直接在网页上打印任何异常,以便于测试。我不确定为什么异常没有写入您的日志文件(检查写入服务器日志文件夹的最新文件)

<%-- mysqlTest.jsp :For testing mysql connection and database query syntax --%>
<html><body>
<h2 align="center">SQL Test for mySQL JDBC Driver</h2>
<%
java.sql.Connection con = null;
try {
    String url="jdbc:mysql://localhost:3306/test";
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    con=java.sql.DriverManager.getConnection(url, "root", "abc");
    if(con!=null) {
        String query="select userName, password from users";
        java.sql.PreparedStatement stmt=con.prepareStatement(query);
        java.sql.ResultSet r=stmt.executeQuery();
        while(r.next()){
            out.print(r.getString(1));
            out.println("<br>");
        }
        r.close();
        stmt.close();
    }
    else
        out.println("<strong>The Database connection is null.  DriverManager could not return a valid connection.</strong>");        
    }
    catch(ClassNotFoundException e){
        out.println("Cannot load driver<br>" +e.toString());
    }
    catch(java.sql.SQLException e){
        out.println(e);
    }
    catch(Exception e){
        out.println(e);
    }
    finally {
        if(con!=null)
            con.close();
    }
%></body></html>