简体   繁体   English

JDBC:“SQLException:找不到列名”

[英]JDBC: “SQLException:Column name is not found”

I have tried to use these two questions as a reference我试图用这两个问题作为参考

  1. JAVA - SQLException throws exist column not found JAVA - SQLException throws exists column not found
  2. Strange SQLException: Column not found 奇怪的 SQLException:找不到列

However, none of it helped my case.但是,这对我的情况没有任何帮助。 I keep receiving java.sql.SQLException: Column 'debut' not found.我不断收到java.sql.SQLException: Column 'debut' not found.

this is my SQL这是我的 SQL

private List<MemberModel> getModelFromNickname(String test) throws SQLException {

        List<MemberModel> result = new ArrayList<>();
        String sql = "select distinct " +
                "m.name," +
                "m.visual," +
                "m.branch, " +
                "m.illustrator, " +
                "m.debut3D AS debut " +
                "FROM " +
                "member_list as m, " +
                "nickname AS n " +
                "WHERE n.nick_id = m.nick AND n.nickname LIKE ?";

        ResultSet ret= sqlAdapter.select(
                sql, 1,test
        );
        while (ret.next()){
            result.add(fillRecord(ret));
        }
        return result;
    }

this is my MYSQLAdapter这是我的 MYSQLAdapter

public ResultSet select(String sql,int option, Object... params) throws SQLException {
        PreparedStatement query;
        try{
            logger.info(params.length);
            query = getConnection().prepareStatement(sql);
        }
        catch (Exception e){
            throw new Error("Problem "+ e);
        }

        resolveParameters(query,option, params);
        return query.executeQuery();

    }

this is my fillRecord command这是我的 fillRecord 命令

private static MemberModel fillRecord(ResultSet resultSet) throws SQLException {
        MemberModel member = new MemberModel();
        member.name =resultSet.getString("name");
        member.branch = resultSet.getString("branch");
        member.debut_3d = resultSet.getBoolean("debut");
        member.visual = resultSet.getString("visual");
        member.illustrator = resultSet.getString("illustrator");
        return member;
    }

while this is my memberModel虽然这是我的 memberModel

public class MemberModel {
    public String name = "";
    public int social_media = 0;
    public int nick = 0;
    public String branch = "";
    public boolean debut_3d= true;
    public String illustrator ="";
    public String visual = "";

}

I tried to use我试着用

ResultSetMetaData rsmd = sqlAdapter.getConnection().prepareStatement(sql).getMetaData();
        for(int i =1; i<= rsmd.getColumnCount();i++){
            System.err.println(rsmd.getColumnCount()+"\n"+rsmd.getColumnName(i));
        }

which gives me the following (I don't know how it somehow managed to print more than 5. But, all of it except "debut" can be found)这给了我以下内容(我不知道它是如何设法打印超过 5 个的。但是,除了“debut”之外的所有内容都可以找到)

5
name
5
visual
5
branch
5
illustrator
5
debut
5
name
5
visual
5
branch
5
illustrator
5
debut

This indicates that the debut column should exist yet I still get that exception.这表明首次亮相的专栏应该存在,但我仍然得到那个例外。 Where did I do wrong?我哪里做错了? I make sure that it is case sensitive just in case.我确保它区分大小写以防万一。 The data type for "debut" column is boolean in my localhost XAMPP MySQL. “debut”列的数据类型是我的本地主机 XAMPP MySQL 中的 boolean。 If I erase member.debut_3d = resultSet.getBoolean("debut");如果我删除member.debut_3d = resultSet.getBoolean("debut"); within fillrecord ... Everything works perfectly finefillrecord内...一切正常

Rather get values by labelName trying column number.而是通过 labelName 尝试列号获取值。

private static MemberModel fillRecord(ResultSet resultSet) throws SQLException {
        MemberModel member = new MemberModel();
        member.name = resultSet.getString(1);
        member.branch = resultSet.getString(3);
        member.debut_3d = resultSet.getBoolean(5);
        member.visual = resultSet.getString(2);
        member.illustrator = resultSet.getString(4);
        return member;
    }
resultSet.getBoolean("debut3D"); // Instead of "debut"

The column in the database table is called "debut3D".数据库表中的列称为“debut3D”。

BTW you are not closing PreparedStatement and ResultSet.顺便说一句,您没有关闭 PreparedStatement 和 ResultSet。 I would use try-with-resources.我会使用 try-with-resources。

The column name is " debut3D " not " debut ", thats why you are getting the error.列名是“首次debut3D ”而不是“ debut ”,这就是您收到错误的原因。

Update below line with correct column name,使用正确的列名更新下面的行,

member.debut_3d = resultSet.getBoolean("debut3D");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM