如何从while循环中打印字符串值?

时间:2014-04-26 04:57:45

标签: java jdbc

您好我正在尝试从数据库中获取值并在控制台上打印所有值。当我在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();
        }
    }
}

我怎样才能得到我想要的出局?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:-1)

试试这段代码。希望这是你想得到的!

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();
        }
    }
}

建议:improve your code convention