dimmed dimmed - 7 months ago 26
Java Question

Java retrieving of data in ms access "Column not found" error

i'm trying to program an application that retrieves data from ms access in java. this is my code:

import java.sql.*;

public class testdb {
public static void main(String[] args) {
String path = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + "C:\\smartphone.accdb";
Statement statement;
ResultSet rs;
Connection con;
String sql = "SELECT dev_name,points FROM list";

try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

con = DriverManager.getConnection(path, "", "");

System.out.println("Connected");

statement = con.createStatement();
statement.executeQuery(sql);
rs = statement.getResultSet();
System.out.println(rs.getString("SELECT dev_name FROM list"));
} catch (Exception ex) {
System.err.println("Got an exception");
System.err.println(ex.getMessage());
}
}
}


it compiles correctly and gives and output of:

Connected
Got an exception
Column not found


please help.

Answer

Change

rs.getString("SELECT dev_name FROM list")

to

rs.getString("dev_name")

Also iterare the ResultSet using

while(rs.next())

To iterate the ResultSet you use its next() method. The next() method returns true if the ResultSet has a next record, and moves the ResultSet to point to the next record.If there were no more records, next() returns false.