81,094
社区成员
发帖
与我相关
我的任务
分享
package d123;
import java.sql.Connection;
import java.sql.Statement;
public class POIDZ {
public static Connection conn = null;
public static Statement stmt = null;
public static boolean connectDB2() {
try {
Class.forName("org.gjt.mm.mysql.Driver");
String url = "jdbc:mysql://localhost:3306/dstore";
conn = DriverManager.getConnection(url, "root", "123");
stmt = conn.createStatement();
}
// 捕获加载驱动程序异常
catch (ClassNotFoundException cnfex) {
cnfex.printStackTrace();
return false;
}
// 捕获连接数据库异常
catch (SQLException sqlex) {
sqlex.printStackTrace();
// System.exit(1); // terminate program
return false;
}
return true;
}
public static boolean readExcelToDB2(String userName,String fahao) {
POIFSFileSystem fs = null;
HSSFWorkbook wb = null;
FileInputStream fjexl = null;
try {
fjexl = new FileInputStream("D:\\Tomcat6.0\\webapps\\aas\\upload\\"+userName+".xls");
fs = new POIFSFileSystem(fjexl);
wb = new HSSFWorkbook(fs);
fjexl.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = null;
HSSFCell cell = null;
String shoudaot ="8";
long jieshoutimet =7;
int rowNum, cellNum;
int i;
rowNum = sheet.getLastRowNum();
for (i = 3; i <= 30; i++) { ---->第一个循环
row = sheet.getRow(i);
// cellNum = row.getLastCellNum();
try{
cell = row.getCell((short) 10);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
dongci = cell.getStringCellValue().trim();
dongci=new String(dongci.getBytes("GBK"),"latin1");
}catch(Exception e){
}
if (haomat!=0 & jieshou!="" & shengdan!=""){
String sql = "insert into dingzhi(user,name) values('"+userName+"','"+namet+"')";
try {
stmt.executeUpdate(sql);
shengdan="";
} catch (SQLException e1) {
e1.printStackTrace();
return false;
}
stmt.close();
conn.close();
}
}
rowNum = sheet.getLastRowNum();
for (i = 3; i <= 30; i++) { ----》第二个循环
row = sheet.getRow(i);
// cellNum = row.getLastCellNum();
try{
cell = row.getCell((short) 0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
name = cell.getStringCellValue().trim();
name=new String(name.getBytes("GBK"),"latin1");
name = name.substring(0, 20);
}catch(Exception e){
}
if (name!="-"){
String sql = "insert into listuser(username,liantong) values('"+name+"','"+liantong+"')";
try {
stmt.executeUpdate(sql);
} catch (SQLException e1) {
e1.printStackTrace();
return false;
}
stmt.close();
conn.close();
}
}
return true;
}
}