title: How do I connect to a remote Cassandra instance running in Docker to retrieve data using Java?
body: <p>I'm trying to connect to a remote machine which has docker installed. I want to exec into the docker container and connect to database and then fetch a table data using java.</p>
  <p>Following are my commands that i'm trying to execute.</p>
  <p>docker exec it containerID - to login to docker container
  cqlsh -u username -p password -- to connect to cassandra DB
  use keyspace; ---to connect to cassandra keyspace
  desc tables; --- to view the tables that are available in keyspace.</p>
  <p>Following is the Java code that I'm trying. Can someone let me know if this approach is correct or what should i do to make this code work. I'm completely new to java code.</p>
  <pre><code>import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.util.Properties;
  
  import com.jcraft.jsch.Channel;
  import com.jcraft.jsch.ChannelExec;
  import com.jcraft.jsch.JSch;
  import com.jcraft.jsch.JSchException;
  import com.jcraft.jsch.Session;
  
  public class ExecuteanyCommands {
  
      public static void main(String[] args) throws JSchException, InterruptedException, IOException {
  
              //JSch
              JSch jsch = new JSch();
              Session session = jsch.getSession(&quot;cambi&quot;, &quot;10.10.96.20&quot;, 22);
              session.setPassword(&quot;axone&quot;);
              Properties config = new Properties();
              config.put(&quot;StrictHostKeyChecking&quot;, &quot;no&quot;);
              session.setConfig(config);
              session.connect();
              
              
              Channel channel = session.openChannel(&quot;exec&quot;);
              
              InputStream in = channel.getInputStream();
              ((ChannelExec) channel).setCommand(&quot;docker exec -it containerID; cqlsh -u username -p password; use keyspace; desc tables;&quot;);
              
              channel.connect();
              
              BufferedReader reader = new BufferedReader(new InputStreamReader(in));
              
              String line;
              
              while((line = reader.readLine()) != null) {
                  System.out.println(line);
              }
              channel.disconnect();
              session.disconnect();
      }
  
  }
  </code></pre>