0

I do not speak English well

An error occurred while doing JDBC.

I knew during coding that ' executequery ' should only be done by ' select ' in the database, and ' insert ' should be ' executeupdate '.

However, when I did executeupdate, I had a problem with the variable and changed it to " int "

Then an error occurred.

How can it be carried out as I intended?

I'll show you the code.

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class EdgeCloud {
    static ArrayList<String> deleteDB = new ArrayList<>();      
    static ArrayList<String> DBCarIP = new ArrayList<>();       
    static ArrayList<String> ContainerCarIP = new ArrayList<>();
    static String carSSID = "192.0.1";                              
    static String state;                                        
    static String upDateInputContainer;                         
    static String upDateInputCar;                               
    static int i = 0;                                           
    static String cIP;                                                                                              

    //-------------------------------------------(    main start   )----------------------------------------------------//
    public static void main(String[] args) throws Exception {
System.out.println("main start");
        while(true) {                           
//              JDBC(state="check");
                if(carSSID!=null) {                                 
                    makeContainer();                                
            }else {
                UPDATE();                                       
            }
        }
    }
    //-------------------------------------------(    main end    )----------------------------------------------------//


    //-------------------------------------------(    make container start   )----------------------------------------------------//
    public static void makeContainer() throws Exception {
        System.out.println("mc start");

        String makeContainer = "/usr/local/bin/docker run --name "+carSSID+" ubuntu:16.04"; 
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec(makeContainer);                              
        InputStream is = process.getInputStream();                                      
        InputStreamReader isr = new InputStreamReader(is);                                  
        BufferedReader br = new BufferedReader(isr);                                        
        String line;

        while((line = br.readLine()) != null) {                                             

            System.out.println(line);                                                   
        }

        String ipload = "/usr/local/bin/docker inspect -f {{.NetworkSettings.IPAddress}} "+carSSID; 

        IPLoad(ipload);                                             
    }

//-------------------------------------------(    make container end    )----------------------------------------------------//


//-------------------------------------------(    get container ip start   )----------------------------------------------------//
    public static void IPLoad(String ipload) throws Exception {
        System.out.println("ipload start");

        upDateInputCar = null;                                      
        upDateInputContainer = null;                                
        upDateInputCar = carSSID;                                   
        carSSID = null;                                             
        state = "insert";                                           
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec(ipload);                     
        InputStream is = process.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        while((cIP = br.readLine()) != null) {                      
            upDateInputContainer= cIP;                              
        System.out.println("cip : "+cIP);
        }
        System.out.println("upd"+upDateInputContainer);
        JDBC(state);                                                
    }

//-------------------------------------------(    get container ip end    )----------------------------------------------------//


//-------------------------------------------(    update start    )----------------------------------------------------//
    public static void UPDATE() throws Exception {                                  
        state = "check";                                                        
        JDBC(state);                                                                
        String getContainerName = "/usr/local/bin/docker ps --format {{.Names}}";   
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec(getContainerName);                           
        InputStream is = process.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line;

        while((line = br.readLine()) != null) {                                     
            ContainerCarIP.add(line);                                               
        }
        deleteDB.addAll(DBCarIP);                                                   
        deleteDB.removeAll(ContainerCarIP);                                         

        if(deleteDB.get(0)!=null) {                                                 

            state="delete";                                                     
            while(deleteDB.get(i++)!=null) {                                    
                //JDBC(state);                                                      
            }
            i = 0;                                                              
        }
    }

//-------------------------------------------(    update end    )----------------------------------------------------//


//-------------------------------------------(    socket start    )----------------------------------------------------//

/*socket place
-
-
-
-
-
-
*/

//-------------------------------------------(    soket end    )----------------------------------------------------//


//-------------------------------------------(    JDBC start    )----------------------------------------------------//
    public static void JDBC(String state){ 
        System.out.println("JDBC start");
        Connection conn = null ;  
        Statement stmt = null;  
        String query = null;

        if(state=="insert") {           
            query = "insert into inpo (ip, carid) values ('"+upDateInputContainer+"', '"+upDateInputCar+"')";   
            System.out.println(query);
        }

        if(state=="delete") {
            query = "delete from inpo where carid = " +deleteDB.get(i);                                     
    }

    if(state=="check") {
        query = "select carid from inpo";                                                           
    }

    try{
        Class.forName("com.mysql.cj.jdbc.Driver").newInstance();  
    }
    catch(ClassNotFoundException e ){  
        System.out.println( "드라이버 연결 에러." ) ;  
    }
    catch(Exception etc) {  
        System.out.println(etc.getMessage());  
    }
    try{
//          String url = "jdbc:mysql://192.168.44.122:3306/ssidIp?serverTimezone=UTC&autoReconnect=true&useSSL=false";  
        String url = "jdbc:mysql://localhost:3306/ssidIp?serverTimezone=UTC&autoReconnect=true&useSSL=false";  
        String userPass = "123456789" ;  
        conn = DriverManager.getConnection(url, userId, userPass);  
        stmt = conn.createStatement();  
        ResultSet rs = null;  
        int rss = 0;
        if(state=="check") {
            rs = stmt.executeQuery(query);  
        }else {
            rss = stmt.executeUpdate(query);  
        }//////////////////////////////////////////////////////
        if (state=="check") {
            rs = stmt.getResultSet();  
        }
        System.out.println("------------------------------------");
        System.out.println("debug1 ");

        if(state=="check") {                                
            System.out.println("debug 2");
            while (rs.next()) {                                     
                String str = rs.getNString(1);  
                DBCarIP.add(str);                               
            }
        }else {
            System.out.println("debug 3");

             while(rs.next()) {                                     // here NullpointerException 
                System.out.println("debug 4");
                String str = rs.getNString(1); 
                System.out.println(str);
            }
        }

        System.out.println("연결");  
        stmt.close();  
        conn.close();
        }
        catch( SQLException e ){  
            System.out.println( "SQLException : " + e.getMessage() ) ;  
        }       
    }
}    
        //-------------------------------------------(    JDBC end    )----------------------------------------------------//

ERROR part

main start
mc start
ipload start
cip : 172.17.0.3
upd172.17.0.3
JDBC start
insert into inpo (ip, carid) values ('172.17.0.3', '192.0.1')
------------------------------------
    debug1 
    debug 3
Exception in thread "main" java.lang.NullPointerException
    at edgeCloud.EdgeCloud.JDBC(EdgeCloud.java:195)
    at edgeCloud.EdgeCloud.IPLoad(EdgeCloud.java:86)
    at edgeCloud.EdgeCloud.makeContainer(EdgeCloud.java:61)
    at edgeCloud.EdgeCloud.main(EdgeCloud.java:33)
user10147267
  • 43
  • 1
  • 5
  • should you use `rs` or `rss` ? Which is populated with the resultset ? – Subbu Jul 29 '18 at 11:34
  • Off topic: Do yourself a favor and split the method `JDBC(String)` into 3 methods, one for each query. Your code will be easier to understand and easier to maintain. – Joakim Danielson Jul 29 '18 at 11:38

1 Answers1

0

You’re reading from rs instead of rss but rs is null for an insert or delete query.

else {
    System.out.println("debug 3");

    while(rs.next()) {                                     
        System.out.println("debug 4");
        String str = rs.getNString(1); 
        System.out.println(str);
    }
}

You need to change this to something like this since rss will contain the number of rows inserted or deleted.

else {
    System.out.println("debug 3");

    if (rss > 0) {                                     
        System.out.println("debug 4"); 
        //...
    }
Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52