Advertisement
Kawalek

Untitled

Jan 17th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. public static void main(String[] args) {
  2. Scanner sc = new Scanner(System.in);
  3. Connection con;
  4. Statement st;
  5. ResultSet rs;
  6. String test;
  7. String url = "jdbc:mysql://localhost/userzy"; //w miejscu kropek należy podać nazwę bazy
  8. System.out.println("Login:");
  9. String user = sc.nextLine();
  10. System.out.println("Hasło:");
  11. String pass = sc.nextLine();
  12. try {
  13. con = DriverManager.getConnection(url, user, pass);
  14. do{
  15. System.out.println("wybierz opcję a,b,c lub d: ");
  16. switch (test=sc.nextLine()) {
  17. case "a": {
  18. try {
  19. con = DriverManager.getConnection(url, user, pass);
  20. st = con.createStatement();
  21. System.out.println("Wyświetlanie danych: ");
  22. rs = st.executeQuery("SELECT * FROM dane_osobowe"); //zapytanie SQL
  23. while (rs.next()) {
  24. System.out.println(rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3));
  25. }
  26. } catch (SQLException ex) {
  27. System.out.println("błąd SQL");
  28. }
  29. break;
  30. }
  31. case "b": {
  32. try {
  33. con = DriverManager.getConnection(url, user, pass);
  34. st = con.createStatement();
  35. System.out.println("Podaj dane do dodania: ");
  36. String input = sc.nextLine();
  37. String[] array = input.split(" ");
  38. String sql="INSERT INTO dane_osobowe (id, Imie, Nazwisko) VALUES (?, ?, ?)"; //zapytanie SQL
  39. PreparedStatement pst = con.prepareStatement(sql);
  40. pst.setString(1, array[0]);
  41. pst.setString(2, array[1]);
  42. pst.setString(3, array[2]);
  43. pst.execute();
  44. } catch (SQLException ex) {
  45. System.out.println("błąd SQL");
  46. }
  47. break;
  48. }
  49. case "c": {
  50. try {
  51. con = DriverManager.getConnection(url, user, pass);
  52. st = con.createStatement();
  53. rs = st.executeQuery("SELECT * FROM dane_osobowe"); //zapytanie SQL
  54. while (rs.next()) {
  55. System.out.println(rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3));
  56. }
  57. System.out.println("Podaj id danej, które chcesz usunąć: ");
  58. String input = sc.nextLine(); String[] array = input.split(" ");
  59. String sql="DELETE FROM dane_osobowe WHERE id= ?"; //zapytanie SQL
  60. PreparedStatement pst = con.prepareStatement(sql);
  61. pst.setString(1, input);
  62. pst.execute();
  63. } catch (SQLException ex) {
  64. System.out.println("błąd SQL");
  65. }
  66. break;
  67. }
  68. case "d": {
  69. try {
  70. con = DriverManager.getConnection(url, user, pass);
  71. st = con.createStatement();
  72. System.out.println("Według czego posortować tabele (id, Imie, Nazwisko)?: ");
  73. String input = sc.nextLine();
  74. rs = st.executeQuery("SELECT * FROM dane_osobowe ORDER BY " + input);
  75. while (rs.next()) {
  76. System.out.println(rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3));
  77. }
  78. } catch (SQLException ex) {
  79. System.out.println("błąd SQL");
  80. }
  81. break;
  82. }
  83.  
  84. }}while(!test.equals("exit"));
  85. }catch (SQLException ex) {
  86. System.out.println("Bład logowania");
  87. }
  88. }
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement