public class MainApp { public static void adaugare_winx(Connection conn){
Scanner scanner=new Scanner(System.in); String name=scanner.next();
int age=scanner.nextInt(); String birthday=scanner.next();
String comanda="insert into fairies (name, age, birthday) values (?,?,?)"; try(PreparedStatement prep=conn.prepareStatement(comanda)){
prep.setString(1,name); prep.setInt(2,age);
prep.setString(3, birthday); prep.executeUpdate();
}catch(SQLException e){ e.printStackTrace();
} }
public static void afisare(Connection conn)
{ String comanda="select * from fairies";
try(Statement statement=conn.createStatement()){ ResultSet resultSet=statement.executeQuery(comanda);
while(resultSet.next()){ int id=resultSet.getInt("id");
String name=resultSet.getString("name"); int age=resultSet.getInt("age");
String birthday=resultSet.getString("birthday"); System.out.println(id+ " "+name+" "+age+" "+birthday);
} }catch(SQLException e){
e.printStackTrace(); }
} public static void main(String[] args) throws SQLException {
try{ String url = "jdbc:mysql://localhost:3306/winx";
Connection connection= DriverManager.getConnection(url, "root", ""); adaugare_winx(connection);
afisare(connection);
}catch (SQLException e){ e.printStackTrace();
} }
}