added preparedStatement

This commit is contained in:
Jonas Hinterdorfer 2025-03-27 11:28:07 +01:00
parent cbf60e8c99
commit e42af6bffd

View File

@ -1,16 +1,22 @@
package at.ionas999;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.*;
public class Main {
public static void main(String[] args) {
try {
Connection con = DriverManager.getConnection("jdbc:derby:derbyDb");
try (Connection con = DriverManager.getConnection("jdbc:derby:derbyDb")) {
con.setAutoCommit(true);
// Insert a new contact without specifying the ID
String insertSQL = "INSERT INTO CONTACT (NAME) VALUES (?)";
PreparedStatement insertPstmt = con.prepareStatement(insertSQL);
insertPstmt.setString(1, "John Doe"); // Set the Name value
int rows = insertPstmt.executeUpdate();
System.out.println("Rows inserted: " + rows);
insertPstmt.close();
// Non-prepared statement
ResultSet resultSet = con.createStatement().executeQuery("select * from CONTACT");
while (resultSet.next()) {
@ -18,6 +24,7 @@ public class Main {
String name = resultSet.getString(2);
System.out.println("Id: " + id + ", Name: " + name);
}
resultSet.close();
} catch (SQLException e) {
throw new RuntimeException(e);