有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

mysql中按java数组计算列中的行数

我有个问题

我的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类中数组中所选日期的价格。我需要知道正常和ceap有多少。如果提供了日期为2015年6月5日和2015年8月5日的软件,我需要能够知道这是1个正常值和1个便宜值

字符串检查应该是一个字符串类型的数组

有人能帮我解决这个问题吗

问候,, 瑞克


共 (1) 个答案

  1. # 1 楼答案

    也许你需要使用一个映射,你可以使用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);
    
    ...
    

    通过这种方式,您可以知道字符串的类型

    我希望这些信息对你有帮助

    祝你好运