Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Same column names in JOIN getting "column not found" error in ResultSet

I have three tables firm, shipp and supplier.

all three columns have same column names as id, name, city ... other.

And I have written a join like in java:

String Query = "SELECT s.name Sname,s.city Scity,f.name Fname,f.city Fcity,"
        +"su.name Suname,su.city Sucity "
        +"FROM `order_details` ot "
        +"INNER JOIN `order` o ON ot.odr_id = o.odr_id " 
        +"INNER JOIN `product` p ON ot.pro_id = p.id "
        +"INNER JOIN `firm` f ON o.firm_id = f.id "
        +"INNER JOIN `shipp` s ON o.shipp_id = s.id "
        +"INNER JOIN `supplier` su ON o.sup_id = su.id ";

product,order and order_details are the other tables that are used in join.

This query working in phpMyAdmin.

But in java ResultSet giving "column 'Sname' not found." error.

I have tried below solutions but these are not working for me:

1) java.sq.SQLException: Column not found

2) Strange SQLException: Column not found

3) Many other but no one working

Error Stack:

java.sql.SQLException: Column 'Sname' not found.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)

And:

SEVERE: null

Please help me!

Thanks In advance.

Edit 1:

Problem Reproduce:

When I run it with fetching data only from one aliased table (e.g. shipp) then it fetches data fine:

SELECT s.name Sname,s.city Scity ... other query part

But as soon as I SELECT columns from other aliased tables (e.g. shipp, firm, and supplier) then it throws error exception written above (Column not found).

SELECT s.name Sname,s.city Scity,f.name Fname,f.city Fcity,
       su.name Suname,su.city Sucity ... other query part

Note: This query running fine in phpmyadmin but not running with ResultSet fetch.

And this is how I'm getting columns from ResultSet (rs2):

String firstColumn = "";
String secondColumn = "";

while(rs2.next()) {
    firstColumn = "Shipp to : ";
    firstColumn += rs2.getString("Sname") + rs2.getString("Scity") ;
    firstColumn += "Person : " + rs2.getString("Fname") +"+ "+ rs2.getString("Fcity");

    secondColumn = "Supplier : " + rs2.getString("Suname");
    secondColumn += rs2.getString("Suname");
    secondColumn += rs2.getString("Sucity");
}
like image 971
youpilat13 Avatar asked Apr 23 '18 15:04

youpilat13


People also ask

How do you know if a ResultSet has a column?

ResultSetMetaData meta = rowSet. getMetaData(); Get the number of columns in the RowSet using the getColumnCount() method.

What is ResultSet getString ()?

getString(String columnLabel) Retrieves the value of the designated column in the current row of this ResultSet object as a String in the Java programming language. Time. getTime(int columnIndex) Retrieves the value of the designated column in the current row of this ResultSet object as a java.

Can we reuse ResultSet?

If you want to test, whether your resultset gets closed or not, you can use a while loop to iterate over the result set and inside the while loop, create another query and assign it to same result set. You will see that an Exception will be thrown..

Why is ResultSet empty?

Hence, when a Java programmer needs to determine if ResultSet is empty or not, it just calls the next() method and if next() returns false it means ResultSet is empty.


2 Answers

Based on your error message I assume that you are using MySQL with the latest connector.

You can utilize the positional queries if necessary, this is using the number on which position on which the element could be find for example:

rs2.getString(1);

(Yes the numbering starts at 1...) It should be equivalent to this (of course without the capitalization problem):

rs2.getString("Sname");

This way you have to know the position only, but if you look at case sensitivity in the database/driver is pretty much the question of settings. To get around most of the problems I write my queries in capital if possible and without a backtick `.

To explore your ResultSet object try the following:

Statement statement = ...;
String queryString = "SELECT s.name sname,s.city scity,f.name fname, f.city fcity, su.name suname, su.city sucity "
        +" FROM `order_details` ot "
        +" INNER JOIN `order` o ON ot.odr_id = o.odr_id "
        +" INNER JOIN `product` p ON ot.pro_id = p.id "
        +" INNER JOIN `firm` f ON o.firm_id = f.id "
        +" INNER JOIN `shipp` s ON o.shipp_id = s.id "
        +" INNER JOIN `supplier` su ON o.sup_id = su.id ";
ResultSet resultSet = statement.executeQuery(queryString);
ResultSetMetaData metaData = resultSet.getMetaData();
int colCount = metaData.getColumnCount();
if (resultSet.next()) {
    for (int i = 1; i <= colCount; i++) {
        System.out.println("Col(" + i + ") '" + metaData.getColumnName(i) + "' value:" + resultSet.getString(i));
    }
}else{
   System.out.println("No row found.");
}

This snippet will print first results the columns and values which are returned by the driver you are using.

like image 85
Hash Avatar answered Sep 29 '22 07:09

Hash


Please check you have imported the correct jdbc connection package com.mysql.jdbc.connection. You could have imported java.sql.connection instead as here

like image 29
Vignesh Avatar answered Sep 29 '22 07:09

Vignesh