import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class UpdateSegment {
public static void main(String[] args) {
try {
//连接数据库
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@ip:端口:数据实例名", "user", "pwd");
String sql = "insert into zt_num_area(num,province,city) values(?,?,?)";//插入语句
//读excel
File file = new File("D:\\segment.xls");
if(file.exists()){
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(file));
HSSFSheet sheet = wb.getSheetAt(0);
int maxRow = sheet.getLastRowNum();
System.out.println("maxRow:"+maxRow);
String num = sheet.getRow(i).getCell((short)0).getStringCellValue().trim();
String province = sheet.getRow(i).getCell((short)1).getStringCellValue().trim();
String city = sheet.getRow(i).getCell((short)2).getStringCellValue().trim();
pst.setString(1, num);
pst.setString(2, province);
pst.setString(3, city);
System.out.println("----------"+i+","+num);
try {
pst.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
pst.close();
continue;
}
pst.close();
}
}
con.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class UpdateSegment {
public static void main(String[] args) {
try {
//连接数据库
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@ip:端口:数据实例名", "user", "pwd");
String sql = "insert into zt_num_area(num,province,city) values(?,?,?)";//插入语句
//读excel
File file = new File("D:\\segment.xls");
if(file.exists()){
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(file));
HSSFSheet sheet = wb.getSheetAt(0);
int maxRow = sheet.getLastRowNum();
System.out.println("maxRow:"+maxRow);
for(int i=1;i<=maxRow;i++){
PreparedStatement pst = con.prepareStatement(sql);String num = sheet.getRow(i).getCell((short)0).getStringCellValue().trim();
String province = sheet.getRow(i).getCell((short)1).getStringCellValue().trim();
String city = sheet.getRow(i).getCell((short)2).getStringCellValue().trim();
pst.setString(1, num);
pst.setString(2, province);
pst.setString(3, city);
System.out.println("----------"+i+","+num);
try {
pst.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
pst.close();
continue;
}
pst.close();
}
}
con.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}