Java Veritabanına Bağlanmak ve Delete Operasyonu Örnek Kodu
Java ile Veri tabanına eriştiğimiz ve Delete operasyonu ile tablodaki seçtiğimiz veriyi sildiğimiz bir örnek kod. MysQl bağlantısı için kullandığımız örnek kod ise alt kısımdadır onu bir class içerisine alıp kolaylık sağladık.
Connection connection = null; DbHelper helper = new DbHelper(); PreparedStatement statement = null; ResultSet resultSet; try { connection = helper.getConnection(); String sql = "delete from city where id = ?"; statement = connection.prepareStatement(sql); statement.setInt(1, 3358); int result = statement.executeUpdate(); System.out.println("Kayıt Silindi"); } catch (SQLException exception) { helper.showErrorMessage(exception); } finally { { statement.close(); connection.close(); } }
DBHelper Class:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DbHelper { private String userName= "root"; private String password="12345"; private String dbUrl="jdbc:mysql://localhost:3306/world?useSSL=false&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"; public Connection getConnection() throws SQLException { return DriverManager.getConnection(dbUrl,userName,password); } public void showErrorMessage(SQLException exception){ System.out.println("Error : " + exception.getMessage()); System.out.println("Hata Kodu: " + exception.getErrorCode()); } }
Yorumlar
Yorum Gönder
Yorumunuz için teşekkür ederiz, en kısa sürede geri dönüş yapacağız.