Java:简单如果Statement没有执行

时间:2014-07-20 12:31:46

标签: java mysql concurrency

好吧,所以这让我惹恼了几个小时。我不明白为什么这个if语句没有执行。这是我的完整代码。有2个班级,'客户'是主类,另一个是连接器类。

package com.app.client;

import java.net.ServerSocket;
import java.sql.SQLException;

public class Client{

    private int IntervalForDatabase=60000;
    private int IntervalForIPCheck=180000;
    private boolean AppRunning=true;
    public boolean ConnectionSuccess=false;
    private Connector ConnectionTools = new Connector();

    public static void main(String args[]){
        Thread thread = new Thread(new Runnable(){
            public void run(){
                Client client = new Client();
            }
        });
        thread.start();
    }

    Client(){
        /* Check For Current Instance Of Program */
        try{
            ServerSocket ApplicationSocket = new ServerSocket(6251);
        }catch(Exception e){
            System.exit(0);
        }

         Thread Network = new Thread(new Runnable(){
            public void run(){
                while(!ConnectionSuccess){
                    try{
                        ConnectionTools.ConnectToDatabaseServer();
                        ConnectionSuccess=true;
                        System.out.println("bein a bit");
                    }catch(Exception e){

                    }
                }
            }
        });
        Network.start();

        Thread LiveConnectionCheck = new Thread(new Runnable(){
            public void run(){
                while(AppRunning){
                    if(ConnectionSuccess){
                     /* THIS IF STATEMENT IS NOT EXECUTING */
                        try {
                            System.out.println("tester");
                            Thread.sleep(IntervalForIPCheck);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }           
        });

        LiveConnectionCheck.start();
    }
}

连接器类:

package com.app.client;

import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Connector {
    public boolean DatabaseConnection=false;
    Socket ClientSocket;
    private String DatabaseHost="localhost";
    private String DatabaseUsername="root";
    private String DatabasePassword = "";
    private String DatabaseName = "spyware";
    private String TableName="activespywares";
    public String CurrentServerIP = "";
    private Connection MYSQLConnection = null;
    public ResultSet result;

    public void ConnectToDatabaseServer(){
        try{
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            MYSQLConnection = DriverManager.getConnection("jdbc:mysql://"+DatabaseHost+"/"+DatabaseName,DatabaseUsername,DatabasePassword);
            DatabaseConnection=true;            
            System.out.println("connected");
        }catch(Exception e){
            System.out.println(e);
            DatabaseConnection=false;
            return;
        }
    }

    public String ObtainServerIP() throws SQLException{
        String text = "";           
        try{
            System.out.println("we've made it till here");
            PreparedStatement state = MYSQLConnection.prepareStatement("SELECT * FROM `activespywares`");
            result = state.executeQuery();
            System.out.println("we've made it till here");
            if(result.next()){
                result.next();
                text = result.getString("InternetProtocol");
            }
            return text;
        }catch(Exception e){
            System.out.println(e);
            text=e+"";
        }
        return text;
    }
}

我检查了一切。我不知道这里有什么问题。 if语句位于'客户端'类。连接器类只是让您知道它不会导致问题。

3 个答案:

答案 0 :(得分:3)

connectionSuccess设为volatile或更改为AtomicBoolean(并标记为final)。问题是您将变量设置为一个Thread但是从另一个变量中读取它。

答案 1 :(得分:1)

        if(result.next()){
            result.next();

毫无意义。当你在这里调用result.next()两次时,你会抛出第一个结果。

答案 2 :(得分:1)

对于您的例外,我建议使用

 e.printStackTace()

如果您的程序出现问题。

现在如果你执行

    //...your code
    if(result.next()){
        result.next();
    //...your code

它会抛弃你的第一个结果并向你展示你的第二个结果。我建议您使用

    //...your code
    while(result.next()){

    //...your code

随意滚动您的记录