DBMS Exp
DBMS Exp
import java.sql.*;
import java.util.Scanner;
// update username
// update password
public UserDatabase() {
try {
// Establish connection
if (connection != null) {
} catch (SQLException e) {
e.printStackTrace();
}
public void addUser(int id, String name, int age) {
String sql = "INSERT INTO test1 (id, name, age) VALUES (?, ?, ?)";
pstmt.setInt(1, id);
pstmt.setString(2, name);
pstmt.setInt(3, age);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
ResultSet rs = pstmt.executeQuery()) {
while (rs.next()) {
System.out.println("" + rs.getInt("id") +
"\t" + rs.getString("name") +
"\t" + rs.getInt("age"));
} catch (SQLException e) {
e.printStackTrace();
}
public void updateUser(int id, String name, int age) {
pstmt.setString(1, name);
pstmt.setInt(2, age);
pstmt.setInt(3, id);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
pstmt.setInt(1, id);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
while (true) {
System.out.println("\nChoose an option:");
System.out.println("1. Add User");
System.out.println("5. Exit");
switch (choice) {
case 1:
int id = scanner.nextInt();
break;
case 2:
System.out.println("All users:");
getAllUsers();
break;
case 3:
break;
case 4:
deleteUser(deleteId);
break;
case 5:
System.out.println("Exiting...");
scanner.close();
return;
default:
userDB.start();
}
MYSQL CODE:
USE currency;
name VARCHAR(50),
age INT
);