package com.zbiti.fr;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;
import java.io.*;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class TestMain {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://127.0.0.1/demo?characterEncoding=utf-8&useSSL=false";
// Database credentials
static final String USER = "root";
static final String PASS = "root";
//获取文件信息;
public void dataSourceMess() throws Exception{
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");
Properties props = new Properties();
props.load(inputStream);
System.out.println( props.getProperty("spring.datasource.driver-class-name"));
}
public static JSONObject convertXmlIntoJSONObject (String xml){
JSONObject jsonObject=new JSONObject();
try {
Document xmlDocument = DocumentHelper.parseText(xml);
OutputFormat format = new OutputFormat();
format.setEncoding("UTF-8");
format.setExpandEmptyElements(true);
StringWriter out = new StringWriter();
XMLWriter writer = new XMLWriter(out, format);
try {
writer.write(xmlDocument);
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
//out.toString() 此结果为xml的<a></a>格式
jsonObject= XML.toJSONObject(out.toString());
} catch (DocumentException e1) {
e1.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject;
}
public static void main(String[] args) throws Exception {
TestMain testMain = new TestMain();
testMain.dataSourceMess();
// Map<String, String> map = new HashMap<>();
// map.put("personnel_account", "1中");
// map.put("access_url", "2");
// map.put("access_report_name", "3");
// map.put("access_time", "2022-05-31 02:00:00");
// map.put("parameter_details", "4");
// map.put("is_successful", "5");
// Connection conn = null;
// Statement stmt = null;
// String tableName="finereport_sso_oper_log";
// Map connectionMap =new HashMap();
// try {
// //建立连接
// connectionMap = connection(conn,stmt);
// connectionMap.put("tableName",tableName);
// //插入数据
// insertDatabase(connectionMap,map);
//
// }catch (Exception e){
// throw e;
// }finally {
// //关闭连接
// closeConnection(connectionMap);
// }
// wholeInsertData(map, tableName);
}
/**
* @return void
* @Author wangjin
* @Description //
* @Date 2022/5/31 0031
* @Param [map()字段和值的集合, tableName(表名)]
**/
public static void wholeInsertData(Map<String, String> map, String tableName) throws Exception {
Connection conn = null;
Statement stmt = null;
try {
//STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
//表字段
StringBuffer column = new StringBuffer();
//表字段对应的值
StringBuffer values = new StringBuffer();
//循环map获取表的字段和对应的值
for (String key : map.keySet()) {
String value = map.get(key);
column.append("`").append(key).append("`").append(",");
values.append("'").append(value).append("'").append(",");
}
//拼接sql
String sql = "INSERT INTO " + tableName + "(" + column.deleteCharAt(column.length() - 1) + ")"
+ "VALUES (" + values.deleteCharAt(values.length() - 1) + ")";
//执行sql
stmt.executeUpdate(sql);
} catch (Exception e) {
throw e;
} finally {
try {
if (stmt != null) {
conn.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException se) {
throw se;
}
}
}
/**
* @return * @return: null
* @Author wangjin
* @Description //建立连接
* @Date 2022/5/31 0031
* @Param * @param null:
**/
public static Map connection(Connection conn, Statement stmt) throws Exception {
try {
//STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
Map map = new HashMap();
map.put("conn", conn);
map.put("stmt", stmt);
return map;
} catch (Exception e) {
throw e;
}
}
/**
* @return void
* @Author wangjin
* @Description //插入数据
* @Date 2022/6/1 0001
* @Param [stmt, map, tableName]
**/
public static void insertDatabase(Map connectionMap, Map<String, String> map) throws SQLException {
try {
Statement stmt = (Statement) connectionMap.get("stmt");
String tableName = (String) connectionMap.get("tableName");
//表字段
StringBuffer column = new StringBuffer();
//表字段对应的值
StringBuffer values = new StringBuffer();
//循环map获取表的字段和对应的值
for (String key : map.keySet()) {
String value = map.get(key);
column.append("`").append(key).append("`").append(",");
values.append("'").append(value).append("'").append(",");
}
//拼接sql
String sql = "INSERT INTO " + tableName + "(" + column.deleteCharAt(column.length() - 1) + ")"
+ "VALUES (" + values.deleteCharAt(values.length() - 1) + ")";
//执行sql
stmt.executeUpdate(sql);
} catch (Exception e) {
throw e;
}
}
/**
* @return void
* @Author wangjin
* @Description //关闭连接
* @Date 2022/5/31 0031
* @Param [conn, stmt]
**/
public static void closeConnection(Map connectionMap) throws Exception {
try {
Statement stmt = (Statement) connectionMap.get("stmt");
Connection conn = (Connection) connectionMap.get("conn");
if (stmt != null) {
conn.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException se) {
throw se;
}
}
}
网友评论