gpt4 book ai didi

java - 在mysql中通过java数组计算列中的行数

转载 作者:行者123 更新时间:2023-11-29 12:19:47 24 4
gpt4 key购买 nike

我有一个问题。

我的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);
}
}

数据库( super 简单):

    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。因此,如果提供日期 6/5/2015 和 8/5/2015,我需要能够知道这是 1 个正常值和 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);

...

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

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

祝你好运。

关于java - 在mysql中通过java数组计算列中的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29199635/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com