【问题标题】:integrate ms access and mysql in java在java中集成ms access和mysql
【发布时间】:2010-01-08 03:04:11
【问题描述】:

我在使用 Java 连接到 MS Access 和 MySQL 时遇到问题。我的问题是我找不到 MySQL 的驱动程序。这是我的代码:

<%@ page import="java.sql.*" %>

<%
   Connection odbcconn = null;
   Connection jdbcconn = null;
   PreparedStatement readsms = null;
   PreparedStatement updsms = null;
   ResultSet rsread = null;

   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  //load database driver
   odbcconn = DriverManager.getConnection("jdbc:odbc:SMS");  //connect to database
   readsms = odbcconn.prepareStatement("select * from inbox where Status='New'");
   rsread = readsms.executeQuery();
   while(rsread.next()){
        Class.forName("com.mysql.jdbc.Driver");
        jdbcconn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bakme", "root", "");  //connect to database
        updsms = jdbcconn.prepareStatement("insert into inbox(sms,phone) values (?,?)");
        updsms.setString(1, rsread.getString("Message"));
        updsms.setString(2, rsread.getString("Phone"));
        updsms.executeUpdate();
   }

%>

【问题讨论】:

  • 看看你的代码在使用内置格式时阅读起来有多容易

标签: java mysql ms-access jsp jdbc


【解决方案1】:

因此,您在 MySQL JDBC 驱动程序类上获得了ClassNotFoundException?然后,您需要将包含该类的 MySQL JDBC 驱动程序 JAR 文件放在类路径中。对于 JSP/Servlet 应用程序,类路径覆盖在每个 Web 应用程序的 /WEB-INF/lib 文件夹下。只需将 JAR 文件放在那里。它的 JDBC 驱动程序也称为 Connector/J。可以here下载。

也就是说,这确实不是将 JDBC 和 JSP 一起使用的方式。这不属于 JSP 文件。您应该在真正的 Java 类中执行此操作。 JDBC 代码也应该写得更健壮,现在它正在泄漏资源。

【讨论】:

【解决方案2】:

BalusC 是正确的:这不是你应该写这样的东西的方式。

Connection、Statement 和 ResultSet 都代表有限的资源。它们不像内存分配;垃圾收集器不会清理它们。您必须在代码中执行此操作,如下所示:

// Inside a method
Connection connection = null;
Statement statement = null;
ResultSet rs = null;

try
{
    // interact with the database using connection, statement, and rs
}
finally
{
    // clean up resources in a finally block using static methods, 
    // called in reverse order of creation, that don't throw exceptions
    close(rs);
    close(statement);
    close(connection);
}

但是,如果您决定将其移至服务器端组件,您肯定会遇到如下代码的巨大问题:

while(rsread.next())
{
    Class.forName("com.mysql.jdbc.Driver");
    jdbcconn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bakme", "root", "");  //connect to database
    updsms = jdbcconn.prepareStatement("insert into inbox(sms,phone) values (?,?)");
    updsms.setString(1, rsread.getString("Message"));
    updsms.setString(2, rsread.getString("Phone"));
    updsms.executeUpdate();
}

注册驱动程序,创建连接,而不关闭它,并在循环内为您从 Access 中获得的每一行重复准备一个语句,这表明对关系数据库和 JDBC 的严重误解。

您应该注册驱动程序并创建连接一次,做需要做的事情,并清理您使用的所有资源。

如果您绝对必须在 JSP 中执行此操作,我建议您对两个数据库都使用 JNDI 数据源,这样您就不必在页面内设置连接。您不应该编写 scriptlet 代码 - 最好学习 JSTL 并使用它的 标签。

【讨论】:

    【解决方案3】:

    您可以use this link to download the MySql Driver。下载后,您需要确保它位于您正在使用的 Web 服务器的类路径中。为服务器配置 JDBC 驱动程序的细节因服务器而异。您可能需要编辑您的问题以包含更多详细信息以获得更好的答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-25
      • 1970-01-01
      • 2014-07-24
      • 1970-01-01
      • 2011-05-16
      • 2011-10-12
      • 1970-01-01
      相关资源
      最近更新 更多