0
    package canlitahmin;

    import java.sql.*;
    import java.util.ArrayList;
    import java.util.List;

    public class baglanti {
       // JDBC driver name and database URL
       static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
       static final String DB_URL = "jdbc:mysql://localhost:3306/canlitahmin";

       //  Database credentials
       static final String USER = "root";
       static final String PASS = "";
       public static List<Integer> id = new ArrayList<Integer>();
       public static List<Integer> evgol = new ArrayList<Integer>();
       public static List<Integer> kuralid = new ArrayList<Integer>();
       public static List<String> kural = new ArrayList<String>();
       public static List<Integer> depgol = new ArrayList<Integer>();
       public static List<Integer> dakika = new ArrayList<Integer>();

       public static void main(String[] args) {


       Connection conn = null;
       Statement stmt = null;
       Statement stmt2 = null;
       try{
          //STEP 2: Register JDBC driver
          Class.forName("com.mysql.jdbc.Driver");

          //STEP 3: Open a connection
          System.out.println("Connecting to database...");
          conn = DriverManager.getConnection(DB_URL,USER,PASS);

          //STEP 4: Execute a query
          System.out.println("Creating statement...");
          stmt = conn.createStatement();
          stmt2 = conn.createStatement();
          String sql;
          String sql2;
          sql = "SELECT id, evgol, depgol, dk FROM maclar";
          sql2="SELECT id,kural from kurallar";
          ResultSet rs = stmt.executeQuery(sql);
          ResultSet rs2 = stmt2.executeQuery(sql2);
          int i=0;
          //STEP 5: Extract data from result set
          while(rs.next()){
             //Retrieve by column name
             id.add(rs.getInt("id"));
             evgol.add(rs.getInt("evgol"));
             depgol.add(rs.getInt("depgol"));
             dakika.add(rs.getInt("dk"));

             //Display values
             System.out.print("ID: " + id.get(i));
             System.out.print(", Evgol: " + evgol.get(i));
             System.out.print(", Depgol: " + depgol.get(i));
             System.out.println(", dakika: " + dakika.get(i));
             i++;
          }
          int k=0;
          while(rs2.next()){
              //Retrieve by column name
              kuralid.add(rs2.getInt("id"));
              kural.add(rs2.getString("kural"));

              //Display values
              System.out.print("KURALID: " + kuralid.get(k));
              System.out.println(", KURAL: " + kural.get(k));
              k++;
           }

          for(int l=0;l<id.size();l++){
              int BYTG=evgol.get(l);
             int DEPTG=depgol.get(l);

            /* int DK=dakika.get(l);
             int MACKODLARI=id.get(l);*/

             for(int j=0;j<kuralid.size();j++){

            ###if(kural.get(j))###{ // ERROR**********************************
                double a=BYTG+DEPTG+0.5;
                int b=BYTG+DEPTG;
                String kural="Tahmin:"+a+" üstü ve "+b+" üstü";
                System.out.println(kural);
            }}
          }
          //STEP 6: Clean-up environment
          rs.close();
          rs2.close();
          stmt.close();
          stmt2.close();
          conn.close();
       }catch(SQLException se){
          //Handle errors for JDBC
          se.printStackTrace();
       }catch(Exception e){
          //Handle errors for Class.forName
          e.printStackTrace();
       }finally{
          //finally block used to close resources
          try{
             if(stmt!=null)
                stmt.close();
          }catch(SQLException se2){
          }// nothing we can do
          try{
             if(conn!=null)
                conn.close();
          }catch(SQLException se){
             se.printStackTrace();
          }//end finally try
       }//end try
       System.out.println("Goodbye!");
    }//end main
    }//end FirstExample

java if statement string comparison. my database datas get in "kural.get(j)" but kural.get(j) error. because its string variable. Question: String a=b>0 && c>0 -- if(a) how i use? String code in if with variable

cihad7887
  • 5
  • 1
  • 3
    You cannot dynamically execute code like that. That is not a thing you can do in Java. – Louis Wasserman Nov 04 '15 at 18:02
  • 2
    What are you trying to achieve with this approach? I suspect you're trying to solve a problem or establish an efficiency in coding which is achievable via some other common method or pattern. – lurker Nov 04 '15 at 18:06
  • You could so such a thing with JavaScript: the Nashorn engine runs in the JVM. Pass a JavaScript function and evaluate it. – duffymo Nov 04 '15 at 18:06
  • [Here an Example](http://stackoverflow.com/questions/3422673/evaluating-a-math-expression-given-in-string-form) - Nashorn in Java 8 – Husam Nov 04 '15 at 18:15
  • This shouldn't be down voted because this practice is very common in Javascript with the eval function: http://www.w3schools.com/jsref/jsref_eval.asp – Salvador Valencia Nov 04 '15 at 18:21
  • 1
    You've completely changed the question. Now the question makes no sense and the answers appear entirely unrelated. – Paul Boddington Nov 04 '15 at 18:29
  • Questions equal String a=c<1 && d<1. if(a) how i use? code in if with variable – cihad7887 Nov 04 '15 at 18:35

4 Answers4

1

This can be done programmatically with the JavaCompiler and the javax.tools package

As a related question, see How do I programmatically compile and instantiate a Java class?

The solution is the same.

Community
  • 1
  • 1
Hbas
  • 904
  • 6
  • 21
  • 1
    As a side note, see lurker comment on your question. Even though I think this is the answer to your **question**, depending on what you are trying to achieve this may not be the answer to your **problem** – Hbas Nov 04 '15 at 18:13
  • You can't get context from the surrounding program, though -- you can't expect i and j to get into the embedded program, really. – Louis Wasserman Nov 04 '15 at 18:16
  • Yeah... But even though you can't "get" context you can "give" context to the compiled instances with something like "setJ" and "setI" methods. I just hope that his code is just an example to ask how to evaluate strings, not his actual usage. – Hbas Nov 04 '15 at 18:25
  • Well, OP completely changed the question. Now the question makes no sense to me and my answer seems entirely unrelated. – Hbas Nov 04 '15 at 18:33
1

You cannot easily do that with Strings. What you can do is make an interface like this

interface IntIntPredicate {
    public boolean test(int i, int j);
}

Then you can do (in Java 8):

IntIntPredicate a = (i, j) -> i == 1 && j <= 2;
IntIntPredicate b = (i, j) -> i <= 0 && j == 2;

Then later you can do:

if (a.test(i, j)) {
    // do something
} else if (b.test(i, j)) {
    // do something else
}

This is possible in earlier versions of Java, but the syntax is more clumsy.

If it is necessary for the data to be entered as a String, it would probably not be too difficult to write a method to parse a String (treating i and j as the first and second arguments) and return an IntIntPredicate

public static IntIntPredicate parse(String x) {
    // This is going to require a lot of work, but
    // there are many questions on this site about how
    // to parse expressions such as "(2 + 3) * 9"
}
Paul Boddington
  • 37,127
  • 10
  • 65
  • 116
0

You could make a method, like:

boolean predicate(i,j) {
    if (i==1 and j <=2) {
        return true;
    }
    return false;
} 

And then invoke the method like this:

if (predicate(i,j)) {
    System.out.println("a");
}
0

You can also used ScriptEngine, but you need to translate the logic to a scripting language like Javascript.

import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
import javax.script.ScriptException;

/**
 *
 * @author afshin
 */
public class Blah {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws ScriptException {

        ScriptEngineManager mgr = new ScriptEngineManager();
        ScriptEngine engine = mgr.getEngineByName("JavaScript");
        int i = 1;
        int j = 1;
        String a = i + "==1 && " + j +"<=2";
        String b= i + "<=0 && " + j+"==2";

        if (((Boolean) engine.eval(a)))
            System.out.println("a is true");
         if (((Boolean) engine.eval(b)))
            System.out.println("b is true");

    }

}
Afshin Moazami
  • 2,092
  • 5
  • 33
  • 55