【问题标题】:count rows in column by java array in mysql通过mysql中的java数组计算列中的行数
【发布时间】:2015-05-25 19:35:46
【问题描述】:

我有问题。

我的 java 程序中有一个字符串数组。 在我的数据库中,我也有字符串,但是 它们在以太列 a 或 b 列中。我需要 在我的 java 程序中知道我的元素是否 数组是元素 a 或元素 b。我善良 对如何执行此操作感到困惑。

我有 mysql 连接,查询工作正常,没有问题。

当前的java类是:

public static String getCountedDatesTypes(String check) throws ClassNotFoundException {
    String wholeDataModel = "";

    String dates;
    String myDriver = "org.mariadb.jdbc.Driver";
    Class.forName(myDriver);

    Connection con = null;
    Statement st = null;
    ResultSet rs = null;

    String url = "jdbc:mariadb://localhost:3306/house";
    String user = "root";
    String password = "supermanspassword";

    try {

        String tmpblack = "";
        String tmpnormal = "";
        String tmpcheap = "";

        con = DriverManager.getConnection(url, user, password);
        st = con.createStatement();

        String sqlQuery = String.format("select count(black) as black,count(normal) as normal ,count(cheap) as cheap from dates;");
        rs = st.executeQuery(sqlQuery);

        while (rs.next()) {
            String b = rs.getString("black");

            String n = rs.getString("normal");

            String c = rs.getString("cheap");

            System.out.println(b + " " + n + " " + c);

        }

    } catch (SQLException ex) {
        Logger lgr = Logger.getLogger(Mysql_interaction.class.getName());
        lgr.log(Level.SEVERE, ex.getMessage(), ex);

    } finally {
        try {
            if (rs != null) {

                rs.close();
            }
            if (st != null) {
                st.close();
            }
            if (con != null) {
                con.close();
            }

        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(Mysql_interaction.class.getName());
            lgr.log(Level.WARNING, ex.getMessage(), ex);
        }
    }

数据库(超级简单):

    MariaDB [house]> select * from dates;
    +----------+----------+----------+
    | black    | normal   | cheap    |
    +----------+----------+----------+
    | 5/5/2015 | NULL     | NULL     |
    | NULL     | 6/5/2015 | NULL     |
    | NULL     | NULL     | 7/5/2015 |
    | NULL     | NULL     | 8/5/2015 |
    +----------+----------+----------+

我需要计算我在 java 类中的数组中选定日期的价格。我需要知道有多少 normal 和 ceap 。因此,如果提供了 2015 年 6 月 5 日和 2015 年 8 月 5 日的日期,我需要能够知道这是 1 正常和 1 便宜。

字符串检查应该是稍后工作时的字符串类型数组。

谁能帮我解决这个问题。

问候, 瑞克

【问题讨论】:

  • 你能澄清你的问题吗?请发布您现在拥有的代码,并解释它如何不能满足您的要求。
  • 我晚上会澄清这个问题,请稍候。

标签: java mysql arrays


【解决方案1】:

也许您需要使用 Map,您可以将 TreeMap 或 HashMap 与另一个集合一起使用来存储您的字符串的每种类型的所有值,它会是这样的:

...

Map <String, List<String>> map = new HashMap <String, List<String>> ();

List<String> b= new ArrayList<String>();
List<String> n= new ArrayList<String>();
List>String> c= new ArrayList<String>();

String sqlQuery = String.format("select * from dates;");

rs = st.executeQuery(sqlQuery);

while (rs.next()) {
    b.add(rs.getString("black") != null ? rs.getString("black") : "");
    n.add(rs.getString("normal") != null ? rs.getString("normal") : "");
    c.add(rs.getString("cheap") != null ? rs.getString("cheap") : "");
}

map.put("b", b);
map.put("n", n);
map.put("c", c);

...

这样你就可以知道你的字符串的类型了。

希望这些信息对您有所帮助。

祝你好运。

【讨论】:

  • 谢谢!这会奏效。我今天会试试这个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-22
  • 2014-11-08
  • 1970-01-01
相关资源
最近更新 更多