Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- Connection con;
- Statement st;
- ResultSet rs;
- String test;
- String url = "jdbc:mysql://localhost/userzy"; //w miejscu kropek należy podać nazwę bazy
- System.out.println("Login:");
- String user = sc.nextLine();
- System.out.println("Hasło:");
- String pass = sc.nextLine();
- try {
- con = DriverManager.getConnection(url, user, pass);
- do{
- System.out.println("wybierz opcję a,b,c lub d: ");
- switch (test=sc.nextLine()) {
- case "a": {
- try {
- con = DriverManager.getConnection(url, user, pass);
- st = con.createStatement();
- System.out.println("Wyświetlanie danych: ");
- rs = st.executeQuery("SELECT * FROM dane_osobowe"); //zapytanie SQL
- while (rs.next()) {
- System.out.println(rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3));
- }
- } catch (SQLException ex) {
- System.out.println("błąd SQL");
- }
- break;
- }
- case "b": {
- try {
- con = DriverManager.getConnection(url, user, pass);
- st = con.createStatement();
- System.out.println("Podaj dane do dodania: ");
- String input = sc.nextLine();
- String[] array = input.split(" ");
- String sql="INSERT INTO dane_osobowe (id, Imie, Nazwisko) VALUES (?, ?, ?)"; //zapytanie SQL
- PreparedStatement pst = con.prepareStatement(sql);
- pst.setString(1, array[0]);
- pst.setString(2, array[1]);
- pst.setString(3, array[2]);
- pst.execute();
- } catch (SQLException ex) {
- System.out.println("błąd SQL");
- }
- break;
- }
- case "c": {
- try {
- con = DriverManager.getConnection(url, user, pass);
- st = con.createStatement();
- rs = st.executeQuery("SELECT * FROM dane_osobowe"); //zapytanie SQL
- while (rs.next()) {
- System.out.println(rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3));
- }
- System.out.println("Podaj id danej, które chcesz usunąć: ");
- String input = sc.nextLine(); String[] array = input.split(" ");
- String sql="DELETE FROM dane_osobowe WHERE id= ?"; //zapytanie SQL
- PreparedStatement pst = con.prepareStatement(sql);
- pst.setString(1, input);
- pst.execute();
- } catch (SQLException ex) {
- System.out.println("błąd SQL");
- }
- break;
- }
- case "d": {
- try {
- con = DriverManager.getConnection(url, user, pass);
- st = con.createStatement();
- System.out.println("Według czego posortować tabele (id, Imie, Nazwisko)?: ");
- String input = sc.nextLine();
- rs = st.executeQuery("SELECT * FROM dane_osobowe ORDER BY " + input);
- while (rs.next()) {
- System.out.println(rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3));
- }
- } catch (SQLException ex) {
- System.out.println("błąd SQL");
- }
- break;
- }
- }}while(!test.equals("exit"));
- }catch (SQLException ex) {
- System.out.println("Bład logowania");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement