java - Ambiguous column using JDBC but query works fine in database -


i connecting sqlite database through java using jdbc.

schema:

workinfo(id, job, salary) person(id, name) 

this query below runs fine in database, when try jdbc:

resultset rs = statement.executequery("select * person join workinfo on (person.id=workinfo.id)"); while(rs.next()){      system.out.println("id: " + rs.getint("person.id")); //column not exist     system.out.println("name: " + rs.getstring("name")); //works fine 

output:

if using person.id: no such column: 'person.id'

without specifying: ambiguous column name 'id'

i've tried using both workinfo , person , using aliases keeps throwing same ambigious column name (if left id) or column not exist.

it's practice explicitly retrieve columns want. change query be:

resultset rs = statement.executequery("select info.id, info.job, info.salary, "     + "person.id, person.name person person join workinfo info "     + "on person.id=info.id"); while(rs.next()){      system.out.println("id: " + rs.getint(4));     system.out.println("name: " + rs.getstring(5)); 

in case, can use column index instead of label.

or using as clause:

resultset rs = statement.executequery("select info.id, info.job, info.salary, "     + "person.id personid, person.name personname "     + "from person person join workinfo info "     + "on person.id=info.id"); while(rs.next()){      system.out.println("id: " + rs.getint("personid"));     system.out.println("name: " + rs.getstring("personname")); 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -