我對我的問題沒有很好的答案,發現如何去做。只有在記錄不存在的情況下,纔會將記錄插入到表中:PL/SQL - 只在記錄不存在的情況下插入記錄
1)在您的模式上創建或替換函數(這是檢查2個參數,您可以將其設置爲與您一樣多希望)PL/SQL是非常具體的,複製和粘貼,因爲我寫了應該編譯成功。花了很多努力才使語法恰到好處。該函數檢查要寫入的表以及要檢查的相應列名是否已經一起存在。
create or replace function Found(
var1 type,
var2 type)
return number
is
numberOfSelectedRows number := 0;
begin
select count(*)
into numberOfSelectedRows
from TABLE
where COLUMN_NAME = var1
and COLUMN_NAME = var2;
return numberOfSelectedRows;
end Found;
2)編寫java執行pl/sql函數:這是用NetBeans完成的。當單擊該按鈕時,它將從其他表中加載FORM數據,並確定該表中是否已存在要插入的表中的記錄。
try {
DriverManager.registerDriver (new oracle.jdbc.OracleDriver());
con = DriverManager.getConnection(""+LOGIN.url+"");
String str1 = jTextField_VariableName.getText();
String str2 = jTextField_VariableName.getText();
String q = "insert into TABLE (var1 type, var2 type) VALUES ('"+str1+"', '"+str2+"')" ;
cs = con.prepareCall("{?=call Found(?, ?)}"); // cs = CallableStatement - defined in class CallableStatement cs = null;
cs.setString(2, str1);
cs.setString(3, str2);
cs.registerOutParameter(1, Types.INTEGER);
cs.execute();
if(cs.getInt(1)>= 1)
{
JOptionPane.showMessageDialog(null, " this record already exists");
}
else
{
try{
DriverManager.registerDriver (new oracle.jdbc.OracleDriver());
con = DriverManager.getConnection(""+LOGIN.url+"");
pst = con.prepareStatement(q);
pst.execute();
}catch(SQLException ex) {Logger.getLogger(REGISTER_STUDENT.class.getName()).log(Level.SEVERE, null, ex);}
}
} catch (SQLException ex) {Logger.getLogger(REGISTER_STUDENT.class.getName()).log(Level.SEVERE, null, ex);}
你可以看看Oracle [MERGE](http://www.oracle-bas e.com/articles/10g/merge-enhancements-10g.php)聲明。 – user75ponic