我在 NetBeans 上用 Java 编写了一段代码:
package helloworld;
import java.sql.*;
import java.io.*;
import java.lang.Object;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.functors.EqualPredicate;
import org.apache.lucene.queryParser.ParseException;
public class HelloWorld {
public static void main(String[] args) throws IOException, ParseException {
// TODO code application logic here
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String url = "jdbc:mysql://localhost:3306/kdd";
String user = "root";
String password = "Pass1234";
try {
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
String sqlQuery = "Select * from ad_contents";
rs = stmt.executeQuery(sqlQuery);
while (rs.next()) { //(rs.next())
int adID = rs.getInt(1);
String keywordTokens = rs.getString(2);
String titleTokens = rs.getString(3);
String descriptionTokens = rs.getString(4);
List<String> keywordList = Arrays.asList(keywordTokens.split("\\|", -1));
List<String> titleList = Arrays.asList(titleTokens.split("\\|", -1));
List<String> descriptionList = Arrays.asList(descriptionTokens.split("\\|", -1));
Set<String> distincts = new HashSet<String>();
distincts.addAll(keywordList);
distincts.addAll(titleList);
distincts.addAll(descriptionList);
int keywordHit = 0, titleHit = 0, descriptionHit = 0;
for (String distinctCounter : distincts) {
keywordHit = CollectionUtils.cardinality(distinctCounter, keywordList);
titleHit = CollectionUtils.cardinality(distinctCounter, titleList);
descriptionHit = CollectionUtils.cardinality(distinctCounter, descriptionList);
String intoQuery = "insert into ad_word_counts values (" + adID + "," + distinctCounter + "," + keywordHit + "," + titleHit + "," + descriptionHit + ")";
stmt.execute(intoQuery);
//node myNode= new node(adID, Integer.parseInt(distinctCounter) , keywordHit, titleHit, descriptionHit);
}
}
} catch (SQLException ex) {
} finally {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException ex) {
}
}
}
}
但是,对于rs结果集的第一行,它只进入while block 一次。因此,它只为 1 个 adID 写入数据。 “intoQuery”不适用于所有 rs 结果集行。我该如何处理?
最佳答案
那是因为您在 while 循环内的 for 循环中执行了另一个查询:-
for (String distinctCounter : distincts) {
// Create a new statement to execute query in while loop
Statement stmt1 = conn.createStatement();
stmt1.execute(intoQuery);
}
这将导致旧的结果集关闭。
看看:- ResultSet#close文档,上面写着:-
A ResultSet object is automatically closed by the Statement object that generated it when that Statement object is closed, re-executed, or is used to retrieve the next result from a sequence of multiple results.
因此,文档明确指出使用相同语句重新执行新查询将关闭 ResultSet 对象。
因此,您需要使用不同的语句实例来创建内部查询。
建议:-
理想情况下,您的上述代码会抛出一个 SQLException(请参阅 ResultSet#next),这是您不知道的,因为您在 catch block 中吞下了异常。
catch (SQLException ex) {
}
切勿使用空 catch block 。它没有用,除了它可以防止您的程序在运行时停止。但这不会有任何好处。
关于java - SQL 查询在 while 循环中不起作用 - JAVA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12915367/
我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht
这篇文章是继上一篇文章“Observability:从零开始创建Java微服务并监控它(一)”的续篇。在上一篇文章中,我们讲述了如何创建一个Javaweb应用,并使用Filebeat来收集应用所生成的日志。在今天的文章中,我来详述如何收集应用的指标,使用APM来监控应用并监督web服务的在线情况。源码可以在地址 https://github.com/liu-xiao-guo/java_observability 进行下载。摄入指标指标被视为可以随时更改的时间点值。当前请求的数量可以改变任何毫秒。你可能有1000个请求的峰值,然后一切都回到一个请求。这也意味着这些指标可能不准确,你还想提取最小/
HashMap中为什么引入红黑树,而不是AVL树呢1.概述开始学习这个知识点之前我们需要知道,在JDK1.8以及之前,针对HashMap有什么不同。JDK1.7的时候,HashMap的底层实现是数组+链表JDK1.8的时候,HashMap的底层实现是数组+链表+红黑树我们要思考一个问题,为什么要从链表转为红黑树呢。首先先让我们了解下链表有什么不好???2.链表上述的截图其实就是链表的结构,我们来看下链表的增删改查的时间复杂度增:因为链表不是线性结构,所以每次添加的时候,只需要移动一个节点,所以可以理解为复杂度是N(1)删:算法时间复杂度跟增保持一致查:既然是非线性结构,所以查询某一个节点的时候
1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里