Advertisement
Guest User

Untitled

a guest
Feb 21st, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.07 KB | None | 0 0
  1. @Entity
  2. @Table (name = "post")
  3. public class Post {
  4.  
  5. @Id
  6. @GeneratedValue(strategy = GenerationType.IDENTITY)
  7. @Column (name = "id")
  8. private long id;
  9.  
  10. @Column (name = "title", nullable = false)
  11. private String title;
  12.  
  13. @Column (name = "summary", nullable = false)
  14. private String summary;
  15.  
  16. @Column (name = "body", nullable = false)
  17. private String body;
  18. @ManyToOne (cascade = CascadeType.REFRESH)
  19. @JoinColumn(name = "category_id")
  20. private Category category;
  21.  
  22. public long getId() {
  23. return id;
  24. }
  25.  
  26. public void setId(long id) {
  27. this.id = id;
  28. }
  29.  
  30. public String getTitle() {
  31. return title;
  32. }
  33.  
  34. public void setTitle(String title) {
  35. this.title = title;
  36. }
  37.  
  38. public String getSummary() {
  39. return summary;
  40. }
  41.  
  42. public void setSummary(String summary) {
  43. this.summary = summary;
  44. }
  45.  
  46. public String getBody() {
  47. return body;
  48. }
  49.  
  50. public void setBody(String body) {
  51. this.body = body;
  52. }
  53.  
  54. public Category getCategory() {
  55. return category;
  56. }
  57.  
  58. public void setCategory(Category category) {
  59. this.category = category;
  60. }
  61.  
  62. public Post(String title, String summary, String body, Category category) {
  63.  
  64. this.title = title;
  65. this.summary = summary;
  66. this.body = body;
  67. this.category = category;
  68. }
  69.  
  70. public Post() {
  71.  
  72. }
  73. public Post(ResultSet resultSet, Category category){
  74. try {
  75. this.id = resultSet.getInt("id");
  76. this.body = resultSet.getString("body");
  77. this.summary = resultSet.getString("summary");
  78. this.title = resultSet.getString("title");
  79. this.category = category;
  80. } catch (SQLException e) {
  81. e.printStackTrace();
  82. }
  83. }
  84. }
  85.  
  86. @Entity
  87. @Table(name = "category")
  88. public class Category {
  89.  
  90. @Id
  91. @GeneratedValue(strategy = GenerationType.IDENTITY)
  92. @Column(name = "id")
  93. private long id;
  94.  
  95. public String getCategoryName() {
  96. return categoryName;
  97. }
  98.  
  99. public void setCategoryName(String categoryName) {
  100. this.categoryName = categoryName;
  101. }
  102.  
  103. @Column(name = "category_name")
  104. private String categoryName;
  105. @OneToMany(mappedBy = "category")
  106. private List<Post> posts;
  107.  
  108. public long getId() {
  109. return id;
  110. }
  111.  
  112. public void setId(long id) {
  113. this.id = id;
  114. }
  115.  
  116. public List<Post> getPosts() {
  117. return posts;
  118. }
  119.  
  120. public void setPosts(List<Post> posts) {
  121. this.posts = posts;
  122. }
  123.  
  124. public Category() {
  125.  
  126. }
  127. }
  128.  
  129. public class QueryProperties {
  130.  
  131. Properties queries;
  132.  
  133. private final String URL = "jdbc:mysql://localhost:3306/postschema";
  134. private final String USERNAME = "root";
  135. private final String PASSWORD = "peroser12";
  136.  
  137. public QueryProperties() {
  138. loadProperties();
  139. }
  140.  
  141. public Connection getConnectionDB()
  142. {
  143. Connection connection = null;
  144. try {
  145. Driver driver = new FabricMySQLDriver();
  146. DriverManager.registerDriver(driver);
  147. connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
  148.  
  149. } catch (SQLException e) {
  150. System.err.println("Driver not found!"); }
  151.  
  152. return connection;
  153. }
  154.  
  155. public String getQuery (String name){
  156. System.out.println("Get query " + name);
  157. if (queries == null)
  158. loadProperties();
  159. String q = queries.getProperty(name);
  160. System.out.println(q);
  161. return q;
  162.  
  163. }
  164.  
  165. private void loadProperties(){
  166. FileInputStream fis;
  167. try {
  168. fis = new FileInputStream("src/main/resources/queries.properties");
  169. queries = new Properties();
  170. queries.load(fis);
  171. } catch (IOException e) {
  172. e.printStackTrace();
  173. }
  174. }
  175. }
  176.  
  177. public interface AbstractDao <T> {
  178.  
  179. public T create(T entity);
  180. public void delete(T entity);
  181. public T edit(T entity);
  182. public T getById(Integer id);
  183. public List<T> getAll();
  184.  
  185. }
  186.  
  187. public abstract class AbstractDaoImpl<T> implements AbstractDao<T> {
  188.  
  189. QueryProperties queryProperties = new QueryProperties();
  190.  
  191. public T create(T entity) {
  192. Connection connection = null;
  193. PreparedStatement pstmt = null;
  194. ResultSet resultSet = null;
  195. try {
  196. connection = queryProperties.getConnectionDB();
  197. String querys = getCreateQuery();
  198. pstmt = connection.prepareStatement(querys, Statement.NO_GENERATED_KEYS);
  199. fillCreateStatement(pstmt, entity);
  200. pstmt.executeUpdate();
  201.  
  202. resultSet = pstmt.getGeneratedKeys();
  203. if (resultSet.next()){
  204. Integer generatedId = resultSet.getInt(1);
  205. return getById(generatedId);
  206. }
  207. } catch (SQLException e) {
  208. e.printStackTrace();
  209. } finally {
  210. if (resultSet != null) try { resultSet.close(); } catch (SQLException logOrIgnore) {}
  211. if (pstmt != null) try { pstmt.close(); } catch (SQLException logOrIgnore) {}
  212. if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {}
  213. }
  214.  
  215.  
  216. return null;
  217. }
  218. }
  219.  
  220.  
  221. public class PostDao extends AbstractDaoImpl<Post> {
  222.  
  223. @Override
  224. public void fillCreateStatement(PreparedStatement pstmt, Post entity) {
  225. try {
  226. pstmt.setString(1, entity.getBody());
  227. pstmt.setString(2, entity.getSummary());
  228. pstmt.setString(3, entity.getTitle());
  229. pstmt.setLong(4, entity.getCategory().getId());
  230. } catch (SQLException e) {
  231. e.printStackTrace();
  232. }
  233. }
  234. }
  235.  
  236. public class Main {
  237. public static void main(String[] args) {
  238. PostDao postDao = new PostDao();
  239. Post post = new Post();
  240. Category category = new Category();
  241. category.setCategoryName("usdgliuidfl");
  242. post.setTitle("jgsdhjksddlgh");
  243. post.setSummary("skdjhgdgsd");
  244. post.setBody("jhgsdajkldgh");
  245. post.setCategory(category);
  246. postDao.create(post);
  247. System.out.println("Added!");
  248. }
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement