Note that there are some explanatory texts on larger screens.

plurals
  1. POCheck for duplicates in database before inserting
    text
    copied!<p>I need to check in database if record already exist before I insert into table. I have two tables, one that I insert data and one log table that is referenced to main table. In this log table I have ID, date_import and file_import. So I want to check if file_import already exist.</p> <p>Here is my scratch code:</p> <pre><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&lt;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; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload