gpt4 book ai didi

java - 在 mysql java 中循环 - 搜索模板

转载 作者:行者123 更新时间:2023-11-29 08:42:36 25 4
gpt4 key购买 nike

我在 mysql 中有一个表,其中包含一个名为 template 的字段。我有一个模板存储在变量中,我需要将该变量与表中找到的每个其他模板进行比较,直到找到匹配项。我不知道如何一次从数据库行检索一个模板,进行比较,如果不匹配,则移动到下一行再次比较,依此类推,直到找到匹配。我是 mysql 循环的新手,请帮忙。

Connection con = null;

try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "");
PreparedStatement st;
st = con.prepareStatement("select template from tbl1 ");

ResultSet result = st.executeQuery();

while (result.next()) {

String dbTemplate = result.getString("template");
if x == dbtemplate

} else {// move to next row? How to say do the loop for next row in table??


}

最佳答案

让 SQL 帮你检查一下,试试这个:

st = con.prepareStatement("select template from tbl1 where template = ?");
st.setString(1, "template_name");

那么您只会得到模板匹配的结果

关于java - 在 mysql java 中循环 - 搜索模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13386737/

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