package mianyang.sanhe;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.ParseException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; public class ImportExcel extends HttpServlet{
static Connection con;
PreparedStatement pst;
private String filepath; public void getexcelname(String filepath){
this.filepath=filepath;
} /**
*
*/
private static final long serialVersionUID = -4528281625469749382L; public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
doPost(request, response);
}
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
filepath=request.getParameter("excelname").toString();
response.setContentType("text/html");
response.setCharacterEncoding("gb2312");
PrintWriter out = response.getWriter();
try {
if(filepath==null||filepath=="")
{
out.print("<script>alert('没有发现 文件')</script>");
}
if (insertDB(filepath)){
out.print("<script>alert('导入数据成功')</script>");
}
else
{
out.print("<script>alert('导入数据失败')</script>");
}
} catch (ParseException e) {
// TODO Auto-generated catch block
out.print("<script>alert('导入数据失败')</script>");
}
}
public boolean insertDB(String filePath) throws ParseException{
boolean flag=true;
try {
//文件流指向excel文件
FileInputStream fin=new FileInputStream(filePath);
HSSFWorkbook workbook=new HSSFWorkbook(fin);//创建工作薄
HSSFSheet sheet=workbook.getSheetAt(0);//得到工作表
HSSFRow row=null;//对应excel的行
HSSFCell cell=null;//对应excel的列
int totalRow=sheet.getLastRowNum();//得到excel的总记录条数
//以下的字段一一对应数据库表的字段
String name="";
String p_no="";
String code="";
int buyprice=0;
int saleprice=0;
int prostruc=0;
String productstruc="";
int etprice=0;
float productper=0.0f;
String sql="insert into products(name,p_no,code,buyprice,saleprice,prostruc,productstruc,etprice,productper) values(?,?,?,?,?,?,?,?,?)";
//SEQ_BOOK.NEXTVAL为数据库表序列
for(int i=2;i<=totalRow;i++){
row=sheet.getRow(i);
cell=row.getCell(1);
name=cell.getRichStringCellValue().toString();
cell=row.getCell(2);
p_no=cell.getRichStringCellValue().toString();
cell=row.getCell(3);
code=cell.getRichStringCellValue().toString();
cell=row.getCell(4);
//格式化字符串时间
buyprice=Integer.parseInt(cell.getRichStringCellValue().toString());
cell=row.getCell(5);
saleprice=Integer.parseInt(cell.getRichStringCellValue().toString());
cell=row.getCell(6);
if(cell.getRichStringCellValue().toString()=="单品"){
prostruc=0;
}
else
{
prostruc=1;
}
cell=row.getCell(7);
productstruc=cell.getRichStringCellValue().toString();
cell=row.getCell(8);
etprice=Integer.parseInt(cell.getRichStringCellValue().toString());
cell=row.getCell(9);
productper=Float.parseFloat(cell.getRichStringCellValue().toString()); BaseDAO dao = new BaseDAO();
con=dao.getConnection();
pst=con.prepareStatement(sql);
pst.setString(1,name);
pst.setString(2,p_no);
pst.setString(3,code);
pst.setInt(4,buyprice);
pst.setInt(5,saleprice);
pst.setInt(6,prostruc);
pst.setString(7,productstruc);
pst.setInt(8,etprice);
pst.setFloat(9,productper);
pst.execute();
}
flag=true;
} catch (FileNotFoundException e) {
flag=false;
//e.printStackTrace();
} catch(IOException ex){
flag=false;
//ex.printStackTrace();
} catch(SQLException exx){
flag=false;
//exx.printStackTrace();
}finally{
try {
pst.close();
con.close();
} catch (SQLException e) {
flag=false;
//e.printStackTrace();
}
}
return flag;
}
}错误:
java.lang.NullPointerException
mianyang.sanhe.ImportExcel.insertDB(ImportExcel.java:153)
mianyang.sanhe.ImportExcel.doPost(ImportExcel.java:51)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.ParseException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; public class ImportExcel extends HttpServlet{
static Connection con;
PreparedStatement pst;
private String filepath; public void getexcelname(String filepath){
this.filepath=filepath;
} /**
*
*/
private static final long serialVersionUID = -4528281625469749382L; public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
doPost(request, response);
}
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
filepath=request.getParameter("excelname").toString();
response.setContentType("text/html");
response.setCharacterEncoding("gb2312");
PrintWriter out = response.getWriter();
try {
if(filepath==null||filepath=="")
{
out.print("<script>alert('没有发现 文件')</script>");
}
if (insertDB(filepath)){
out.print("<script>alert('导入数据成功')</script>");
}
else
{
out.print("<script>alert('导入数据失败')</script>");
}
} catch (ParseException e) {
// TODO Auto-generated catch block
out.print("<script>alert('导入数据失败')</script>");
}
}
public boolean insertDB(String filePath) throws ParseException{
boolean flag=true;
try {
//文件流指向excel文件
FileInputStream fin=new FileInputStream(filePath);
HSSFWorkbook workbook=new HSSFWorkbook(fin);//创建工作薄
HSSFSheet sheet=workbook.getSheetAt(0);//得到工作表
HSSFRow row=null;//对应excel的行
HSSFCell cell=null;//对应excel的列
int totalRow=sheet.getLastRowNum();//得到excel的总记录条数
//以下的字段一一对应数据库表的字段
String name="";
String p_no="";
String code="";
int buyprice=0;
int saleprice=0;
int prostruc=0;
String productstruc="";
int etprice=0;
float productper=0.0f;
String sql="insert into products(name,p_no,code,buyprice,saleprice,prostruc,productstruc,etprice,productper) values(?,?,?,?,?,?,?,?,?)";
//SEQ_BOOK.NEXTVAL为数据库表序列
for(int i=2;i<=totalRow;i++){
row=sheet.getRow(i);
cell=row.getCell(1);
name=cell.getRichStringCellValue().toString();
cell=row.getCell(2);
p_no=cell.getRichStringCellValue().toString();
cell=row.getCell(3);
code=cell.getRichStringCellValue().toString();
cell=row.getCell(4);
//格式化字符串时间
buyprice=Integer.parseInt(cell.getRichStringCellValue().toString());
cell=row.getCell(5);
saleprice=Integer.parseInt(cell.getRichStringCellValue().toString());
cell=row.getCell(6);
if(cell.getRichStringCellValue().toString()=="单品"){
prostruc=0;
}
else
{
prostruc=1;
}
cell=row.getCell(7);
productstruc=cell.getRichStringCellValue().toString();
cell=row.getCell(8);
etprice=Integer.parseInt(cell.getRichStringCellValue().toString());
cell=row.getCell(9);
productper=Float.parseFloat(cell.getRichStringCellValue().toString()); BaseDAO dao = new BaseDAO();
con=dao.getConnection();
pst=con.prepareStatement(sql);
pst.setString(1,name);
pst.setString(2,p_no);
pst.setString(3,code);
pst.setInt(4,buyprice);
pst.setInt(5,saleprice);
pst.setInt(6,prostruc);
pst.setString(7,productstruc);
pst.setInt(8,etprice);
pst.setFloat(9,productper);
pst.execute();
}
flag=true;
} catch (FileNotFoundException e) {
flag=false;
//e.printStackTrace();
} catch(IOException ex){
flag=false;
//ex.printStackTrace();
} catch(SQLException exx){
flag=false;
//exx.printStackTrace();
}finally{
try {
pst.close();
con.close();
} catch (SQLException e) {
flag=false;
//e.printStackTrace();
}
}
return flag;
}
}错误:
java.lang.NullPointerException
mianyang.sanhe.ImportExcel.insertDB(ImportExcel.java:153)
mianyang.sanhe.ImportExcel.doPost(ImportExcel.java:51)
javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
怎么会错了
这个应该放在循环体内部吧
productper=Float.parseFloat(cell.getRichStringCellValue().toString());