我被赋予了从Excel工作表中获取数据并在MySQL数据库中更新的任务,该数据库有2列USN和Name。问题是USN正在打印行和名称。例如,如果我将其保存在DB中有12条记录,则会插入24条记录。我也在使用Swing概念。以下是我的代码。
class OpenClass implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Workbook w ;
chooser = new JFileChooser();
JFileChooser chooser = new JFileChooser();
int option = chooser.showOpenDialog(ExcelFileUploading.this);
if(option == JFileChooser.APPROVE_OPTION)
{
try
{
w = Workbook.getWorkbook(chooser.getSelectedFile());
Sheet sheet = w.getSheet(0);
for(int i = 0; i<sheet.getRows(); i++)
{
for(int j=0; j<sheet.getColumns();j++)
{
Cell cell = null;
if(j==0)
{
cell = sheet.getCell(j, i);
if(checkIfNumber(cell.getContents().toString())== false)
{
//System.out.print("\t\t");
continue;
}
System.out.print("\n");
}
cell = sheet.getCell(j, i);
CellType type = cell.getType();
if((type == CellType.LABEL)|| (type == CellType.NUMBER))
{
try
{
Class.forName(driver);
con = DriverManager.getConnection(url+dbName,username,password);
stmt = con.createStatement();
String query = "INSERT INTO student (USN,Name)"
+"VALUES('"+cell.getContents()+"','"+cell.getContents()+"')";
stmt.executeUpdate(query);
} catch (Exception exp)
{
exp.printStackTrace();
}
finally
{
try
{
stmt.close();
con.close();
} catch (SQLException exp)
{
}
}
}
}
}
} catch (Exception exp)
{
exp.printStackTrace();
}
display.setText("Opened File : " +((chooser.getSelectedFile()!=null)?
chooser.getSelectedFile().getName(): "nothing"));
}
if(option == JFileChooser.CANCEL_OPTION)
{
display.setText("Open Operation Cancelled");
}
}
private boolean checkIfNumber(String string)
{
try
{
double d = Double.parseDouble(string);
//System.out.print(d);
return true;
} catch (NumberFormatException ne)
{
return false;
}
}
}
由于我的Db表(USN,Name)中有2列,我必须将cell.getContents()
放两次。
任何人都可以帮助我吗?
答案 0 :(得分:1)
您遍历Excel中的所有行,然后遍历Excel中的所有列。 您使用2列不需要第二个循环。 此外,您只有一个Cell对象并且只保留对单元格的一个引用,但是当您创建insert语句时,您为SQL中的两个值分配相同的单元格
所以如果你有2行(用“,”字符分隔的列:
您的SQL语句是:
INSERT INTO student (USN,Name)VALUES('1','1')
INSERT INTO student (USN,Name)VALUES('11','11')
INSERT INTO student (USN,Name)VALUES('2','2')
INSERT INTO student (USN,Name)VALUES('12','12')
你想要的是这样的,我们每行:
所以,如果没有实际的mysql连接/插入,我认为你更喜欢这样的东西:
public class OpenClass {
public static void main(String args[]) {
new OpenClass().testIt();
}
public void testIt() {
Workbook w;
JFileChooser chooser = new JFileChooser();
int option = chooser.showOpenDialog(null);
//CREATE TWO CELL OBJECTS
Cell cell1, cell2;
if (option == JFileChooser.APPROVE_OPTION) {
try {
w = Workbook.getWorkbook(chooser.getSelectedFile());
Sheet sheet = w.getSheet(0);
//ITERATE BY ROWS
for (int i = 0; i < sheet.getRows(); i++) {
//GET AND CHECK COLUMN1 VALUE
cell1 = sheet.getCell(0, i);
if (checkIfNumber(cell1.getContents().toString()) == false)
continue;
//GET CHECK COLUMN2 VALUE
cell2 = sheet.getCell(1, i);
CellType type = cell2.getType();
if ((type == CellType.LABEL) || (type == CellType.NUMBER)) {
String query = "INSERT INTO student (USN,Name)"
+ "VALUES('" + cell1.getContents() + "','"
+ cell2.getContents() + "')";
System.out.println(query);
//RUN YOUR QUERY HERE!
}
}
} catch (BiffException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private boolean checkIfNumber(String string) {
try {
double d = Double.parseDouble(string);
//System.out.print(d);
return true;
} catch (NumberFormatException ne) {
return false;
}
}
}
最后,由于扩展的开销,连接只应创建一次到数据库。 jexcelapi库代码与您的代码完美配合,因此您必须使用该库。