public class LogicalStationDao {
private static String url ="jdbc:mysql://localhost:3306/lianxi?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
private static String user = "root";
private static String password = "root";
public static void Test(){
LogicalStation station=new LogicalStation();//
List<LogicalStation> list=TestRunPoi.read();
Connection conn = null;
PreparedStatement ps =null;
int j=0;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, user, password);
int batchSize = 5000;
String sql = "insert into jz (name, city, netWork_type, Site_type,longitude,latitude,manufacturers,Station_name) values(?, ?, ?, ?,?,?,?,?)";
ps = conn.prepareStatement(sql);
Long startTime = System.currentTimeMillis();
conn.setAutoCommit(false);
for (int i = 0; i<list.size(); i++) {
++j;
ps.setString(1, station.getName());
ps.setString(2, station.getCity());
ps.setString(3, station.getNetWork_type());
ps.setString(4, station.getSite_type());
ps.setString(5, station.getLongitude());
ps.setString(6, station.getLatitude());
ps.setString(7, station.getManufacturers());
ps.setString(8, station.getStation_name());
ps.addBatch();
if ( j % batchSize == 0 ) {
ps.executeBatch();
conn.commit();
}
}
if ( j % batchSize != 0 ) {
ps.executeBatch();
conn.commit();
Long endTime = System.currentTimeMillis();
System.out.println("用时:" + (endTime - startTime));
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}finally{
if(ps!=null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
}
》——————————————————————————
public class LogicalStation {
private String name;// 名称
private String city;// 城市
private String netWork_type;
private String Site_type;
private String longitude;
private String latitude;
private String manufacturers;
private String Station_name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getNetWork_type() {
return netWork_type;
}
public void setNetWork_type(String netWork_type) {
this.netWork_type = netWork_type;
}
public String getSite_type() {
return Site_type;
}
public void setSite_type(String site_type) {
Site_type = site_type;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getManufacturers() {
return manufacturers;
}
public void setManufacturers(String manufacturers) {
this.manufacturers = manufacturers;
}
public String getStation_name() {
return Station_name;
}
public void setStation_name(String station_name) {
Station_name = station_name;
}
@Override
public String toString() {
return "LogicalStation [name=" + name + ", city=" + city + ", netWork_type=" + netWork_type + ", Site_type="
+ Site_type + ", longitude=" + longitude + ", latitude=" + latitude + ", manufacturers=" + manufacturers
+ ", Station_name=" + Station_name + "]";
}
}