代码如下:
package com.dataprocess;
import java.util.List;
public interface DataImport
{
//查询数据表中存在的所有数据
public List<Object> getDBData();
//查询excel文件中存在的所有数据
public List<Object> getExcelData(String filePath);
//插入数据到数据库
public int insert(Object object);
//跟新数据库中的数据
public int update(Object object);
}
具体的实现类:
package com.importdate;
import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import jxl.Sheet;
import jxl.Workbook;
import com.dataprocess.DataImport;
import com.wgh.db.ConnDB;
import commerce.Member;
public class Test implements DataImport
{
private Member member;
private int i=0;
private Connection conn;
public List<Object> getDBData()
{
List<Member> list=new ArrayList<Member>();
try
{
ConnDB connDB=new ConnDB();
String sql="select * from tb_member";
ResultSet rs=connDB.executeDBQuery(sql);
while(rs.next())
{
int ID=rs.getInt("ID");
String userName=rs.getString("userName");
String trueName=rs.getString("trueName");
String passWord=rs.getString("passWord");
String city=rs.getString("address");
String address=rs.getString("address");
String postCode=rs.getString("cardNo");
String cardNo=rs.getString("cardType");
String cardType=rs.getString("cardType");
int grade=rs.getInt("grade");
double amount=rs.getDouble("amount");
String tel=rs.getString("tel");
String email=rs.getString("email");
int freeze=rs.getInt("freeze");
list.add(new Member(ID,userName,trueName,passWord,city,address,postCode,cardNo,cardType,grade,amount,tel,email,freeze));
}
}catch(Exception ex)
{
ex.printStackTrace();
}
return list;
}
public List<Object> getExcelData(String filePath)
{
List<Member> list=new ArrayList<Member>();
try
{
Workbook wk=Workbook.getWorkbook(new File(filePath));
Sheet st=wk.getSheet(0);
int rows=st.getRows();
int cols=st.getColumns();
for(int i=1;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
//getCell(int col,int row),第一个参数为列数,第二个参数为行数
String ID=st.getCell(j++,i).getContents();
String userName=st.getCell(j++,i).getContents();
String trueName=st.getCell(j++,i).getContents();
String passWord=st.getCell(j++,i).getContents();
String city=st.getCell(j++,i).getContents();
String address=st.getCell(j++,i).getContents();
String postCode=st.getCell(j++,i).getContents();
String cardNo=st.getCell(j++,i).getContents();
String cardType=st.getCell(j++,i).getContents();
String grade=st.getCell(j++,i).getContents();
String amount=st.getCell(j++,i).getContents();
String tel=st.getCell(j++,i).getContents();
String email=st.getCell(j++,i).getContents();
String freeze=st.getCell(j++,i).getContents();
list.add(new Member(Integer.parseInt(ID),userName,trueName,passWord,city,address,postCode,cardNo,cardType,Integer.parseInt(grade),Double.parseDouble(amount),tel,email,Integer.parseInt(freeze)));
}
}
}catch(Exception ex)
{
ex.printStackTrace();
}
return list;
}
public int insert(Object object)
{
this.member=(Member)object;
conn=ConnDB.getDBConnection();
String sql="insert into tb_member (ID,userName,trueName,passWord,city,address,postCode,cardNo,cardType,grade,amount,tel,email,freeze)" +
"values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
try
{
PreparedStatement ps=conn.prepareStatement(sql);
ps.setInt(1, member.getID());
ps.setString(2, member.getUserName());
ps.setString(3, member.getTrueName());
ps.setString(4, member.getPassWord());
ps.setString(5, member.getCity());
ps.setString(6, member.getAddress());
ps.setString(7, member.getPostCode());
ps.setString(8, member.getCardNo());
ps.setString(9, member.getCardType());
ps.setInt(10, member.getGrade());
ps.setDouble(11, member.getAmount());
ps.setString(12, member.getTel());
ps.setString(13, member.getEmail());
ps.setInt(14, member.getFreeze());
i=ps.executeUpdate();
}catch(Exception ex)
{
ex.printStackTrace();
}
return i;
}
public int update(Object object)
{
this.member=(Member)object;
conn=ConnDB.getDBConnection();
String sql="update tb_member set userName=?,trueName=?,passWord=?,city=?,address=?,postCode=?,cardNo=?,cardType=?,grade=?,amount=?,tel=?,email=?,freeze=? where ID=?";
try
{
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1, member.getUserName());
ps.setString(2, member.getTrueName());
ps.setString(3, member.getPassWord());
ps.setString(4, member.getCity());
ps.setString(5, member.getAddress());
ps.setString(6, member.getPostCode());
ps.setString(7, member.getCardNo());
ps.setString(8, member.getCardType());
ps.setInt(9, member.getGrade());
ps.setDouble(10, member.getAmount());
ps.setString(11, member.getTel());
ps.setString(12, member.getEmail());
ps.setInt(13, member.getFreeze());
ps.setInt(14, member.getID());
i=ps.executeUpdate();
}catch(SQLException ex)
{
ex.printStackTrace();
}
return i;
}
}
javabean实体类:
package commerce;
public class Member
{
private int ID;
private String userName;
private String trueName;
private String passWord;
private String city;
private String address;
private String postCode;
private String cardNo;
private String cardType;
private int grade;
private double amount;
private String tel;
private String email;
private int freeze;
public Member()
{
}
public Member(int ID,String userName,String trueName,String passWord,String city,String address,String postCode,String cardNo,String cardType,int grade,double amount,String tel,String email,int freeze)
{
this.ID=ID;
this.userName=userName;
this.trueName=trueName;
this.passWord=passWord;
this.city=city;
this.address=address;
this.postCode=postCode;
this.cardNo=cardNo;
this.cardType=cardType;
this.grade=grade;
this.amount=amount;
this.tel=tel;
this.email=email;
this.freeze=freeze;
}
public int getID()
{
return ID;
}
public void setID(int iD)
{
ID = iD;
}
public String getUserName()
{
return userName;
}
public void setUserName(String userName)
{
this.userName = userName;
}
public String getTrueName()
{
return trueName;
}
public void setTrueName(String trueName)
{
this.trueName = trueName;
}
public String getPassWord()
{
return passWord;
}
public void setPassWord(String passWord)
{
this.passWord = passWord;
}
public String getCity()
{
return city;
}
public void setCity(String city)
{
this.city = city;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
public String getPostCode()
{
return postCode;
}
public void setPostCode(String postCode)
{
this.postCode = postCode;
}
public String getCardNo()
{
return cardNo;
}
public void setCardNo(String cardNo)
{
this.cardNo = cardNo;
}
public String getCardType()
{
return cardType;
}
public void setCardType(String cardType)
{
this.cardType = cardType;
}
public int getGrade()
{
return grade;
}
public void setGrade(int grade)
{
this.grade = grade;
}
public double getAmount()
{
return amount;
}
public void setAmount(double amount)
{
this.amount = amount;
}
public String getTel()
{
return tel;
}
public void setTel(String tel)
{
this.tel = tel;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public int getFreeze()
{
return freeze;
}
public void setFreeze(int freeze)
{
this.freeze = freeze;
}
}
这是myeclipse的在return list提示错误:
Type mismatch: cannot convert from List<Member> to List<Object>
因为数据库很多数据表,导入每个数据表都会用到下面的四个方法
//查询数据表中存在的所有数据
public List<Object> getDBData();
//查询excel文件中存在的所有数据
public List<Object> getExcelData(String filePath);
//插入数据到数据库
public int insert(Object object);
//跟新数据库中的数据
public int update(Object object);
所以我就想着用接口,实现接口,就用了泛型。因为不同表的字段和数据类型都不一样,javabean也就不一样。所以实现类中具体的对象由具体的表的实体决定。但是出现cannot convert from List<Member> to List<Object>的错误,我也不知道怎么处理。还望大神求解!
------解决思路----------------------
遍历,把每个都转回来