java - Check for duplicates in database before inserting -
i need check in database if record exist before insert table. have 2 tables, 1 insert data , 1 log table referenced main table. in log table have id, date_import , file_import. want check if file_import exist.
here 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 * dbtest.t_ajpes_tr_log"); stmt.executeupdate(mysql_new_table); resultset uprs = stmt.executequery("select * dbtest.t_ajpes_tr"); //select init //read file file folder = new file(readfolder); string[] filename = folder.list(); (;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 uprs.next(); file_exists = uprs1.getstring("file_import"); if (!file_exists.equals(name)) { uprs1.afterlast(); uprs1.movetoinsertrow(); uprs1.updatestring("date_import",datef.format(date)); uprs1.updatestring("file_import",name); uprs1.insertrow(); i=0;
i may have misunderstood question, if send
select * dbtest.t_ajpes_tr_log file_import='myfilename.xml';
to database, you'll either empty result set (= actual filename new) or set 1..n entries (= filename known , has been logged 1..n times).
put in quotes, knubo said:
string query = string.format( "select * dbtest.t_ajpes_tr_log file_import='%s'", name); uprs1 = stmt1.executequery(query);
Comments
Post a Comment