Java 如何打印while循环外的字符串值?

Java 如何打印while循环外的字符串值?,java,jdbc,Java,Jdbc,嗨,我正在尝试从数据库中获取值,并在控制台上打印所有值。当我在while循环中打印值时,它起作用,并将所有值作为字符串给出,但当我尝试打印while循环中的所有值时,它只给出最后一个值。如何将所有值作为字符串从循环中导出 这是我的密码 import java.sql.*; public class commoditywise { public static void main(String args[]) { String s=null; Stri

嗨,我正在尝试从数据库中获取值,并在控制台上打印所有值。当我在while循环中打印值时,它起作用,并将所有值作为字符串给出,但当我尝试打印while循环中的所有值时,它只给出最后一个值。如何将所有值作为字符串从循环中导出

这是我的密码

import java.sql.*;
public class commoditywise {
    public static void main(String args[])
    {
        String s=null;
        String id = "paddy";
        String driverName = "com.mysql.jdbc.Driver";
        String connectionUrl = "jdbc:mysql://localhost:3306/";
        String dbName = "mandi";
        String userId = "root";
        String password = "";
        String market=null;
        String rate=null;
        try {
            Class.forName(driverName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try{ 
            connection = DriverManager.getConnection(connectionUrl+dbName, userId, password);
            statement=connection.createStatement();
            String sql = "select ch.mandihindi,cw.price from commoditywise cw inner join mandihindi ch on ch.mandieng=cw.mandi where cw.commodity=  '"+id+"'";

            resultSet = statement.executeQuery(sql);
            while(resultSet.next()){
                market=resultSet.getString("mandihindi");
                rate=resultSet.getString("price");

                market=market.concat("~");
                rate=rate.concat("|");

                s=market.concat(rate);
                s=s.replaceAll(" ", "");
            }
            System.out.println(s);
        }

        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

如何获得所需的输出?

您可以在读取每个值时将其放入数组中,然后从数组中打印出来。

试试下面的代码。希望这就是你想要的

import java.sql.*;

public class Commoditywise {
    public static void main(String args[]) {
        String s = null;
        String id = "paddy";
        String driverName = "com.mysql.jdbc.Driver";
        String connectionUrl = "jdbc:mysql://localhost:3306/";
        String dbName = "mandi";
        String userId = "root";
        String password = "";
        String market = null;
        String rate = null;
        try {
            Class.forName(driverName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            connection = DriverManager.getConnection(connectionUrl + dbName,
                    userId, password);
            statement = connection.createStatement();
            String sql = "select ch.mandihindi,cw.price from Commoditywise cw inner join mandihindi ch on ch.mandieng=cw.mandi where cw.commodity=  '"
                    + id + "'";

            resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
                market = resultSet.getString("mandihindi");
                rate = resultSet.getString("price");
                s += market.concat("~").concat(rate).concat("|");
            }
            s = s.replaceAll(" ", "").substring(0, s.length() - 1);
            System.out.println(s);
        }

        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

建议:

@LifeRunsOnCode谢谢你的编辑你能帮我吗?flagged:不清楚你在问什么,问题可以通过一些非常基础的研究工作来解决。你能给我一些小代码吗