Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Check for duplicates before inserting into database

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Check for duplicates before inserting into database

    Hi!

    First I would like to say I'm sory for opening new thread about this problem but I googled and also searched this site for this problem but I didn't find anything that could do job for me.
    So on problem... I have two mySQL tables, T_AJPES_TR_LOG_ table(with ID,Date_import and File_import) and T_AJPES_TR table that I store data from XML files. In TR_LOG table I write filename from which I want to import file.
    What I would like to do is that before I insert filename in TR_LOG, I want to check if that file wasn't imported before. If it was imported then program tries to import next file otherwise import it.

    So here is my scratch code:
    //DATE
            DateFormat dateF = new SimpleDateFormat("dd.MM.yyyy");
            Date date = new Date();
    //DATE        
     
    //DB INIT
            String URL = "jdbc:mysql://192.168.1.128:3306";
            Connection con = (Connection) DriverManager.getConnection(URL,user,pass);
            Statement stmt = (Statement) con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            Statement stmt1 = (Statement) con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            String mySQL_log = ("CREATE TABLE IF NOT EXISTS dbtest.T_AJPES_TR_LOG"
                + "(ID INT unsigned PRIMARY KEY AUTO_INCREMENT, Date_import VARCHAR(45),"
                + "File_import VARCHAR(75)) ENGINE = INNODB");
            String mySQL_new_table = ("CREATE TABLE IF NOT EXISTS dbtest.T_AJPES_TR "
                + "(row_count INT PRIMARY KEY AUTO_INCREMENT,"
                + "rn CHAR(15),sSpre CHAR(5),reg CHAR(5),eno VARCHAR(10),davcna VARCHAR(15),Ime VARCHAR(75),"
                + "Priimek VARCHAR(75),ID_LOG INT unsigned,"
                + "CONSTRAINT FOREIGN KEY(ID_LOG) references T_AJPES_TR_LOG(ID) ON DELETE CASCADE ON UPDATE CASCADE) ENGINE = INNODB");
     
                stmt1.executeUpdate(mySQL_log);
                ResultSet uprs1 = stmt1.executeQuery("SELECT * FROM dbtest.T_AJPES_TR_LOG");
                stmt.executeUpdate(mySQL_new_table);
                ResultSet uprs = stmt.executeQuery("SELECT * FROM dbtest.T_AJPES_TR");
    //SELECT INIT
     
    //READ FILE        
            File folder = new File(readFolder);
            String[] fileName = folder.list();
     
            for (;k<fileName.length;k++) {
                name = fileName[k];
     
                if (name.contains(".xml")) {
                    FileInputStream fstream = new FileInputStream(readFolder + name);
                    DataInputStream in = new DataInputStream(fstream);
                    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    //READ FILE
     
                    uprs1.afterLast();
                    uprs1.moveToInsertRow();
                    uprs1.updateString("Date_import",dateF.format(date));
                    uprs1.updateString("File_import",name);
                    uprs1.insertRow();
                    i=0;

    This is not entire code from program, only part where I check if file is already imported or not. Further is only XML parsing part.

    edit: I tried to declare File_import as unique key but when it comes to uprs1.insertRow() then it throws duplicate error(if file was already imported).
    Also tried to move uprs1 = stmt.executeQuery("SELECT * FROM dbtest.T_AJPES_TR_LOG WHERE File_import" + name) resultset after for loop and then it throws me error unknown column File_import ...


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Check for duplicates before inserting into database

    Crossposted: Check for duplicates before inserting into mySQL table - Java Forums

Similar Threads

  1. Inserting Image Help
    By Bradshjo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 22nd, 2010, 12:50 PM
  2. how to check the value
    By javaking in forum Java Servlet
    Replies: 2
    Last Post: July 22nd, 2010, 06:56 AM
  3. treemap Duplicates
    By debug in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 6th, 2010, 11:52 AM
  4. Can Anyone Check This Link
    By arpitgadle in forum Java Servlet
    Replies: 5
    Last Post: October 7th, 2009, 08:56 AM
  5. Problem in AWT and IFrame implementaion
    By AZBOY2000 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: April 24th, 2009, 03:41 AM