zoukankan      html  css  js  c++  java
  • 通过java api 读取sql 中数据(查询)

    配置文件:dbconfig.properties 里面的数据

    jdbc.url.jwhat=jdbc:mysql://ip:3306/laibadev?useUnicode=true&characterEncoding=UTF-8
    jdbc.username.jwhat=
    jdbc.password.jwhat=
    jdbc.dbType=mysql
    

     table 表sql:

    INSERT INTO `bns_spider_user` VALUES ('http://toutiao.com/m4227761960/', NULL, NULL, NULL, 43, ' 娱乐八卦姐妹淘', NULL);
    INSERT INTO `bns_spider_user` VALUES ('http://toutiao.com/m3704154333/', NULL, NULL, NULL, 44, ' MC1890', NULL);
    INSERT INTO `bns_spider_user` VALUES ('http://toutiao.com/m5360794786/', NULL, NULL, NULL, 45, ' 八卦队长', NULL);
    INSERT INTO `bns_spider_user` VALUES ('http://toutiao.com/m3676970251/', NULL, NULL, NULL, 46, ' 娱乐圈电影解读', NULL);
    INSERT INTO `bns_spider_user` VALUES ('http://toutiao.com/m3345854756/', NULL, NULL, NULL, 47, ' 青春娱乐网', NULL);
    INSERT INTO `bns_spider_user` VALUES ('http://toutiao.com/m3178840731/', NULL, NULL, NULL, 48, ' 百街秀娱乐', NULL);
    INSERT INTO `bns_spider_user` VALUES ('http://toutiao.com/m4619040717/', NULL, NULL, NULL, 49, ' 时光机Studio', NULL);
    INSERT INTO `bns_spider_user` VALUES ('http://toutiao.com/m3908723027/', NULL, NULL, NULL, 50, ' 星扒客push', NULL);
    
    import java.io.ByteArrayInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.Properties;
    import java.util.Set;
    
    import cn.paltform.Spider;
    
    /**
     * ClassName:DBlUtils <br/>
     * Function: TODO ADD FUNCTION. <br/>
     * Reason: TODO ADD REASON. <br/>
     * Date: 2015年9月14日 下午3:19:51 <br/>
     * 
     * @author
     * @version
     * @since JDK 1.7.79
     */
    public class DBlUtils {
    
    	public static String url = null;
    	public static String username = null;
    	public static String password = null;
    	public static Connection conn;
    	public static Statement stmt;
    	public static ResultSet rs;
    	public static String fileName = null;
    
    	public static String PATH = "/dbconfig.properties";
    	public static Properties properties;
    	static {
    		try {
    			InputStream is = DBlUtils.class.getResourceAsStream(PATH);
    			System.out.println("is:" + is);
    			properties = new Properties();
    			properties.load(is);
    			url = properties.getProperty("jdbc.url.jwhat");
    			username = properties.getProperty("jdbc.username.jwhat");
    			password = properties.getProperty("jdbc.password.jwhat");
    			if (is != null)
    				is.close();
    		} catch (IOException e) {
    
    			e.printStackTrace();
    		}
    
    	}
    
    	public void closeConnection(Connection conn) {
    
    		if (conn != null) {
    			try {
    				conn.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    
    	public static void insertd(String sql) {
    		try {
    			conn = DriverManager.getConnection(url, username, password);
    			conn.setAutoCommit(false);
    			stmt = conn.prepareStatement("load data local infile '' "
    					+ "into table loadtest fields terminated by ','");
    			StringBuilder sb = new StringBuilder();
    			InputStream is = new ByteArrayInputStream(sb.toString().getBytes());
    			((com.mysql.jdbc.Statement) stmt).setLocalInfileInputStream(is);
    			stmt.executeUpdate(sql);
    			conn.commit();
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}
    	}
    
    	/**
    	 * 查询url
    	 * 
    	 * @param sql
    	 * @return
    	 */
    	public static List<Map> selecturlBySql(String sql) {
    		List<Map> listUrl = new ArrayList<Map>();
    		try {
    			conn = DriverManager.getConnection(url, username, password);
    			conn.setAutoCommit(false);
    			stmt = conn.prepareStatement("load data local infile '' "
    					+ "into table loadtest fields terminated by ','");
    			StringBuilder sb = new StringBuilder();
    			InputStream is = new ByteArrayInputStream(sb.toString().getBytes());
    			((com.mysql.jdbc.Statement) stmt).setLocalInfileInputStream(is);
    			rs = stmt.executeQuery(sql);
    			if (rs != null) {
    				ResultSetMetaData metaData = rs.getMetaData();
    				int count = metaData.getColumnCount();
    				Map map = null;
    				while (rs.next()) {
    					map = new HashMap();
    					for (int i = 1; i <= count; i++) {
    						map.put(metaData.getColumnName(i), rs.getObject(i));
    					}
    					listUrl.add(map);
    				}
    			}
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}
    		return listUrl;
    	}
    
    	public static void main(String[] args) throws Exception {
    		String sql = "select t.url from bns_spider t ";
    		List<Map> selecturlBySql = selecturlBySql(sql);
    		for (int i = 0; i < selecturlBySql.size(); i++) {
    			Map map = selecturlBySql.get(i);
    			Set set = map.entrySet();
    			Iterator iterator = set.iterator();
    			String key = null;
    			String value = null;
    			while (iterator.hasNext()) {
    				Map.Entry mapentry = (Map.Entry) iterator.next();
    				key = (String)mapentry.getKey();
    				value =(String) mapentry.getValue();
    			}
    		//	System.out.println( key + "  " + value);
    		}
    		// insertd(sql);
    
    	}
    }
  • 相关阅读:
    【博弈论】囚徒困境
    【LTE与5G】
    【现代通信技术】绪论
    【操作系统】 逻辑结构
    【密码学】
    【计算机网络】网络应用
    部署docker仓库-Harbor
    ELK+filebeat收集K8S平台日志
    istio-http流量管理
    K8S集群部署istio
  • 原文地址:https://www.cnblogs.com/zhanggl/p/4874737.html
Copyright © 2011-2022 走看看