我的任务是从 Excel 工作表中获取数据并在 MySQL 数据库中更新,该数据库有 2 列 USN 和名称。问题是 USN 在行和名称中都打印。例如,如果有 12 条记录,当我将它保存在 DB 中时,它将插入 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 表中有 2 列(USN、名称),所以我不得不将 cell.getContents()
放置两次。
谁能帮帮我?
最佳答案
您遍历 excel 中的所有行,然后遍历 excel 中的所有列。 您使用 2 列不需要第二个循环。 此外,您只有一个 Cell 对象并且只保留一个对单元格的引用,但是当您创建插入语句时,您为 SQL 中的两个值分配了相同的单元格
因此,如果您有 2 行(用“,”字符分隔的列:
- 1,11
- 2,22
你的 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')
你想要的是这样的,对于每一行我们:
- 检查第一个单元格是否为字符串,如果是则分配给 cell1。
- 检查第二个单元格是否适合输入。如果不是,请转到下一行和第 1 步。
- 使用 Cell1 和 Cell2 创建 SQL 语句。
所以,如果没有实际的 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 库代码与您的代码完美配合,因此您一定在使用该库。
关于java - 在数据库中插入冗余数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14643542/