解析XML配置文件连接数据库,并实现对数据库批量添加数据
xml文件
<?xml version="1.0" encoding="gb2312"?>
<datasource-config>
<datasource name="mzdb" class="jos.framework.jdbc.datasource.DruidDataSource" load="true" default="true" dialect="mysql">
<property name="driver">com.mysql.cj.jdbc.Driver</property>
<property name="url">jdbc:mysql://localhost/srapp_zhpt2017?allowMultiQueries=true&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT&useSSL=false</property>
<property name="username">root</property>
<property name="password">2811107845</property>
<property name="initialPoolSize">2</property>
<property name="maxPoolSize">200</property>
<property name="minPoolSize">5</property>
<property name="maxIdleTime">25000</property>
<property name="maxStatements">0</property>
<property name="preferredTestQuery">SELECT 1 FROM C3P0TESTTABLE</property>
<property name="testConnectionOnCheckin">false</property>
<property name="testConnectionOnCheckout">true</property>
<property name="acquireIncrement">5</property>
<property name="idleConnectionTestPeriod">18000</property>
<property name="poolPreparedStatements">true</property>
</datasource>
</datasource-config>
解析xml
/**
* @program: demo_code
* @description: 获取数据库链接
* @author: xys
* @created: 2020/12/02 11:26
*/
public class JDBCConn {
public static String driver;
public static String url;
public static String username;
public static String password;
public static Connection getConnect() {
xmlAnalysis();
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection conn = null;
try {
conn = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println(conn);
return conn;
}
public static void closeConnect(Connection connection) {
/* *
* 功能描述: 关闭数据库链接资源
*
* @param: connection
* @return: void
* @auther: xys
* @date: 2020/12/2 下午1:49
*/
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void closeConn(Connection conn, PreparedStatement preparedStatement) {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (preparedStatement != null) {
preparedStatement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
@Test
public void test() {
Connection collection = getConnect();
closeConnect(collection);
}
public static void xmlAnalysis() {
//1.创建Reader对象
SAXReader reader = new SAXReader();
//2.加载xml
Document document = null;
try {
document = reader.read(new File("web/WEB-INF/config/global-datasource.xml"));
} catch (DocumentException e) {
e.printStackTrace();
}
//3.获取根节点
Element rootElement = document.getRootElement();
//将根节点包装成一个迭代器,方便遍历
Iterator iterator = rootElement.elementIterator();
//遍历根节点
while (iterator.hasNext()){
//获取根节点的子节点
Element stu = (Element) iterator.next();
Iterator iterator1 = stu.elementIterator();
//遍历子节点
while (iterator1.hasNext()){
Element stuChild = (Element) iterator1.next();
//获取子节点中的name属性的值,并进行判断
if ("driver".equals(stuChild.attributeValue("name"))&&(driver==null)){
driver = stuChild.getStringValue();
System.out.println("driver="+driver);
}
if ("url".equals(stuChild.attributeValue("name"))&&(url==null)){
url = stuChild.getStringValue();
System.out.println("url="+url);
}
if ("username".equals(stuChild.attributeValue("name"))&&(username==null)){
username = stuChild.getStringValue();
System.out.println("username="+username);
}
if ("password".equals(stuChild.attributeValue("name"))&&(password==null)){
password = stuChild.getStringValue();
System.out.println("password="+password);
}
}
} //将根节点包装成一个迭代器,方便遍历
}
}
批量生成数据
//顺序生成字符串,因为主键是字符串类型的使用数字得话,顺序会很乱
public static List<String> OrderlyStringSequence(String s, int count) {
List<String> res = new ArrayList<String>();
char[] src = s.toLowerCase().toCharArray();
char[] replica = s.toLowerCase().toCharArray();
int maxIndex = src.length - 1;
char initchar = src[maxIndex];
int counter = 1;
if (count <= 0) {
return null;
}
for (int j = 0; j < src.length; j++) {
if(!Character.isLetter( src[j])) {
return null;
}
}
res.add(String.valueOf(replica));
counter++;
while (true) {
replica[maxIndex] = initchar;
for (int i = 0; i < 26; i++) {
int c = (int)replica[maxIndex];
if (c < 'z') {
c += 1;
replica[maxIndex] = (char)c;
res.add(String.valueOf(replica));
if (counter++ >= count) {
return res;
}
}
}
int n = src.length - 1;
while ((n >= 0) && (replica[n] >= 'z')) {
replica[n] = src[n];
n--;
}
if (n < 0) {
return res;
} else if (n >= 0) {
replica[n] = (char)((int)replica[n] + 1);
}
res.add(String.valueOf(replica));
if (counter++ >= count) {
return res;
}
}
}
//批量生成数据
@Test
public void test() {
Connection conn = null;
PreparedStatement ps = null;
List<String> list = OrderlyStringSequence("aaaaaaa",200);
try {
//1.获取数据库链接
conn = JDBCConn.getConnect();
conn.setAutoCommit(false);
long start = System.currentTimeMillis();
//2.预处理sql语句
String sql = "insert into ygo_card(card_name,card_type,card_effect,card_time,card_address,pk_id)values(?,?,?,?,?,?)";
ps = conn.prepareStatement(sql);
String card_name = "天使的施舍";
String card_type = "魔法卡";
String card_effect = "抽卡型";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd");
Date card_time = simpleDateFormat.parse("2020-12-1");
String card_address = "杭州西湖三墩街道";
for (int i = 1; i <= 200; i++) {
String pk_id = list.get(i-1);
ps.setObject(1, card_name + i);
ps.setObject(2, card_type + i);
ps.setObject(3, card_effect + i);
ps.setObject(4, card_time);
ps.setObject(5, card_address + i);
ps.setObject(6,pk_id );
ps.addBatch();
if (i % 50 == 0) {
//执行sql
ps.executeBatch();
ps.clearBatch();
}
}
//3.执行sql语句
// int i = ps.executeUpdate();
// if (i > 0) {
// System.out.println("执行成功");
// }
conn.commit();
long end = System.currentTimeMillis();
System.out.println("花费的时间为:"+(end-start));
} catch (Exception e) {
e.printStackTrace();
} finally {
//4.处理结果集
//5.关闭资源流
JDBCConn.closeConn(conn, ps);
}
}
版权声明:本文为weixin_43853447原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。