JDBC Transaction Örneği - 04-03-2014

advertisement
JDBC Transaction Örne?i - 04-03-2014
by Burak - Burak Kutbay'?n Ki?isel Blog'u | - http://blog.burakkutbay.com
JDBC Transaction Örne?i
by Burak - Per?embe, Nisan 03, 2014
http://blog.burakkutbay.com/jdbc-transaction-uygulama-ornegi.html/
JDBC Dersleri'ne devam ediyoruz. Önceki derste Transtaction Kavram?n?n ne oldu?unu ve
metodlar?n ne i?e yarad???n? görmü?tük. ( Buradan )
Bunu bir örnekle peki?tirelim. Teoride anlatt???m? uygulamal? olarak bakal?m.
/*
* To change this license header, choose License Headers in Project Pr
operties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package jdbc_transtaction;
import
import
import
import
java.sql.Connection;
java.sql.DriverManager;
java.sql.SQLException;
java.sql.Statement;
/**
*
* @author BurakKutbay.com
*/
public class JDBC_Transtaction {
/**
* @param args the command line arguments
* @throws java.lang.ClassNotFoundException
* @throws java.sql.SQLException
*/
public static void main(String[] args) throws ClassNotFoundExcepti
on, SQLException {
// TODO code application logic here
{
String driverClass = "sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(driverClass);
String url = "jdbc:odbc:mydatasource";
String username = "burak";
String password = "kutbay";
1/2
JDBC Transaction Örne?i - 04-03-2014
by Burak - Burak Kutbay'?n Ki?isel Blog'u | - http://blog.burakkutbay.com
Connection baglanti;
baglanti = DriverManager.getConnection(url, username, pass
word);
try {
double para = 100;
String id = "Burak";
baglanti.setAutoCommit(false);
String sorgu = "update Para" + " set Kalan=Kalan-" + p
ara + " where id=" + id;
Statement stmtFrom = baglanti.createStatement();
stmtFrom.executeUpdate(sorgu);
String sorgu2 = "update Para" + " set Kalan=Kalan+" +
para + " where id=" + id;
Statement stmt = baglanti.createStatement();
int durum = stmt.executeUpdate(sorgu2);
if (durum > 0) {
baglanti.commit();
System.out.println("??lem Tamam");
} else {
baglanti.rollback();
System.out.println("??lem Geri Al?nd?");
}
} catch (SQLException e) {
baglanti.rollback();
System.out.println("Geri Al?nd?");
}
}
}
}
Örne?imizde yapt???m?z i?lemleri incelediyseniz veri aktar?m? yaparken Transaction'a göre otomatik
olarak kapatt?k, commit ve rollback'leri kullanarak istedi?imiz zaman sorgumuzu tümüyle gönderdik e?er
bir hata oldu?unda ise tüm i?lemi iptal ederek geri ald?k.
_______________________________________________
PDF generated by Kalin's PDF Creation Station
2/2
Powered by TCPDF (www.tcpdf.org)
Download