Results 1 to 4 of 4
- 12-02-2010, 11:25 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 2
- Rep Power
- 0
Check for duplicates before inserting into mySQL table
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:
Java 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 ...Last edited by igor0203; 12-02-2010 at 12:21 PM.
- 12-02-2010, 01:57 PM #2
- 12-02-2010, 02:02 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
Why are you using a resultset to do this insert?
Just use an INSERT.
That's my first suggestion.
Second, the unique key is the way forward since that's what you want to do.
Loop round the file list you have doing an insert each time.
That insert will be in a try/catch block. On a failure you simply continue round the loop.
Also use a PreparedStatement rather than concatenating your inserts together.
- 12-02-2010, 02:14 PM #4
Member
- Join Date
- Dec 2010
- Posts
- 2
- Rep Power
- 0
Thanks for answer.
About resultset...since I'm beginner in this I'll leave it this way until I build working program. Then I'll optimize everything with preparedStatements etc.
Hm...where to insert this try catch?
As now I tried:
try {
uprs1.insertRow();} catch (SQLException e) {}
But it's not working properly.
edit: I'm googling for this problem for 2 days now and I really didn't find any solution that could work.Last edited by igor0203; 12-02-2010 at 02:18 PM.
Similar Threads
-
Help on inserting into table
By ShinTec in forum JDBCReplies: 4Last Post: 06-08-2010, 09:26 AM -
Problem regarding Inserting data into Mysql via JSP
By abhi118 in forum JDBCReplies: 5Last Post: 04-05-2010, 01:24 PM -
inserting multiple rows from one table into another
By xcallmejudasx in forum JDBCReplies: 1Last Post: 04-23-2009, 07:55 PM -
Inserting a Table in a TextFile?
By Keerti in forum Advanced JavaReplies: 5Last Post: 11-19-2008, 04:31 PM -
Inserting into a table (Example)
By Java Tip in forum Java TipReplies: 0Last Post: 02-06-2008, 09:25 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks