list集合导入数据库

sql

USE `hibernate_32`;

/*Table structure for table `t_xml` */

DROP TABLE IF EXISTS `t_xml`;

CREATE TABLE `t_xml` (
  `A` varchar(20) COLLATE utf8_bin DEFAULT NULL,
  `B` varchar(20) COLLATE utf8_bin DEFAULT NULL,
  `C` varchar(20) COLLATE utf8_bin DEFAULT NULL,
  `D` varchar(20) COLLATE utf8_bin DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

H.java

package cn.itcast.tx;

public class H {
private  String A;
private  String B;
private  String C;
private  String D;



public H(String a, String b, String c, String d) {
    
    this.A = a;
    B = b;
    C = c;
    D = d;
}

@Override
public String toString() {
    return "h [A=" + A + ", B=" + B + ", C=" + C + ", D=" + D + "]";
}
  
public String getA() {
    return A;
}
public void setA(String a) {
    A = a;
}
public String getB() {
    return B;
}
public void setB(String b) {
    B = b;
}
public String getC() {
    return C;
}
public void setC(String c) {
    C = c;
}
public String getD() {
    return D;
}
public void setD(String d) {
    D = d;
}

}

src 目录下面放上

c3p0-config.xml

<c3p0-config>
    <default-config>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/hibernate_32
        </property>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="user">root</property>
        <property name="password">root</property>
        <property name="initialPoolSize">3</property>
        <property name="maxPoolSize">6</property>
        <property name="maxIdleTime">1000</property>
    </default-config>


    <named-config name="oracle_config">
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbc_demo</property>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="user">root</property>
        <property name="password">root</property>
        <property name="initialPoolSize">3</property>
        <property name="maxPoolSize">6</property>
        <property name="maxIdleTime">1000</property>
    </named-config>


</c3p0-config>
DbUtil.java
package cn.itcast.tx;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * 1. 返回连接 2. 关闭
 * 
 * @author Jie.Yuan
 * 
 */
public class DbUtil {

    // 连接参数
//    // private String url = "jdbc:mysql://localhost:3306/jdbc_demo";
//    private static String url = "jdbc:mysql:///jdbc";
//    private static String user = "root";
//    private static String password = "root";
    
    //初始化连接池
    private static DataSource dataSource;
    //静态代码块初始化连接池资源,只加载一次
    static{
        dataSource=new ComboPooledDataSource();
    }
    

    /**
     * 返回连接对象
     */
    public static Connection getConnection() {
        try {
            
            return dataSource.getConnection();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 关闭
     */
    public static void closeAll(Connection con, Statement stmt, ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();  // 快速异常捕获 Alt + shift + z 
                rs = null;   // 建议垃圾回收期回收资源
            }
            if (stmt != null) {
                stmt.close();
                stmt = null;
            }
            if (con != null && !con.isClosed()) {
                con.close();
                con = null;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}

up.java

package cn.itcast.tx;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class up {


public static void main(String[] args){
            //插入数据的sql语句
            String sql="insert into T_XML(A, B, C, D) values (?, ?, ?, ?)";
            Connection conn=null;
            PreparedStatement pstmt=null;
            
            try{
                conn= DbUtil.getConnection();
                pstmt=conn.prepareStatement(sql);
                //读取xml文件
                Document doc=new SAXReader().read(new File("D:/1.XML"));
                //选择xml文件的节点
                //List itemList=doc.selectNodes("ACCESOS/item/SOCIO");
                List<H> itemList = new ArrayList();
                H h1=new H("a1","a2","a3","a4");
                H h2=new H("b1","b2","b3","b4");
                itemList.add(h1);
                itemList.add(h2);
//                itemList.add("a3");
//                itemList.add("a4");
//                for(int i=1;i<3;i++) {
//                    itemList.add("h"+i);
//                }
                System.out.println("list::::::"+itemList);
                for(H s:itemList) {
                    pstmt.setString(1,s.getA());
                    pstmt.setString(2, s.getB());
                    pstmt.setString(3, s.getC());
                    pstmt.setString(4, s.getD());

                    pstmt.addBatch();
                }
                //遍历读出的xml中的节点
               /* for(Iterator iter=itemList.iterator();iter.hasNext();){
                
                    
                    Element el=(Element)iter.next();
                    
                    //读取节点内容
                    String numero=el.elementText("NUMERO");
                    String reposicion = el.elementText("REPOSICION");
                    String nombre = el.elementText("NOMBRE");
                    //遍历TURNOS节点中的内容
                    List turnosList = el.elements("TURNOS");
                    StringBuffer sbString=new StringBuffer();
                    for(Iterator iter1=turnosList.iterator();iter1.hasNext();){                    
                        Element turnosElt=(Element)iter1.next();
                        String lu = turnosElt.elementText("LU");
                        String ma = turnosElt.elementText("MA");
                        String mi = turnosElt.elementText("MI");
                        String ju = turnosElt.elementText("JU");
                        String vi = turnosElt.elementText("VI");
                        String sa = turnosElt.elementText("SA");
                        String doo = turnosElt.elementText("DO");
                        sbString.append(lu + "," + ma + "," + mi + "," + ju + "," + vi + "," + sa + "," + doo);
                    }*/
                    //为sql语句赋值
                    
//                }
                pstmt.executeBatch();
                System.out.print("将XML导入数据库成功");
            }catch(Exception e){
                e.printStackTrace();
            }finally{
                DbUtil.closeAll(conn,pstmt,null);
//                DbUtil.close(conn);
            }
        }

    
    
    
    
    
    
    
    
    
}
原文地址:https://www.cnblogs.com/huodaihao/p/8048055.html