java上传数据到mysql

package Test0;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.huaban.analysis.jieba.JiebaSegmenter;
import com.huaban.analysis.jieba.JiebaSegmenter.SegMode;
import com.huaban.analysis.jieba.SegToken;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;

class Node {
	public int id;
	public int type;
	public String name;

	public Node(int id, int type, String name) {
		this.id = id;
		this.type = type;
		this.name = name;
	}
}

class Resource {
	public int id;
	public String name;

	public Resource(int id, String name) {
		this.id = id;
		this.name = name;
	}
}

public class Test00 {
	static Connection conn;
	static Statement st;

	public static void main(String[] args) {
		 insert();//插入数据
		//query();// 查询数据

	}

	public static Connection getConnection() {
		Connection con = null; // 创建用于连接数据库的Connection对象
		try {
			Class.forName("com.mysql.jdbc.Driver");// 加载Mysql数据驱动
			con = (Connection) DriverManager
					.getConnection("jdbc:mysql://192.168.1.125/cgdb?user=root&password=1314521&useUnicode=true&characterEncoding=utf-8");
		} catch (Exception e) {
			System.out.println("数据库连接失败" + e.getMessage());
		}
		return con;
	}

	public static void insert() {
		// String pathshell="/home/wjx/spider/baidu/java.sh";
//		String cmd = "sort /home/wjx/桌面/spider/SearchUrl_CSDN.txt | uniq >/home/wjx/桌面/spider/SearchUrl_CSDN_b.txt"
//				+ ";sort /home/wjx/桌面/spider/SearchUrl_OSchina.txt | uniq >/home/wjx/桌面/spider/SearchUrl_OSchina_b.txt"
//				+ ";sort /home/wjx/桌面/spider/SearchUrl_Sina.txt | uniq >/home/wjx/桌面/spider/SearchUrl_Sina_b.txt";
		String cmd = "sort /home/wjx/桌面/spider/SearchUrl_*.txt | uniq >/home/wjx/桌面/spider/All_b.txt";
		Process ps;
		try {
			ps = Runtime.getRuntime().exec(new String[] { "sh", "-c", cmd });
			// ps = Runtime.getRuntime().exec(pathshell);
			ps.waitFor();
			String path = "/home/wjx/桌面/spider/";
			File f = new File(path);
			String[] names = f.list(new FilenameFilter() {
				public boolean accept(File dir, String name) {
					return name.indexOf(".txt") != -1;// 等于-1,表示不存在
				}
			});
			for (String str : names) {
				//System.out.println(str);
				if (str.endsWith("_b.txt")){
					insert_b(path + str);}
					if (str.endsWith("_v.txt"))
						;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void insert_b(String path) {
		try {
			conn = getConnection(); // 首先要获取连接,即连接到数据库
			st = (Statement) conn.createStatement(); // 创建用于执行静态sql语句的Statement对象
			String usesql = "use cgdb";
			st.execute(usesql);
			int i = 0;
			InputStream in = new FileInputStream(path);
			InputStreamReader ir = new InputStreamReader(in);
			BufferedReader br = new BufferedReader(ir);
			String target = null;
			Pattern pattern = Pattern
					.compile("^\\{(.+)\\}\\{(\\d+)\\}\\{(\\d+)\\}\\{(.+)\\}$");
			while ((target = br.readLine()) != null) {

				Matcher matcher = pattern.matcher(target);
				matcher.find();
				String sql = "INSERT INTO Resource(name,extend,comment,url,type)"
						+ " VALUES (‘"
						+ matcher.group(1)
						+ "‘,‘"
						+ matcher.group(2)
						+ "‘,‘"
						+ matcher.group(3)
						+ "‘,‘"
						+ matcher.group(4) + "‘,‘2‘)";
				// String sql = new String(str.getBytes(), "utf-8");
				System.out.println(sql);
				st.executeUpdate(sql); // 执行插入操作的sql语句,并返回插入数据的个数
				i++;
			}
			System.out.println("向Resource表中插入 " + i + " 条数据");
			br.close();
			ir.close();
			in.close();
			conn.close(); // 关闭数据库连接
		} catch (Exception e) {
			System.out.println("插入数据失败:" + e.getMessage());
		}
	}

	/* 查询数据库,输出符合要求的记录的情况 */
	public static void query() {
		JiebaSegmenter segmenter = new JiebaSegmenter();
		conn = getConnection();
		try {
			st = (Statement) conn.createStatement();
			String usesql = "use cgdb";
			st.execute(usesql);

			String sql_n = "select * from Node";
			ResultSet rs_n = (ResultSet) st.executeQuery(sql_n);
			List<Node> nodelist = new ArrayList<Node>();
			while (rs_n.next()) {
				nodelist.add(new Node(rs_n.getInt("id"), rs_n.getInt("type"),
						rs_n.getString("name")));
			}
			rs_n.last();

			String sql_s = "select * from Resource";
			ResultSet rs_s = (ResultSet) st.executeQuery(sql_s);
			List<Resource> resourcelist = new ArrayList<Resource>();
			while (rs_s.next()) {
				resourcelist.add(new Resource(rs_s.getInt("id"), rs_s
						.getString("name")));
			}
			rs_s.last();

			System.out.println("最后的查询结果为:");
			Set<String> content = new HashSet<String>();  //无顺序,内容不可重复

			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			//Map nodemap=new HashMap();
			for (Node node : nodelist) {// 对每中算法进行 循环 获得关键字
				System.out.println(node.id + " " + node.type + " " + node.name
						+ "   ::");
				List<SegToken> segn = segmenter.process(node.name,
						SegMode.INDEX);
				//nodemap.put(node.id, segn);
				double n = segn.size();
				
				for (Resource resource : resourcelist) {
					List<SegToken> segs = segmenter.process(resource.name,
							SegMode.INDEX);
			   double i = 0;
				for (SegToken seg : segn) {
					if (seg.word.getToken().equals("-")) {
						n--;
						continue;
					}
						
						for (SegToken seg2 : segs) {
							if (seg.word.getToken().equalsIgnoreCase(
									seg2.word.getToken())) {
								i++;
								break;
							}
						}
					}
				
				if (i / n > 0.5) {
					System.out.print(i + "        " + n);
					content.add(node.id + "‘,‘" + resource.id);
					System.out.println("  "+node.name + "      " + resource.name);
				}
				}
				
//				
//				System.out.println();
//				System.out.println();
			}
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			Iterator<String> iter = content.iterator();
			while (iter.hasNext()) {
				String sql = "INSERT INTO NodeResource(node_id,resource_id)"
						+ " VALUES (‘" + iter.next() + "‘)";
				System.out.println(sql);
				st.executeUpdate(sql);
			}

			conn.close(); // 关闭数据库连接
		} catch (Exception e) {
			System.out.println("查询数据失败");
			e.printStackTrace();
		}
	}

	// /* 更新符合要求的记录,并返回更新的记录数目*/
	// public static void update() {
	// conn = getConnection(); //同样先要获取连接,即连接到数据库
	// try {
	// String sql = "update staff set wage=‘2200‘ where name = ‘lucy‘";//
	// 更新数据的sql语句
	//
	// st = (Statement) conn.createStatement();
	// //创建用于执行静态sql语句的Statement对象,st属局部变量
	//
	// int count = st.executeUpdate(sql);// 执行更新操作的sql语句,返回更新数据的个数
	//
	// System.out.println("staff表中更新 " + count + " 条数据"); //输出更新操作的处理结果
	//
	// conn.close(); //关闭数据库连接
	//
	// } catch (SQLException e) {
	// System.out.println("更新数据失败");
	// }
	// }
	//
	// /* 删除符合要求的记录,输出情况*/
	// public static void delete() {
	//
	// conn = getConnection(); //同样先要获取连接,即连接到数据库
	// try {
	// String sql = "delete from staff  where name = ‘lili‘";// 删除数据的sql语句
	// st = (Statement) conn.createStatement();
	// //创建用于执行静态sql语句的Statement对象,st属局部变量
	//
	// int count = st.executeUpdate(sql);// 执行sql删除语句,返回删除数据的数量
	//
	// System.out.println("staff表中删除 " + count + " 条数据\n"); //输出删除操作的处理结果
	//
	// conn.close(); //关闭数据库连接
	//
	// } catch (SQLException e) {
	// System.out.println("删除数据失败");
	// }
	//
	// }
}

/*
 * http://www.jb51.net/article/30774.htm
 * http://www.linuxidc.com/Linux/2012-04/58990.htm
 * http://dupeng.name/zz/the-centos-fully-uninstall-mysql.html
 * 
 * 对数据库的修改: 1. url改为text 2. name改为text 之前要将un去掉勾
 */

java上传数据到mysql,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。