简体   繁体   English

通过MySQL中的java数组计算列中的行

[英]count rows in column by java array in mysql

I have a problem. 我有个问题。

There is an array of strings in my java program. 我的java程序中有一个字符串数组。 In my database i have the strings as well , but they are in ether column a or column b. 在我的数据库中,我也有字符串,但是它们在以太列a或列b中。 I need to know in my java program if the element in my array is an element a or an element b. 我需要在我的Java程序中知道数组中的元素是元素a还是元素b。 I kind of have an blackout on how to do this. 我有点无法做到这一点。

I have the mysql connection with queries working, no problem there. 我有与查询工作的mysql连接,那里没有问题。

The java class currently is : 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);
        }
    }

Database (super simple): 数据库(超级简单):

    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 |
    +----------+----------+----------+

I need to calculate the prices of the selected dates in the array i have in a java class. 我需要计算我在java类中的数组中选定日期的价格。 I need to know how many of normal and ceap there are. 我需要知道有多少普通和盲肠。 Sof if date 6/5/2015 and 8/5/2015 are provided, i need to be able to know this is 1 normal and 1 cheap. 如果提供日期6/5/2015和8/5/2015,我需要能够知道这是1正常且1便宜。

String check is supposed to be a array of type string later when this works. 稍后,字符串检查应为字符串类型的数组。

Can someone help me please with this problem. 有人可以帮我解决这个问题。

Regards, Rick 问候,里克

Maybe you need to use a Map, you can use TreeMap or HashMap with another collection to store all values of each type of your Strings, it would be something like this: 也许您需要使用一个Map,可以将TreeMap或HashMap与另一个集合一起使用来存储每种类型的String的所有值,就像这样:

...

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

...

In this way you can know the type of your Strings. 这样,您就可以知道字符串的类型。

I hope this information helps you. 希望这些信息对您有所帮助。

Good Luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM