2015-11-04 163 views
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語句字符串比較。我的數據庫數據得到「kural.get(j)」 但kural.get(j)錯誤。因爲它的字符串變量。 問題:字符串a = b> 0 & & c> 0 - 如果(a)我如何使用?串碼是否與可變java if語句字符串比較

+3

您不能動態執行這樣的代碼。這不是你可以在Java中做的事情。 –

+2

你想用這種方法實現什麼?我懷疑你正試圖解決一個問題,或建立一個編碼的效率,這可以通過其他常見的方法或模式實現。 – lurker

+0

JavaScript可以這麼做:Nashorn引擎在JVM中運行。傳遞JavaScript函數並對其進行評估。 – duffymo

回答

1

這可以通過編程與JavaCompilerjavax.tools package

作爲一個相關的問題後,看到How do I programmatically compile and instantiate a Java class?

的解決方案是一樣的。

+1

作爲一個方面的說明,請參閱潛在的評論你的問題。即使我認爲這是你的**問題**的答案,取決於你試圖達到的目標可能不是你的**問題的答案** – Hbas

+0

你不能從周圍的程序中獲取上下文,儘管 - 你不能指望我和j進入嵌入式程序,真的。 –

+0

是的......但即使你不能「獲取」上下文,你仍然可以使用諸如「setJ」和「setI」方法的方式給已編譯的實例「賦予」上下文。我只是希望他的代碼只是一個問題,如何評估字符串,而不是他的實際用法。 – Hbas

1

你不能用String s輕鬆做到這一點。你可以做的是讓這樣的

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

的接口,那麼你可以做(​​在Java中8):

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

後來的後來,你可以這樣做:

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

這是可能的在早期版本的Java中,但語法更笨拙。

如果有必要的數據作爲String輸入,它可能不會太難寫分析的方法String(治療ij作爲第一個和第二個參數),並返回一個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" 
} 
0

你可以做的方法,如:

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

,然後調用這樣的方法:

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

您也可以使用ScriptEngine,但是您需要將邏輯轉換爲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"); 

    } 

}