简体   繁体   English

不接受Java JDBC查询

[英]Java JDBC query not accepted

Hey I'm making a little webapp and have a java file in it with a function what connects a db and fetches the data. 嘿,我正在制作一个小的webapp,并在其中包含一个Java文件,该文件具有连接数据库并获取数据的功能。 But I'm getting a exception anyone knows why because my query is valid if I'm right. 但是我遇到了一个例外,任何人都知道为什么,因为如果我说对了,我的查询就是有效的。

I use eclipse and mysql workbench. 我使用eclipse和mysql工作台。

Function: 功能:

import java.sql.*;

public class Functions {
    public void dbConn(String nVal, String inpVal){

        System.out.println("Running function...");

        if(nVal != null || inpVal != null){
            String sqlSerch;
            if(nVal.equals("name")){
                sqlSerch = "ID, aNaam FROM profiles WHERE naam = 'casper'";
            }else{
                sqlSerch = "naam, aNaam FROM profiles WHERE ID = " + inpVal;
            }

            //driver / db path
                final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
                final String DB_URL = "jdbc:mysql://localhost:3306/profile";
                //DB user&password
                final String USER = "root";
                final String PASS = "";
                //declare con & sql var
                Connection conn = null;
                Statement stmt = null;
                //register jdbc driver
                try{
                    Class.forName(JDBC_DRIVER);
                    //make a connection
                    conn = DriverManager.getConnection(DB_URL,USER,PASS);
                    //SQL Statement
                    stmt = conn.createStatement();
                    String sql = "SELECT "+ sqlSerch;
                    ResultSet rs = stmt.executeQuery(sql);

                    //Declareer variablen met data uit db
                    //int id = rs.getInt("ID");
                    String naam = rs.getString("naam");
                    String aNaam = rs.getString("aNaam");

                    System.out.println( naam + aNaam);


                    rs.close();
                    stmt.close();
                    conn.close();

                }catch(Exception e){
                    System.out.println(e);
                }


                System.out.println(" - " + nVal + " - " + inpVal);


        }
    }
}

exception: 例外:

java.sql.SQLException: Column 'naam' not found. java.sql.SQLException:找不到列“ naam”。

database structure: 数据库结构:

在此处输入图片说明

Thank you in advance, 先感谢您,

Casper 卡斯珀

When you receive "name" through the nVal parameter, you select only ID and aNaam columns. 通过nVal参数收到“名称”时,仅选择IDaNaam列。

So, if you try to get values for naam from that ResultSet you get the Exception . 因此,如果您尝试从该ResultSet获取naam的值,则会得到Exception

Also, I suggest limiting the results of your query to 1, since you use the WHERE clause with naam and ID , which seem to be not unique, unless there's some constraint not included in the screenshot. 另外,我建议将查询结果限制为1,因为您将WHERE子句与naamID一起使用 ,这似乎不是唯一的,除非屏幕截图中未包含某些约束。

Hope this helped. 希望这会有所帮助。

You branch and create your queries: 您分支并创建查询:

        if(nVal.equals("name")){
            sqlSerch = "ID, aNaam FROM profiles WHERE naam = 'casper'";
        }else{
            sqlSerch = "naam, aNaam FROM profiles WHERE ID = " + inpVal;
        }

Then regardless of the branch, you get your result set values: 然后,无论分支如何,都将得到结果集值:

        String naam = rs.getString("naam");
        String aNaam = rs.getString("aNaam");

But "naam" will not be in your "ID, aNaam" search. 但是“ naam”不会出现在您的“ ID,aNaam”搜索中。

In general, a good rule of thumb is to always return the same columns. 通常,一个好的经验法则是始终返回相同的列。

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

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