Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在数据库中插入冗余数据_Java_Mysql_Swing_Excel - Fatal编程技术网

Java 在数据库中插入冗余数据

Java 在数据库中插入冗余数据,java,mysql,swing,excel,Java,Mysql,Swing,Excel,我的任务是从Excel工作表中获取数据并在MySQL数据库中更新,该数据库有两列USN和Name。问题是USN正在打印行和名称。例如,如果在DB中保存时有12条记录,它将插入24条记录。我也在使用Swing概念。下面是我的代码 class OpenClass implements ActionListener { public void actionPerformed(ActionEvent e) { Workbook w ; chooser = new JFileChoo

我的任务是从Excel工作表中获取数据并在MySQL数据库中更新,该数据库有两列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;
    }

}
 }
class OpenClass实现ActionListener
{
已执行的公共无效操作(操作事件e)
{
工作手册w;
chooser=newjfilechooser();
JFileChooser chooser=新的JFileChooser();
int option=chooser.showOpenDialog(ExcelFileUpload.this);
if(option==JFileChooser.APPROVE\u选项)
{       
尝试
{
w=Workbook.getWorkbook(chooser.getSelectedFile());
图纸=w.getSheet(0);

对于(int i=0;i),您将迭代excel中的所有行,然后迭代excel中的所有列。 如果使用2列,则不需要第二个循环。 另外,您只有一个单元格对象,并且对一个单元格只保留一个引用,但是当您创建insert语句时,您为SQL中的两个值分配了相同的单元格

因此,如果您有两行(以“,”字符分隔的列):

  • 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')
您想要的是这样的东西,其中每一行我们:

  • 检查第一个单元格是否为字符串,如果为字符串,则分配给单元格1
  • 检查第二个单元格的类型是否合适。转到下一行,如果不合适,请执行步骤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;
            }
    
        }
    
    }
    
    公共类OpenClass{
    公共静态void main(字符串参数[]){
    新的OpenClass().testIt();
    }
    公开无效测试(){
    工作手册w;
    JFileChooser chooser=新的JFileChooser();
    int option=chooser.showOpenDialog(null);
    //创建两个单元对象
    细胞1、细胞2;
    if(option==JFileChooser.APPROVE\u选项){
    试一试{
    w=Workbook.getWorkbook(chooser.getSelectedFile());
    图纸=w.getSheet(0);
    //按行迭代
    对于(int i=0;i

    最后,由于扩展的开销,只应创建一次到数据库的连接。jexcelapi库代码与您的代码完美配合,因此您必须使用该库。

    您对excel中的所有行进行迭代,然后对excel中的所有列进行迭代。 如果使用2列,则不需要第二个循环。 另外,您只有一个单元格对象,并且对一个单元格只保留一个引用,但是当您创建insert语句时,您为SQL中的两个值分配了相同的单元格

    因此,如果您有两行(以“,”字符分隔的列):

    • 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')
    
    您想要的是这样的东西,其中每一行我们:

  • 检查第一个单元格是否为字符串,如果为字符串,则分配给单元格1
  • 检查第二个单元格的类型是否合适。转到下一行,如果不合适,请执行步骤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;
            }
    
        }
    
    }
    
    公共类OpenClass{
    公共静态void main(字符串参数[]){
    新的OpenClass().testIt();
    }
    公开无效测试(){
    工作手册w;
    JFileChooser chooser=新的JFileChooser();
    int option=chooser.showOpenDialog(null);
    //创建两个单元对象
    细胞1、细胞2;
    if(option==JFileChooser.APPROVE\u选项){
    试一试{
    w=Workbook.getWorkbook(chooser.getSelectedFile());
    图纸=w.getSheet(0);
    //按行迭代
    对于(int i=0;i