Check for duplicates in database before inserting
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.
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++开发者_StackOverflow中文版) {
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 your question, but if you send
SELECT * FROM dbtest.T_AJPES_TR_LOG WHERE file_import='MyFileName.xml';
to the database, you'll either get an empty result set (= actual filename is new) or a set with 1..n entries (= filename is known and has been logged 1..n times).
Put it in quotes, as Knubo said:
String query = String.format(
"SELECT * FROM dbtest.T_AJPES_TR_LOG WHERE File_import='%s'", name);
uprs1 = stmt1.executeQuery(query);
You're doing this the hard way. Most databases have the ability to prevent duplicate entries (though I'm not familiar with mySQL specifically). Assuming your RDBMS supports it, simply set up "unique key" for the column(s) in question, & let the database do the duplicate detection for you. Doing it in your own code is simply over-engineering & redundant.
If your RDMS doesn't support such a basic feature, it's time to consider an upgrade.
精彩评论