Java Veritabanına Bağlanmak ve Update Operasyonu Örnek Kodu
Java ile Veri tabanına eriştiğimiz ve Update operasyonu ile tablodaki veriyi güncellediğ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 = "update city set population=343434 ,district='Turkey' where id = ?"; statement = connection.prepareStatement(sql); statement.setInt(1, (city id`s,)4083); int result = statement.executeUpdate(); System.out.println("Kayıt Güncellendi"); } 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.