added preparedStatement
This commit is contained in:
parent
cbf60e8c99
commit
e42af6bffd
@ -1,16 +1,22 @@
|
|||||||
package at.ionas999;
|
package at.ionas999;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.*;
|
||||||
import java.sql.DriverManager;
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
try {
|
try (Connection con = DriverManager.getConnection("jdbc:derby:derbyDb")) {
|
||||||
Connection con = DriverManager.getConnection("jdbc:derby:derbyDb");
|
|
||||||
con.setAutoCommit(true);
|
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");
|
ResultSet resultSet = con.createStatement().executeQuery("select * from CONTACT");
|
||||||
|
|
||||||
while (resultSet.next()) {
|
while (resultSet.next()) {
|
||||||
@ -18,6 +24,7 @@ public class Main {
|
|||||||
String name = resultSet.getString(2);
|
String name = resultSet.getString(2);
|
||||||
System.out.println("Id: " + id + ", Name: " + name);
|
System.out.println("Id: " + id + ", Name: " + name);
|
||||||
}
|
}
|
||||||
|
resultSet.close();
|
||||||
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user