列名无效!java代码里的SQL语句!数据库里可以得到正确为什么放java里出错了?
在数据库里执行下面的SQL语句可得到正确结果,放在java代码里错误了!Stringsql="SELECTtradeId,t.cardNo,tradeType,trade...
在数据库里执行下面的SQL语句可得到正确结果,放在java代码里错误了!
String sql = "SELECT tradeId, t.cardNo, tradeType, tradeAmount, targetCard, poundage, tradeTime, acceptBankID, atmNo FROM Trade t JOIN BankCard b on t.cardNo = b.cardNo JOIN Customer c on b.custId = c.custId WHERE name = ? ";
错误信息:
java.sql.SQLException: com.microsoft.sqlserver.jdbc.SQLServerException: 列名 'name' 无效。 Query: SELECT tradeId, t.cardNo, tradeType, tradeAmount, targetCard, poundage, tradeTime, acceptBankID, atmNo FROM Trade t JOIN BankCard b on t.cardNo = b.cardNo JOIN Customer c on b.custId = c.custId WHERE name = ? Parameters: [xiaoxiao]
at org.apache.commons.dbutils.AbstractQueryRunner.rethrow(AbstractQueryRunner.java:363)
at org.apache.commons.dbutils.QueryRunner.query(QueryRunner.java:350)
at org.apache.commons.dbutils.QueryRunner.query(QueryRunner.java:211)
at com.javaweb.bank.dao.impl.BaseDAO.queryForList(BaseDAO.java:123)
at com.javaweb.bank.test.TradeDAOImplTest.testGetTradesWithName(TradeDAOImplTest.java:61)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
........
..... 展开
String sql = "SELECT tradeId, t.cardNo, tradeType, tradeAmount, targetCard, poundage, tradeTime, acceptBankID, atmNo FROM Trade t JOIN BankCard b on t.cardNo = b.cardNo JOIN Customer c on b.custId = c.custId WHERE name = ? ";
错误信息:
java.sql.SQLException: com.microsoft.sqlserver.jdbc.SQLServerException: 列名 'name' 无效。 Query: SELECT tradeId, t.cardNo, tradeType, tradeAmount, targetCard, poundage, tradeTime, acceptBankID, atmNo FROM Trade t JOIN BankCard b on t.cardNo = b.cardNo JOIN Customer c on b.custId = c.custId WHERE name = ? Parameters: [xiaoxiao]
at org.apache.commons.dbutils.AbstractQueryRunner.rethrow(AbstractQueryRunner.java:363)
at org.apache.commons.dbutils.QueryRunner.query(QueryRunner.java:350)
at org.apache.commons.dbutils.QueryRunner.query(QueryRunner.java:211)
at com.javaweb.bank.dao.impl.BaseDAO.queryForList(BaseDAO.java:123)
at com.javaweb.bank.test.TradeDAOImplTest.testGetTradesWithName(TradeDAOImplTest.java:61)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
........
..... 展开
2个回答
展开全部
你是不是Trade 表,BankCard 表,Customer 表中不止一个字段为name?要么就是你表中没有name字段。建议你用 别名.name=?这样的话数据库系统就知道你用哪个表的哪个字段做匹配。
追问
就只有一个Customer表里有name ,别名也试过了,还是有错。说是—— 无法绑定由多个部分组成的标识符 "c.name"
debug过了也没看见sql语句有什么问题--!
终于找到问题了!把Customer放最前边,也就是在FROM后面!只是不知道为什么
追答
那我也就想不太通了,我刚刚自己建了三张表,在mysql上亲自测试了以下没有问题。以下是sql语句,表示没问题,明天上班再用oracle试试,我记得我写这样的sql也有好几次,oracle也没问题。
SELECT *
FROM student s join class c on c.id=s.classid join lession l on l.lessionid = s.lessionId
where l.lessionname='Chinese';
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询