JDBC连接Mysql并统计指定关键词在某一列中出现的次数
2014-05-18 13:40
344 查看
1:先看一下效果
这个是原始内容数据:
这个是统计后的数据显示,初始都为0
这个是执行过程中的状态:
2、贴出代码
import java.util.regex.Pattern; import java.sql.*; public class Statistics { public static void main(String[] args) { statistics(); } public static Connection getConnection() throws SQLException,java.lang.ClassNotFoundException{ String url = "jdbc:mysql://localhost:3306/xici?useUnicode=true&characterEncoding=utf-8"; //连接xici数据库 Class.forName("com.mysql.jdbc.Driver"); //加载mysql支持包 String userName = "root"; //root用户 String password = "123"; //密码 Connection con = DriverManager.getConnection(url, userName, password); return con; } public static void statistics(){ String str[]=new String[]{“内容1”,"内容二","内容三"}; //关键词输入 System.out.println("开始了");//个人习惯 try{ String s="select * from xici_content"; //选择数据库中一个表 Connection con = getConnection(); Statement sql=con.createStatement(); Statement sql1=con.createStatement(); ResultSet res=sql.executeQuery(s); /*计算每个关键字在指定内容中出现的频率*/ while(res.next()){ //.next()见详解1 String contentString=res.getString("Content"); String id=res.getString("id"); //System.out.println(contentString); //供测试使用 int count=0,start=0,number=0; //初始化 System.out.println(str.length); //供测试 for(count=0;count<str.length;count++) { while ( contentString.indexOf(str[count],start)>=0 && start<contentString.length())//indexOf()见详解二 { number++; start=contentString.indexOf(str[count],start)+str[count].length(); } if(number>=0) { System.out.println("用户:"+res.getString("Username")+" "+str[count]+"出现次数:"+number); String si2="update xici_keyword set "+str[count]+"="+number+" where id="+id; System.out.println(si2); //程序可视化 sql1.execute(si2); //执行sql语句 } number=0; start=0; } } } catch(java.lang.ClassNotFoundException e){ //捕获到ClassNotFoundException异常 ,输出异常信息 System.err.println("ClassNotFoundException:" + e.getMessage()); } catch(SQLException ex){ System.err.println("SQLException:" + ex.getMessage()); //异常处理 } System.out.println("一切都结束了"); //个人习惯 } }
3:代码讲解
详解1:sql.next()
依次使第123456789....行成为当前行.最常用语句。详解2:indexOf()
public int indexOf(String str,int fromIndex)官方解释:Returns the index within this string of the first occurrence of the specified substring, starting at the specified index. The integer returned is the smallest valuek for which:
返回关键词str出现的位置,为int型。fromIndex为起始位置。这里通过
start=contentString.indexOf(str[count],start)+str[count].length();
来增量改变start,从而统计这一条内容中所有的的关键词次数。
PS:注意编码格式,昨天汉字的编码格式不相符,耽误了不少时间找错。个人水平较低,也许会有错误,故作此记录。
相关文章推荐
- mysql统计字段中某一字符串出现的次数
- mysql统计字段中某一字符串出现的次数
- 使用jdbc连接mysql 出现 java.sql.Exception: this specifer does...(root@'%')..doesn't exist.
- 连接数据库时出现java.lang.ClassNotFoundException: com.mysql.jdbc.Driver怎么修改
- awk以列为关键词统计出现次数
- K&R练习题6-1统计关键词出现的次数
- 在hadoop上进行编写mapreduce程序,统计关键词在text出现次数
- JDBC连接MySQL出现的问题
- 连接mysql的时候出现:java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
- tomcat使用jdbc连接mysql出现的错误
- STL 统计vector容器中指定对象元素出现的次数:count()与count_if()算法
- MySql中关于某列中相同数值连续出现次数的统计
- 使用powerdesigner连接数据库时出现Non SQL Error : Could not load class com.mysql.jdbc.Drive
- JDBC 连接mysql,存储出现中文乱码
- 【shell】linux统计某一文件中字符串“sent ip”出现的次数
- 使用powerdesigner连接数据库时出现Non SQL Error : Could not load class com.mysql.jdbc.Drive
- 统计某个字符串中指定字符串出现的次数
- awk统计文件中某关键词出现次数的命令
- 统计一个字符串在另一个字符串中出现的次数(Java mysql)
- 使用powerdesigner连接数据库时出现Non SQL Error : Could not load class com.mysql.jdbc.Drive