2015-11-04 130 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"); 

    } 

}