0

I use JDBC to get data from sqlite data base. In DB I have 3 column - login, password and role. I try find row by login, but it doesn't work , and I have exeption when I try getString("password") or "role", where is the mistake? Thanks

resSet = statmt.executeQuery("SELECT * FROM users WHERE login='"+login+"';");
if( hasUser( login)){
    System.out.println("User finded:");
    while(resSet.next()) {
        System.out.println("login = " + resSet.getString("login"));
        // !exeption   
        System.out.println("password = " + resSet.getString("password"));
        // !exeption   
        System.out.println("role = " + resSet.getString("role"));
        System.out.println();
    }
}else{
    System.out.println( "User not found");
}
Vivek Singh
  • 2,047
  • 11
  • 24
dzrkot
  • 543
  • 2
  • 4
  • 23
  • 2
    print exception log with your question – Ramanlfc Dec 24 '15 at 11:21
  • java.sql.SQLException: no such column: 'password' at org.sqlite.jdbc3.JDBC3ResultSet.findColumn(JDBC3ResultSet.java:48) at org.sqlite.jdbc3.JDBC3ResultSet.getString(JDBC3ResultSet.java:437) at SE_10_JDBC.task1.DataBaseHandler.readUserFromDB(DataBaseHandler.java:56) at SE_10_JDBC.task1.MainTask1.main(MainTask1.java:15) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) – dzrkot Dec 24 '15 at 11:22
  • users table should have password column , the exception answers your question pretty clearly – Ramanlfc Dec 24 '15 at 11:23
  • but column password and role exsist – dzrkot Dec 24 '15 at 11:23
  • and that code work properly: – dzrkot Dec 24 '15 at 11:24
  • can you check the names of your columns with that of which you are accessing. If these are same try to run this query `select login, password, role from users WHERE login=?;` – Vivek Singh Dec 24 '15 at 11:24
  • resSet = statmt.executeQuery("SELECT * FROM users"); while(resSet.next()) { System.out.println("login = " + resSet.getString("login")); System.out.println("password = " + resSet.getString("password")); System.out.println("role = " + resSet.getString("role")); System.out.println(); } – dzrkot Dec 24 '15 at 11:24
  • I try it, but it doesn't work too – dzrkot Dec 24 '15 at 11:29
  • it's probably because your query isn't returning results of password column – Ramanlfc Dec 24 '15 at 11:33
  • Please use the "edit" option under your question to put the stack trace in the question instead of the comment. – Adam Michalik Jan 09 '16 at 10:22

2 Answers2

0
ResultSetMetaData metaData = resultSet.getMetaData();
int count = metaData.getColumnCount(); //number of column
String columnName[] = new String[count];

for (int i = 1; i <= count; i++)
{
  metaData.getColumnLabel(i)); 
}

Refer to this ans..ans see what are the names of your columns in resultant set http://stackoverflow.com/questions/19094999/java-how-to-get-column-name-on-result-set

Ankit Deshpande
  • 3,476
  • 1
  • 29
  • 42
0

You can check to see what column names are being returned.

ResultSetMetaData rsmd = resSet.getMetaData();
int colCount = rsmd.getColumnCount();

String rValue = "";
for (int i = 1; i <= colCount ; i++){

    String name = rsmd.getColumnName(i);
    rValue += name + " ";
}

System.out.println(rValue);
mawalker
  • 2,072
  • 2
  • 22
  • 34
  • 1
    That means your query is wrong. or your table doesn't actually have what you think it does... or something... what about when you "SELECT * FROM users" ? – mawalker Dec 24 '15 at 11:34
  • Looks like `PRAGMA table_info([users]);` might give results similar to 'MySQL's 'describe users' from http://stackoverflow.com/a/7679086/2801237 so that you can check your table – mawalker Dec 24 '15 at 11:37
  • You are right, thanks , I found place where I done mistake. I use if(hasLogin()) wich uncorrectly – dzrkot Dec 24 '15 at 11:40