0

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. But I'm getting a exception anyone knows why because my query is valid if I'm right.

I use eclipse and mysql workbench.

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.

database structure:

enter image description here

Thank you in advance,

Casper

9
  • 4
    you forget rs.next() Commented Sep 24, 2015 at 13:29
  • Where did I forgot that? I use rs.get() I don't need next right bc it's just one row. Or am i wrong? Commented Sep 24, 2015 at 13:32
  • 1
    Right after "ResultSet rs = stmt.executeQuery(sql);", insert "rs.next()" Commented Sep 24, 2015 at 13:33
  • 1
    @singhakash I think someone should copy the first comment to the Answer box. Commented Sep 24, 2015 at 13:37
  • 1
    I still got the exception that the column naam is note found Commented Sep 24, 2015 at 13:43

2 Answers 2

2

When you receive "name" through the nVal parameter, you select only ID and aNaam columns.

So, if you try to get values for naam from that ResultSet you get the 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.

Hope this helped.

Sign up to request clarification or add additional context in comments.

1 Comment

Dude you are my hero, it's true, I also need to select the column where I'm searching on Thank you very much
1

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.

In general, a good rule of thumb is to always return the same columns.

1 Comment

Yea it's true, I didn't think of it but it works now, the answer before u told me the same. Thank you for sharing you knowledge with me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.