1

I want to write a function that will execute different queries. I want it to pass the table name and the method of select, insert, and a list of values ​​with regard to where the like. I've done something similar in php and I handled everything in the array, but do not know how to solve in Java, there maybe some ready class?

My current solution in PHP:

$user = db::getInstance()->update('users', 2, array(
    'password' => 'newpassword'));

    public function update($table, $id, $fields){

        $set = '';
        $x = 1;

        foreach($fields as $name => $value){
            $set .= "{$name} = ?";

            if($x < count($fields)){
                $set .= ', ';       
            }
        }

        $sql = "UPDATE {$table} SET {$set} where id = {$id}";


        if(!$this->query($sql, $fields)->error()){
                    return true;
        }
    }
Nathan A
  • 11,059
  • 4
  • 47
  • 63
lukassz
  • 127
  • 1
  • 2
  • 12

2 Answers2

2

You can do nearly the same thing by using an Interface and passing an Implementation of that interface as a parameter

An abstract example:

An Interface

public interface MyInterface {
    public void doTheSelect();
}

Implementation A

public class MyImplementationA implements MyInterface{

    @Override
    public void doTheSelect() {
        // Do some sort of select
    }
}

Implementation B

public class MyImplementationB implements MyInterface{

    @Override
    public void doTheSelect() {
        // Do some other sort of select
    }
}

Your Class with your function/method

public class MyClass{

    public void myFunction(MyInterface implementation) {
        implementation.doTheSelect();
    }

}

The 'difference' is, that you have to think Object Oriented

dot_Sp0T
  • 389
  • 4
  • 26
  • More interested to me like przeazać parameters to a function in such a way as I have in php, because I have a Java class from your database and do not want anything to change. – lukassz Aug 12 '14 at 19:04
  • Well there's such a thing as language design and limitations. I assume in my answer and also now, that you are talking about function pointer, please see this article about it: http://stackoverflow.com/questions/1073358/function-pointers-in-java – dot_Sp0T Aug 12 '14 at 19:06
  • Ok, and how to pass an array to a function in an analogous manner to the 'password' => 'newpassword'? – lukassz Aug 13 '14 at 09:02
  • Well the 'key => value' operator seems to relay to an associative array. The data structure in Java closest to an associative array would probably be a Map – dot_Sp0T Aug 13 '14 at 12:50
1

This is my solution

  @Override
        public int insert(Map<String, String> valueMap, String table) throws SQLException { 
        // TODO Auto-generated method stub

            Connection conn = Database.getInstance().getConnection();

            String val = "";
            String query = "";
            String question = "";
            Integer mapsize = valueMap.size();
            Integer x = 1;

            for (Map.Entry<String, String> entry : valueMap.entrySet()) {

                val += entry.getKey();
                question += "?";

                if (x < mapsize) {
                    val += ", ";
                    question += ", ";
                }
                mapsize--;
            }

            query = "insert into " + table + "(" + val + ") values ("+question+")";
            PreparedStatement add = conn.prepareStatement(query);

            for(Map.Entry<String, String> entry : valueMap.entrySet()){

                add.setObject(x,entry.getValue());
                x+=1;
            }
            int addtotable = add.executeUpdate();
            add.close();

            return addtotable;
        }
lukassz
  • 127
  • 1
  • 2
  • 12