fork download
  1. package org.ruler.sample;
  2.  
  3. import java.sql.SQLException;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8.  
  9. import org.ruler.Client;
  10. import org.ruler.Mode;
  11. import org.ruler.Provider;
  12. import org.ruler.Server;
  13.  
  14. public class Main {
  15.  
  16. public static void main(String[] args) {
  17. try {
  18. new Main().test();
  19. } catch (Exception e) {
  20. // TODO Auto-generated catch block
  21. e.printStackTrace();
  22. }
  23. }
  24.  
  25.  
  26.  
  27. // TODO Auto-generated method stub
  28. Server server = new Server(Mode.tcp); // HSQL localhost
  29. server.start();
  30.  
  31. // Provider likes EntityManagerFactory.class of JPA or Hibernate etc...
  32. Provider provider = new Provider(Mode.tcp); // HSQL localhost
  33. provider.bind(User.class, () -> new User());
  34. provider.open();
  35.  
  36.  
  37. // Cliant likes EntityManager.class of JPA or Hibernate etc...
  38. try(Client client = provider.open()){ // AutoCloseable.close().
  39.  
  40. // 前回のデータが残っていたらドロップする。
  41. client.table.drop(User.class);
  42.  
  43. client.table.create(User.class);
  44.  
  45. for(int i=0; i<100; i++){
  46.  
  47. // CRUD(Create)
  48. User user = client.insert(User.class);
  49.  
  50. // CRUD(Update)
  51. user.name.update("Java Entity Ruler");
  52. user.age.update(i);
  53. client.update(user);
  54. }
  55.  
  56.  
  57. Map<Integer, User> map = new HashMap<>();
  58. List<User> list = new ArrayList<>();
  59.  
  60. // CRUD(Read)
  61. client.find(User.class, (e) -> {
  62.  
  63. // Event e == JDBC ResultSet, that will be closed after lambda.
  64.  
  65. int count = e.count(); // all size (SQL COUNT(*))
  66. int index = e.index();
  67. User user = e.value();
  68.  
  69. // from 25% to 75%
  70. if(index > count/4 && index < count*3/4){
  71. // CRUD(Delete)
  72. client.remove(user);
  73.  
  74. // return value with Map<int, Entity>.
  75. int age = user.age.value();
  76. map.put(age, user);
  77.  
  78. // Entity value with List<Entity>.
  79. list.add(user);
  80. }
  81. });
  82.  
  83. for(User user : list){
  84. long id = user.id.value();
  85. String msg = "User id("+id+") is deleted.";
  86. System.out.println(msg);
  87. }
  88.  
  89. int age = 99;
  90. User user = map.get(age);
  91. if( user != null ){
  92. String msg = "User age("+age+") is deleted.";
  93. System.out.println(msg);
  94. }
  95.  
  96.  
  97. client.table.drop(User.class);
  98.  
  99. }
  100.  
  101. provider.close();
  102.  
  103. server.stop();
  104. server.dispose();
  105. }
  106. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:34: error: illegal start of expression
		provider.bind(User.class, () -> new User());
		                           ^
Main.java:34: error: illegal start of expression
		provider.bind(User.class, () -> new User());
		                              ^
Main.java:62: error: illegal start of expression
			client.find(User.class, (e) -> {
			                             ^
Main.java:62: error: illegal start of expression
			client.find(User.class, (e) -> {
			                               ^
Main.java:62: error: ';' expected
			client.find(User.class, (e) -> {
			                                ^
Main.java:82: error: illegal start of expression
			});
			 ^
Main.java:102: error: <identifier> expected
		provider.close();
		              ^
Main.java:104: error: <identifier> expected
		server.stop();
		           ^
Main.java:105: error: <identifier> expected
		server.dispose();
		              ^
Main.java:107: error: class, interface, or enum expected
}
^
10 errors
stdout
Standard output is empty