有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java对多个数据库使用单个JDBC连接

我有一个到SQLite数据库的JDBC连接。从第一个数据库(productLine.db)获取信息后,我想将连接切换到shipMethods。然后从数据库中读取一个表。我目前得到的错误是:

    SQL error or missing database (no such table: Ground)

我检查了数据库,桌子就在那里。我需要创建一个新的连接变量吗?还是我做错了什么

代码如下:

    //Get connection to database
    try {
        Connection con = DriverManager.getConnection ("jdbc:sqlite:productLine.db");
        //Make statement for interacting with database
        Statement stmt = con.createStatement();

        //Get total weight
        ResultSet rs = stmt.executeQuery ("SELECT Weight FROM Numbers WHERE TYPE = '" + product + "'");
        while (rs.next()) {
            weight = rs.getDouble("weight")*1000;
        }

        //get zone
        rs = stmt.executeQuery ("SELECT ZONE FROM Zones WHERE ZIP = " + shortZip);
        while (rs.next()) {
            zone = rs.getInt("Zone");
        }

        //Change to Ship Methods database
        con = DriverManager.getConnection("jdbc:sqlite:shipMethods.db");

        //Get Price
        rs = stmt.executeQuery ("SELECT EIGHT FROM Ground WHERE WEIGHT = " + weight);
        while (rs.next()) {
            price = rs.getDouble("Cost");
        }

        System.out.println("Weight: " + weight);
        System.out.println("Zone: " + zone);
        System.out.println("Price: " + price);

    } catch(SQLException e) {
        System.out.println("SQL exception occured" + e);
    }

共 (1) 个答案

  1. # 1 楼答案

    您正在使用的Statement属于旧连接。 (你有两个活动连接;否则计算机怎么知道该使用哪一个呢?)

    从新连接创建新语句