SQLException : no such column

放肆的年华 提交于 2019-12-23 15:00:57

问题


I'm a new member. I have been struggling with sqlitejdbc, I thought. I made a query to sqlite database from a java program.I got above exception. My query is

select * 
from (  person as p 
        inner join company as c on p.p_id=c.p_id
     ) 
     inner join contact as ct on p.p_id=ct.p_id 
 where p.p_id=?;

When I put the query on navicatLite editor by placing p.p_id='1' instead of p.p_id=? , I was fine. It showed me correct values.

But from my java program. I got this exception.

Exception in thread "main" java.sql.SQLException: no such column: p.p_id
        at org.sqlite.DB.throwex(DB.java:288)
        at org.sqlite.NativeDB.prepare(Native Method)
        at org.sqlite.DB.prepare(DB.java:114)
        at org.sqlite.PrepStmt.(PrepStmt.java:37)
        at org.sqlite.Conn.prepareStatement(Conn.java:231)
        at org.sqlite.Conn.prepareStatement(Conn.java:224)
        at org.sqlite.Conn.prepareStatement(Conn.java:213)
        at programTest.test.main(test.java:19)
Java Result: 1

I'm sure there exists the person table and p_id field. I've searched this kind of question on this site but I found the one which is related to ruby on rails, not java. I have no idea what's wrong.

My java program is

import java.sql.*;

/**
 *
 * @author Htet 101
 */
public class test {

    public static void main(String[] args) throws Exception {
        Class.forName("org.sqlite.JDBC");
        Connection conn =
                DriverManager.getConnection("jdbc:sqlite://C://Users//Htet 101//Documents//addressbook.s3db");
        PreparedStatement stat = conn.prepareStatement("select * from (person as p inner join company as c on p.p_id=c.p_id) inner join contact as ct on p.p_id=ct.p_id where p.p_id=?;");
        stat.setInt(1, 1);

        ResultSet rs = stat.executeQuery();
        while (rs.next()) {
            System.out.print("Name : " + rs.getString("p_name") + " ");
        }
        rs.close();
        conn.close();
    }
}

I'm developing it using NetBeans 6.9.1, SQLite 3.7.8, SQLiteJDBC v056.


回答1:


I'm not absolutely sure, but I think it should work if you remove brackets from From : select * from person as p inner join company as c on p.p_id=c.p_id
inner join contact as ct on p.p_id=ct.p_id where p.p_id=?;




回答2:


The brackets are likely interpreted as if you are beginning what is called an inline view in Oracle - basically a dynamically created view. So I don't think it is a bug in the driver.




回答3:


If the exact same query goes fine with navicat, and wrong with java, it means the driver is buggy. But make sure it is exactly the same.

Also, those brackets are suspicious - get rid of them.



来源:https://stackoverflow.com/questions/8112936/sqlexception-no-such-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!