配置文件: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); }
}

通过java api 读取sql 中数据(查询)的更多相关文章

  1. 使用JAVA API读取HDFS的文件数据出现乱码的解决方案

    使用JAVA api读取HDFS文件乱码踩坑 想写一个读取HFDS上的部分文件数据做预览的接口,根据网上的博客实现后,发现有时读取信息会出现乱码,例如读取一个csv时,字符串之间被逗号分割 英文字符串 ...

  2. [搜索]ElasticSearch Java Api(一) -添加数据创建索引

    转载:http://blog.csdn.net/napoay/article/details/51707023 ElasticSearch JAVA API官网文档:https://www.elast ...

  3. SQL中Between查询日期时需要注意的地方

    SQL中Between查询日期时需要注意的地方   某个表某个字段是Datetime型 以"YYYY-MM-DD 00:00:00" 存放 (1).例如数据 2009-01-22 ...

  4. 08Microsoft SQL Server 数据查询

    Microsoft SQL Server 数据查询 单表查询所有列 --查询所有行所有列 select all * from table; --查询不重复行的所有列 select distinct * ...

  5. JavaWeb_(Hibernate框架)Hibernate中数据查询语句SQL基本用法

    本文展示三种在Hibernate中使用SQL语句进行数据查询基本用法 1.基本查询 2.条件查询 3.分页查询 package com.Gary.dao; import java.util.List; ...

  6. sql中对查询出来的数据进行分页

    当sql中存储的数据量比较大时,在web中 数据显示时都会对数据进行分页,分页不会在客户端进行分页,而是在数据库查询过程中进行了分页. sql代码: DECLARE @pageindex INT; - ...

  7. 6、SQL Server 数据查询

    一.使用SELECT检索数据 数据查询是SQL语言的中心内容,SELECT 语句的作用是让数据库服务器根据客户要求检索出所需要的信息资料,并按照规定的格式进行整理,返回给客户端. SELECT 语句的 ...

  8. SQL Server 数据查询 整理

    一.使用SELECT检索数据 数据查询是SQL语言的中心内容,SELECT 语句的作用是让数据库服务器根据客户要求检索出所需要的信息资料,并按照规定的格式进行整理,返回给客户端. SELECT 语句的 ...

  9. Mysql对表中 数据 查询的操作 DQL

    准备数据,倒入sql文件 运行sql文件 得到四张表 select * from 表名  * 代表全部 1.AS子句作为别名 select studentname as "姓名" ...

随机推荐

  1. echarts数据包坐标拾取工具

    http://geojson.io/#map=4/37.20/103.45

  2. A + B Problem II HDU - 1002

    非常简单的大数加法,因为不会Java只能手写大数加法了;博客存一下以后回来看看 #include<bits/stdc++.h> using namespace std; +; char A ...

  3. JPA 派生标识符的两种实现方式

    方法一:@Entity@IdClass(ModuleId.class)public class Module { @Id private Integer index; @Id @ManyToOne p ...

  4. 一份详尽的 Java 问题排查工具清单,值得收藏!

    | grep 5 -A 3    #上匹配seq 10 | grep 5 -B 3    #下匹配seq 10 | grep 5 -C 3    #上下匹配,平时用这个就妥了cat f.txt | g ...

  5. centors7 和 win7 修改开机顺序

    打开/boot/grub2/grub.cfg,找到windows对应的项 我的是 Windows 7 (loader) (on /dev/sda1) 执行命令 grub2-set-default &q ...

  6. C#关键字扫盲——Tuple(元组类) 、ValueTuple(值元组)

    原文:C#关键字扫盲--Tuple(元组类) .ValueTuple(值元组) 版权声明:本文为博主原创文章,随意转载. https://blog.csdn.net/Michel4Liu/articl ...

  7. spring 中各个配置文件的说明

    (1)pom.xml pom.xml文件是在整个项目下面,该xml的主要作用是:导入框架的jar包及其所依赖的jar包,导入的jar包是写在<dependencies></depen ...

  8. IDEA Caused by: java.lang.OutOfMemoryError: PermGen space

    错误:OutOfMemoryError: PermGen space 非堆溢出(永久保存区域溢出) 解决方法: 在Run/Debug configuration 的你要运行行的tomcat里面的 vm ...

  9. jquery.cookie.js实现cookie记住用户名和密码

    记得导入 <script src="jquery.js" type="text/javascript"></script> <sc ...

  10. 使用Maven进行依赖管理和项目构建

    什么是Maven 1 依赖的管理:仅仅通过jar包的几个属性,就能确定唯一的jar包,在指定的文件pom.xml中,只要写入这些依赖属性,就会自动下载并管理jar包. 2 项目的构建:内置很多的插件与 ...