Advertisement
Guest User

Untitled

a guest
Nov 12th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. package ua.inf.smart.database;
  2.  
  3. import java.sql.*;
  4.  
  5. public class JDBCExample_2 {
  6.  
  7. private static final String URL = "jdbc:mysql://localhost:3306/bookkeeping";
  8. private static final String NAME = "avangard";
  9. private static final String PASS = "7654321";
  10.  
  11. public static void main(String[] args) {
  12.  
  13. try (Connection con = DriverManager.getConnection(URL, NAME, PASS);
  14. Statement stmt = con.createStatement()) {
  15.  
  16. stmt.execute("CREATE TABLE donations (\n" +
  17. " id INT NOT NULL AUTO_INCREMENT,\n" +
  18. " type VARCHAR(45) NULL,\n" +
  19. " amount INT NULL,\n" +
  20. " PRIMARY KEY (id))\n" +
  21. "DEFAULT CHARACTER SET = utf8;");
  22.  
  23. stmt.execute("INSERT INTO donations VALUES (777, 'name', 777)");
  24. stmt.execute("DELETE FROM donations WHERE id = 777");
  25.  
  26. stmt.execute("INSERT INTO donations (type, amount) VALUES ('Dollars', 2570)");
  27. stmt.execute("INSERT INTO donations (type, amount) VALUES ('Euros', 5032)");
  28. stmt.execute("INSERT INTO donations (type, amount) VALUES ('Pounds', 1259)");
  29. stmt.execute("INSERT INTO donations (type, amount) VALUES ('Real-Estate', 55300)");
  30. stmt.execute("INSERT INTO donations (type, amount) VALUES ('Food_supply', 550)");
  31.  
  32. stmt.executeUpdate("UPDATE donations SET type='Goods' WHERE type='Food_supply'");
  33. int changedRows =
  34. stmt.executeUpdate("UPDATE donations SET amount=2930 WHERE type='Goods'");
  35. System.out.println("Changed rows: " + changedRows);
  36.  
  37. stmt.addBatch("INSERT INTO donations (type, amount) VALUES ('Type_1', 1)");
  38. stmt.addBatch("INSERT INTO donations (type, amount) VALUES ('Type_1', 2)");
  39. stmt.addBatch("INSERT INTO donations (type, amount) VALUES ('Type_1', 3)");
  40. stmt.executeBatch();
  41.  
  42. stmt.clearBatch();
  43.  
  44. stmt.addBatch("INSERT INTO donations (type, amount) VALUES ('Type_4', 4)");
  45.  
  46. ResultSet resSet = stmt.executeQuery("SELECT * FROM donations");
  47. while (resSet.next()) {
  48. int id = resSet.getInt(1);
  49. String type = resSet.getString(2);
  50. int amount = resSet.getInt(3);
  51. System.out.println(id + ") " + type + ", " + amount);
  52. }
  53. resSet.close();
  54.  
  55. } catch (SQLException e) {
  56. e.printStackTrace();
  57. }
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement