Hello to all! I am doing practice for JDBC and using netbeans 8.1. I created a table in mc access and wrote a program. But the problem is that when i pass column index then my program runs successfully. But I pass column name as in my table in mc access then there occurs an error that "Column not found".
I am pasting code of my program and try to explain my problem further.
package database;
impot java.sql.*;

package database;
impot java.sql.*;

public class Database {

public static void main(String[] args) {


        String url = "jdbc:odbc:personDSN";    

        Connection con = DriverManager.getConnection(url);

        Statement st = con.createStatement();

        String sql = "SELECT *FROM students";

        ResultSet rs = st.executeQuery(sql);

            String Name = rs.getString(2);

           String add = rs.getString(3);

           String pNum = rs.getString(4);

           System.out.println(Name + " "  + add + " "  + pNum);


        catch(ClassNotFoundException | SQLException sqlEx)



As you can see in while loop that i have passed column index in getString() function. In this case my program runs successfully. But when i pass name of field/attribute which is in my database table, then it gives me error that "Column not found". For example if I pass getString("name") then it gives me above error.
Please help me to solve my problem.

Note: I have checked again and again that there is no spelling mistake in my parameter opposite to actual table in ms access.

Recommended Answers

All 5 Replies

Is the capitalisation of the names the same?

yes the capitalization of the names is same

where is that getString("name"); call? I only see calls to a getSring(int column); method.

Ehmmm silly question will
"SELECT *FROM students"

actually work?
Shouldn't it be with space?
"SELECT * FROM students"

I haven't touched DBs in ages :P

Peter: that might depend on the DB you use, but when you run a query like that in MS SQL Server Management Studia, it returns he data.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.