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 List lists = new ArrayList(); public static String PATH = "/dbconfig.properties";
private static Properties properties;
static {
try {
InputStream is = DBlUtils.class.getResourceAsStream(PATH);
properties = new Properties();
properties.load(is);
url = properties.getProperty("jdbc.url");
username = properties.getProperty("jdbc.username");
password = properties.getProperty("jdbc.password");
fileName = properties.getProperty("fileName");
System.out.println("fileName:" + fileName);
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();
}
}
} /**
* 查询mysql 数据库数据,并获得内容
*
* @param sql
*/
public static void queryDatas(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);
ResultSet rs = stmt.executeQuery(sql);
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
// 输出列名
for (int i = 1; i <= columnCount; i++) {
System.out.print(rsmd.getColumnName(i));
System.out.print("(" + rsmd.getColumnTypeName(i) + ")");
System.out.print(" | ");
}
System.out.println();
// 输出数据
while (rs.next()) {
for (int i = 1; i <= columnCount; i++) {
System.out.print(rs.getString(i) + " | ");
}
System.out.println();
} // conn.commit();
} catch (SQLException e) {
e.printStackTrace();
} } public static void main(String[] args) {         String sql = "select * from tablename";
        queryDatas(sql);     }

java 读取mysql中数据 并取出的更多相关文章

  1. 关于Java读取mysql中date类型字段默认值'0000-00-00'的问题

    今天在做项目过程中,查询一个表中数据时总碰到这个问题:      java.sql.SQLException:Value '0000-00-00' can not be represented as ...

  2. java读取配置文件中数据

    Properties pps=new Properties();        try {            pps.load(new FileInputStream("src/emai ...

  3. java读取请求中body数据

    java读取请求中body数据 /** * 获取request中body数据 * * @author lifq * * 2017年2月24日 下午2:29:06 * @throws IOExcepti ...

  4. PHP读取mysql中的数据

    <!DOCTYPE HTML> <html> <head> <title> PHP动态读取mysql中的数据 </title> <me ...

  5. Java读取接口中的数据,并保存到txt文件中!

    //创建读取接口中数据的方法 public static String read() { URL url = null; BufferedReader reader = null; HttpURLCo ...

  6. Java读取word中表格

    因为要新建一个站,公司要把word表格的部分行列存到数据库中.之前用java操作过excel,本来打算用java从word表格中读取数据,再存到数据库中,结果因为权限不够,无法访问公司要写的那个数据库 ...

  7. 通过java读取HDFS的数据 (转)

    原文链接:通过java读取HDFS的数据 通过JAVA直接读取HDFS中的时候,一定会用到FSDataInputStream类,通过FSDataInputStream以流的形式从HDFS读数据代码如下 ...

  8. java读取PHP接口数据的实现方法(四)

    PHP文件: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 3 ...

  9. java读取request中的xml

    java读取request中的xml   答: // 读取xml InputStream inputStream; StringBuffer sb = new StringBuffer(); inpu ...

随机推荐

  1. 点击手机返回键弹出Dialog对话框功能

    在程序中,我们为了防止出现客户在使用程序填信息或者浏览页面时因误点返回键造成关闭界面的现象,需要添加弹出框功能,以确认客户是否要退出本界面,下面是功能实现的代码: 1.点击手机返回键的判断 publi ...

  2. angulajs 详解 directive 中 的 scope 概念

    Directive 是 angularjs 中最重要的概念,我的理解就是自定义html tag, 这个自定的tag 浏览器不会解析,会有angularjs 来动态解析. 比如在html 中添加 < ...

  3. 3.1 开始使用 redux

    前面我们介绍了 flux 架构以及其开源实现 redux,在这一节中,我们将完整的介绍 redux: redux 介绍 redux 是什么 redux 概念 redux 三原则 redux Store ...

  4. PAT_A1041#Be Unique

    Source: PAT A1041 Be Unique (20 分) Description: Being unique is so important to people on Mars that ...

  5. Ubuntu12.04下删除文件夹内所有的.svn文件

    前段时间在公司里遇到一个问题,从svn上下载下来的文件含有.svn文件,如何删除当前目录下的所有.svn文件呢? 一个个手动删除显然不可能,太多了.其实在Ubuntu12.04下延伸至其他所搜的Lin ...

  6. OMG that's another blog!

    目录 1.Beginning 2.then 1.Beginning we'v learnt how to ask file from our own computer and tried to bui ...

  7. 35-Ubuntu-组管理-01-添加组/删除组/确认组信息

    组管理 提示: 创建组/删除组的终端命令都需要sudo执行,标准用户没有权限! 序号 命令 作用 01 sudo groupadd 组名 添加组 02 sudo groupdel 组名 删除组 03 ...

  8. 欧拉筛 线性筛 素数+莫比乌斯的mu[]

    https://blog.csdn.net/qq_39763472/article/details/82428602 模板来自https://blog.csdn.net/Avalon_cc/artic ...

  9. codeforces1175E Minimal Segment Cover 倍增

    题目传送门 题意:给出n条平行于x轴的线段,q次询问,每次询问一个区间最少要几条线段来覆盖,若不能覆盖则输出-1. 思路:先考虑贪心,必定是先找到,所有左端点小于等于$x$的线段的右端点最大在哪里,然 ...

  10. matlab计时超好用

    方法一: profile on <body?> profile viewer 会把所有代码的时间,都显示出来,每行每个函数用时统计,一目了然: 方法二: tic; <body-par ...