赞
踩
一、前言
关于实现mysql数据简单操作工具类MySQLUtils,用户密码连接数据库执行sql脚本ExecSQL、将查询结果转为com.alibaba.fastjson.JSONArray(fastjson)等常用操作。
二、代码示例import java.io.BufferedReader;@b@import java.io.FileReader;@b@import java.sql.Connection;@b@import java.sql.DriverManager;@b@import java.sql.ResultSet;@b@import java.sql.ResultSetMetaData;@b@import java.sql.Statement;@b@import java.util.ArrayList;@b@import java.util.List;@b@@b@import com.alibaba.fastjson.JSONArray;@b@import com.alibaba.fastjson.JSONObject;@b@@b@public class MySQLUtils {@b@@b@public static class ExecSQL {@b@@b@public final String url;@b@public final String sql;@b@public final String user;@b@public final String passwd;@b@@b@public ExecSQL(String host, String user, String passwd, String db, String sql) {@b@this.url = String.format("jdbc:mysql://%s/%s?characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull", host, db);@b@this.sql = sql;@b@this.user = user;@b@this.passwd = passwd;@b@}@b@@b@public ExecSQL(String full_host, String user, String passwd, String sql) {@b@this.url = full_host;@b@this.sql = sql;@b@this.user = user;@b@this.passwd = passwd;@b@}@b@}@b@@b@public static boolean exec(ExecSQL execSQL) {@b@Connection conn = null;@b@Statement stmt = null;@b@try {@b@Class.forName("com.mysql.jdbc.Driver");@b@conn = DriverManager.getConnection(execSQL.url, execSQL.user, execSQL.passwd);@b@stmt = conn.createStatement();@b@return stmt.execute(execSQL.sql);@b@} catch (Exception e) {@b@e.printStackTrace();@b@} finally {@b@if (stmt != null) try { stmt.close(); } catch (Exception e2) {}@b@if (conn != null) try { conn.close(); } catch (Exception e2) {}@b@}@b@return false;@b@}@b@@b@public static JSONArray query(ExecSQL execSQL) {@b@JSONArray ret = null;@b@Connection conn = null;@b@Statement stmt = null;@b@ResultSet rs = null;@b@try {@b@Class.forName("com.mysql.jdbc.Driver");@b@conn = DriverManager.getConnection(execSQL.url, execSQL.user, execSQL.passwd);@b@stmt = conn.createStatement();@b@rs = stmt.executeQuery(execSQL.sql);@b@ret = convert2(rs);@b@} catch (Exception e) {@b@e.printStackTrace();@b@} finally {@b@if (rs != null) try { rs.close(); } catch (Exception e2) {}@b@if (stmt != null) try { stmt.close(); } catch (Exception e2) {}@b@if (conn != null) try { conn.close(); } catch (Exception e2) {}@b@}@b@return ret;@b@}@b@@b@public static JSONArray convert2(ResultSet rs) throws Exception {@b@JSONArray json = new JSONArray();@b@ResultSetMetaData rsmd = rs.getMetaData();@b@int numColumns = rsmd.getColumnCount();@b@JSONArray th = new JSONArray();@b@for (int i=1; i parseFile(String sql_file) throws Exception {@b@List ret = new ArrayList();@b@String file_dir = sql_file.substring(0, sql_file.lastIndexOf('/') + 1);@b@BufferedReader reader = null;@b@try {@b@reader = new BufferedReader(new FileReader(sql_file));@b@StringBuffer sqlBuf = new StringBuffer();@b@String line;@b@boolean skip = false;@b@while ((line = reader.readLine()) != null) {@b@line = line.trim();@b@if (line.toUpperCase().startsWith("DELIMITER")) {@b@skip = !line.endsWith(";");@b@continue;@b@} else if (skip || line.startsWith("--")) {@b@continue;@b@}@b@@b@sqlBuf.append(line);@b@if (line.endsWith(";")) {@b@String sql = sqlBuf.toString().trim();@b@sqlBuf = new StringBuffer();@b@if (sql.startsWith("source ")) {@b@String source_file = sql.replace("source", "").replace(";", "").trim();@b@ret.addAll(parseFile(file_dir + source_file));@b@} else {@b@ret.add(sql);@b@}@b@} else {@b@sqlBuf.append(" ");@b@}@b@}@b@} finally {@b@if (reader != null) try { reader.close(); } catch (Exception e) { }@b@}@b@return ret;@b@}@b@}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。