Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对于循环赢得';t运行正确的次数-Java_Java_Database_Loops_Ssh_Stream - Fatal编程技术网

对于循环赢得';t运行正确的次数-Java

对于循环赢得';t运行正确的次数-Java,java,database,loops,ssh,stream,Java,Database,Loops,Ssh,Stream,我有一个从未遇到过的奇怪问题。我有一个字符串的ArrayList,其大小通常大于800,具体取决于输入。但是,对于for-each循环或经典for循环,循环只运行3-5次。 ArrayList由ssh输出行填充。我需要遍历输出,对字符串进行压缩,然后将它们放入数据库中 processLogs方法(发生问题的位置) 公共作废处理日志(列出日志){ 试一试{ Connection conn=DMConnector.getDMConnection(); PreparedStatement=conn.p

我有一个从未遇到过的奇怪问题。我有一个字符串的ArrayList,其大小通常大于800,具体取决于输入。但是,对于for-each循环或经典for循环,循环只运行3-5次。 ArrayList由ssh输出行填充。我需要遍历输出,对字符串进行压缩,然后将它们放入数据库中

processLogs方法(发生问题的位置)

公共作废处理日志(列出日志){
试一试{
Connection conn=DMConnector.getDMConnection();
PreparedStatement=conn.prepareStatement(“插入日志(收件人地址、状态、smtp交易、日志时间戳)值(?,,?)”;
System.out.println(logs.size());
字符串到_地址;
字符串状态;
字符串smtp\u transac;
字符串时间戳;
for(字符串日志:日志){
to_address=log.split(“”[0];
状态=log.split(“”[2];
smtp_transac=log.split(“>:”)[1];
时间戳=log.split(“”[0]+log.split(“”[1];
语句.设置字符串(1,至地址);
语句.设置字符串(2,状态);
声明.集合字符串(3,smtp_transac);
语句.setString(4,时间戳);
语句。executeUpdate();
System.out.println(“toAdd=“+to_address+”status=“+status+”smtp_transac=“+smtp_transac+”\n”);
}
语句。close();
康涅狄格州关闭();
}捕获(例外情况除外){
System.out.println(例如getMessage());
}
}
运行上述代码后,我只会在数据库中获得几行,而我的System.out只会打印那么多次。我还尝试将statement.addBatch()与statement.executeBatch()和statement.executeQuery()一起使用,但不使用铁砧

以下是从控制台获取SSH行的方法:

public List<String> getLogs() {


    String remoteHostUserName = "";
    String remoteHostPassword = "";
    String hostIP = "";
    List<String> results = new ArrayList<String>();

    JSch jsch = new JSch();
    try {

        Session session = jsch.getSession(remoteHostUserName, hostIP, 22);

        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);

        // Skip prompting for the password info and go direct...
        session.setPassword(remoteHostPassword);
        session.connect();

        String command = "hvmail_report -q all --last '1 min' -a --show-address --show-mtaid --show-time | sort";

        Channel channel = session.openChannel("exec");
        ((ChannelExec) channel).setCommand(command);

        ((ChannelExec) channel).setErrStream(System.err);

        InputStream in = channel.getInputStream();

        System.out.println("Connect to session...");
        channel.connect();

        byte[] tmp = new byte[1024];
        while (true) {
            while (in.available() > 0) {
                int i = in.read(tmp, 0, 1024);
                if (i < 0) {
                    break;
                }
                //System.out.println(new String(tmp, 0, i));

                Collections.addAll(results, new String(tmp, 0, i).split("\r\n|\r|\n"));
            }
            if (channel.isClosed()) {
                System.out.println("exit-status: " + channel.getExitStatus());
                break;
            }
            try {
                Thread.sleep(1000);
            } catch (Exception ee) {
                ee.printStackTrace();
            }
        }
        channel.disconnect();
        session.disconnect();

        if (in.markSupported()){
            in.reset();
        }


    } catch (Exception e) {
        System.out.println(e);
    }
    return results;
}
public List getLogs(){
字符串remoteHostUserName=“”;
字符串remoteHostPassword=“”;
字符串hostIP=“”;
列表结果=新建ArrayList();
JSch JSch=新的JSch();
试一试{
Session Session=jsch.getSession(remoteHostUserName,hostIP,22);
属性配置=新属性();
配置放置(“检查”、“否”);
session.setConfig(config);
//跳过密码信息提示,直接进入。。。
session.setPassword(remoteHostPassword);
session.connect();
String command=“hvmail_report-q all--last'1 min'-a--show address--show mtaid--show time | sort”;
Channel=session.openChannel(“exec”);
((ChannelExec)channel).setCommand(command);
((ChannelExec)channel.setErrStream(System.err);
InputStream in=channel.getInputStream();
System.out.println(“连接到会话…”);
channel.connect();
字节[]tmp=新字节[1024];
while(true){
while(in.available()>0){
inti=in.read(tmp,0,1024);
if(i<0){
打破
}
//System.out.println(新字符串(tmp,0,i));
Collections.addAll(结果,新字符串(tmp,0,i).split(“\r\n | \r | \n”);
}
if(channel.isClosed()){
System.out.println(“退出状态:+channel.getExitStatus());
打破
}
试一试{
睡眠(1000);
}捕获(异常ee){
ee.printStackTrace();
}
}
通道断开();
session.disconnect();
如果(在.markSupported()中){
in.reset();
}
}捕获(例外e){
系统输出打印ln(e);
}
返回结果;
}

我唯一的猜测是为什么会发生这种情况与getLogs()方法有关。

文档说明不能在PreparedStatement上调用addBatch,但您已经成功了?我做到了。我的公司使用的是Java1.6,所以这可能就是为什么它被允许的原因。我将把我的帖子改为executeUpdate(),因为我也可以这样做。在for循环的开头放一条System.out指令,并在循环之前打印日志数组的大小,以获得更多提示。@Berger在
PreparedStatement
上不能调用
addBatch()
是什么意思?方法是在那里定义的。你说得对,我正在查看语句的文档:
public List<String> getLogs() {


    String remoteHostUserName = "";
    String remoteHostPassword = "";
    String hostIP = "";
    List<String> results = new ArrayList<String>();

    JSch jsch = new JSch();
    try {

        Session session = jsch.getSession(remoteHostUserName, hostIP, 22);

        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);

        // Skip prompting for the password info and go direct...
        session.setPassword(remoteHostPassword);
        session.connect();

        String command = "hvmail_report -q all --last '1 min' -a --show-address --show-mtaid --show-time | sort";

        Channel channel = session.openChannel("exec");
        ((ChannelExec) channel).setCommand(command);

        ((ChannelExec) channel).setErrStream(System.err);

        InputStream in = channel.getInputStream();

        System.out.println("Connect to session...");
        channel.connect();

        byte[] tmp = new byte[1024];
        while (true) {
            while (in.available() > 0) {
                int i = in.read(tmp, 0, 1024);
                if (i < 0) {
                    break;
                }
                //System.out.println(new String(tmp, 0, i));

                Collections.addAll(results, new String(tmp, 0, i).split("\r\n|\r|\n"));
            }
            if (channel.isClosed()) {
                System.out.println("exit-status: " + channel.getExitStatus());
                break;
            }
            try {
                Thread.sleep(1000);
            } catch (Exception ee) {
                ee.printStackTrace();
            }
        }
        channel.disconnect();
        session.disconnect();

        if (in.markSupported()){
            in.reset();
        }


    } catch (Exception e) {
        System.out.println(e);
    }
    return results;
}