net.ucanaccess.jdbc.UcanaccessSQLException: Column not found: 0

寵の児 提交于 2019-12-24 03:36:12

问题


I am new for UCanAccess

package checktpsystemdatabase;

import java.sql.*;

public class CheckTPSystemDatabase {

    public static void main(String[] args) throws SQLException {
        try {
            Connection con = DriverManager.getConnection("jdbc:ucanaccess://D:/Java/TransactionProcessingSystem/src/transactionprocessingsystem/Resources/TPSystem.accdb");

            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM Product");

            while (rs.next()) {
                System.out.println(rs.getInt(0) + "\t" + rs.getString(1) + "\t" + rs.getString(2));
            }
            rs.close();

        } catch (SQLException e) {
            System.out.println(e);
        }
    }
}

When I execute this code, It is showing "net.ucanaccess.jdbc.UcanaccessSQLException: Column not found: 0". Please help me!


回答1:


You are seeing that error because the numeric index values for a JDBC ResultSet start with 1, not 0. Or, as they say in the "Retrieving Column Values from Rows" section of the Java Tutorial here:

The ResultSet interface declares getter methods (for example, getBoolean and getLong) for retrieving column values from the current row. You can retrieve values using either the index number of the column or the alias or name of the column. The column index is usually more efficient. Columns are numbered from 1

(Emphasis mine.)




回答2:


simply

the ResultSet rs begins with index 1 not 0 so you should write rs.getInt(1) or rs.getObject(1)



来源:https://stackoverflow.com/questions/26527054/net-ucanaccess-jdbc-ucanaccesssqlexception-column-not-found-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!