我有三张桌子,shipp和供应商.
所有三列都有与id,name,city … other相同的列名.
我在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和order_details是join中使用的其他表.
但是在java ResultSet中,没有找到“列’Sname’.”错误.
我尝试过以下解决方案,但这些对我不起作用:
1)java.sq.SQLException: Column not found
2)Strange SQLException: Column not found
3)许多其他但没有人工作
错误堆栈:
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)
和:
SEVERE: null
请帮我!
提前致谢.
编辑1:
问题再现:
当我只从一个别名表(例如shipp)获取数据来运行它时,它会获取数据:
SELECT s.name Sname,s.city Scity ... other query part
但是一旦我从其他别名表中选择列(例如shipp,firm和supplier),它就会抛出上面写的错误异常(未找到列).
SELECT s.name Sname,su.name Suname,su.city Sucity ... other query part
注意:此查询在PHPmyadmin中运行良好,但未使用ResultSet fetch运行.
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");
}
如果需要,您可以使用positional queries,这是使用可以找到元素的位置的数字,例如:
rs2.getString(1);
(是的,编号从1开始…)它应该等同于此(当然没有大写问题):
rs2.getString("Sname");
这样你只需知道位置,但是如果你看一下数据库/驱动程序中的区分大小写几乎是设置的问题.为了解决大多数问题,如果可能的话,我会用资本编写我的查询,而不需要反引号.
要探索ResultSet对象,请尝试以下操作:
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.");
}
此代码段将首先打印您正在使用的驱动程序返回的列和值.