Advertisement
Guest User

Untitled

a guest
Sep 1st, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1.  
  2. import java.sql.*;
  3.  
  4. public class Main {
  5.  
  6. // CRUD - Create Read Update Delete
  7. // SQL ordene: SELECT = read, UPDATE = update, INSERT = create og DELETE = delete
  8.  
  9.  
  10. public static void main(String[] args){
  11. String dbURL = "jdbc:mysql://localhost:3306/sampledb"; // eller student db
  12. String username = "root";
  13. String password = "4Chan2k!";
  14.  
  15. try {
  16. Connection conn = DriverManager.getConnection(dbURL, username, password);
  17.  
  18. if (conn != null) {
  19. System.out.println("Connected");
  20. //insertUser(conn);
  21. //deleteUser(conn);
  22. //listUsers(conn);
  23. updateUser(conn);
  24. }
  25. } catch (SQLException ex) {
  26. ex.printStackTrace();
  27. }
  28. }
  29.  
  30. public static void insertUser(Connection conn){
  31. String sql = "INSERT INTO Users (username, password, fullname, email) VALUES (?, ?, ?, ?)";
  32.  
  33. PreparedStatement statement = null;
  34. try {
  35. statement = conn.prepareStatement(sql);
  36.  
  37. statement.setString(1, "M");
  38. statement.setString(2, "secretpa2ss");
  39. statement.setString(3, "Joakim Sælemyr");
  40. statement.setString(4, "bill.gates@microsoft.com");
  41.  
  42. int rowsInserted = statement.executeUpdate();
  43. if (rowsInserted > 0) {
  44. System.out.println("A new user was inserted successfully!");
  45. }
  46. } catch (SQLException e) {
  47. e.printStackTrace();
  48. }
  49. }
  50.  
  51. public static void deleteUser(Connection conn){
  52. String sql = "DELETE FROM SampleDB.users WHERE user_id=11";
  53.  
  54. PreparedStatement statement = null;
  55. try {
  56. statement = conn.prepareStatement(sql);
  57.  
  58. int rowsInserted = statement.executeUpdate();
  59. System.out.println("ADELTELTLELELELTLETKELKTLEKTLEKLE! " + rowsInserted);
  60.  
  61. } catch (SQLException e) {
  62. e.printStackTrace();
  63. }
  64. }
  65.  
  66. public static void listUsers(Connection conn){
  67. String sql = "SELECT * FROM Users";
  68.  
  69. try{
  70. Statement statement = conn.createStatement();
  71. ResultSet result = statement.executeQuery(sql);
  72.  
  73. int count = 0;
  74.  
  75. while (result.next()){
  76. String name = result.getString(2);
  77. String pass = result.getString(3);
  78. String fullname = result.getString("fullname");
  79. String email = result.getString("email");
  80.  
  81. String output = "User #%d: %s - %s - %s - %s";
  82. System.out.println(String.format(output, ++count, name, pass, fullname, email));
  83. }
  84. } catch(SQLException e){
  85. System.out.println("WAT DA FEK");
  86. }
  87. }
  88.  
  89. public static void updateUser(Connection conn){
  90. try{
  91. String sql = "UPDATE Users SET password=?, fullname=?, email=? WHERE username=?";
  92.  
  93. PreparedStatement statement = conn.prepareStatement(sql);
  94. statement.setString(1, "MORTEN2k");
  95. statement.setString(2, "Morten Amundsen");
  96. statement.setString(3, "Mortenamundsen@microsoft.com");
  97. statement.setString(4, "morten15");
  98.  
  99. int rowsUpdated = statement.executeUpdate();
  100. if (rowsUpdated > 0) {
  101. System.out.println("An existing user was updated successfully!");
  102. }
  103. } catch(SQLException e){
  104. System.out.println("A FEKK");
  105. }
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement