Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Entity
- @Table (name = "post")
- public class Post {
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Column (name = "id")
- private long id;
- @Column (name = "title", nullable = false)
- private String title;
- @Column (name = "summary", nullable = false)
- private String summary;
- @Column (name = "body", nullable = false)
- private String body;
- @ManyToOne (cascade = CascadeType.REFRESH)
- @JoinColumn(name = "category_id")
- private Category category;
- public long getId() {
- return id;
- }
- public void setId(long id) {
- this.id = id;
- }
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public String getSummary() {
- return summary;
- }
- public void setSummary(String summary) {
- this.summary = summary;
- }
- public String getBody() {
- return body;
- }
- public void setBody(String body) {
- this.body = body;
- }
- public Category getCategory() {
- return category;
- }
- public void setCategory(Category category) {
- this.category = category;
- }
- public Post(String title, String summary, String body, Category category) {
- this.title = title;
- this.summary = summary;
- this.body = body;
- this.category = category;
- }
- public Post() {
- }
- public Post(ResultSet resultSet, Category category){
- try {
- this.id = resultSet.getInt("id");
- this.body = resultSet.getString("body");
- this.summary = resultSet.getString("summary");
- this.title = resultSet.getString("title");
- this.category = category;
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- @Entity
- @Table(name = "category")
- public class Category {
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- @Column(name = "id")
- private long id;
- public String getCategoryName() {
- return categoryName;
- }
- public void setCategoryName(String categoryName) {
- this.categoryName = categoryName;
- }
- @Column(name = "category_name")
- private String categoryName;
- @OneToMany(mappedBy = "category")
- private List<Post> posts;
- public long getId() {
- return id;
- }
- public void setId(long id) {
- this.id = id;
- }
- public List<Post> getPosts() {
- return posts;
- }
- public void setPosts(List<Post> posts) {
- this.posts = posts;
- }
- public Category() {
- }
- }
- public class QueryProperties {
- Properties queries;
- private final String URL = "jdbc:mysql://localhost:3306/postschema";
- private final String USERNAME = "root";
- private final String PASSWORD = "peroser12";
- public QueryProperties() {
- loadProperties();
- }
- public Connection getConnectionDB()
- {
- Connection connection = null;
- try {
- Driver driver = new FabricMySQLDriver();
- DriverManager.registerDriver(driver);
- connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
- } catch (SQLException e) {
- System.err.println("Driver not found!"); }
- return connection;
- }
- public String getQuery (String name){
- System.out.println("Get query " + name);
- if (queries == null)
- loadProperties();
- String q = queries.getProperty(name);
- System.out.println(q);
- return q;
- }
- private void loadProperties(){
- FileInputStream fis;
- try {
- fis = new FileInputStream("src/main/resources/queries.properties");
- queries = new Properties();
- queries.load(fis);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public interface AbstractDao <T> {
- public T create(T entity);
- public void delete(T entity);
- public T edit(T entity);
- public T getById(Integer id);
- public List<T> getAll();
- }
- public abstract class AbstractDaoImpl<T> implements AbstractDao<T> {
- QueryProperties queryProperties = new QueryProperties();
- public T create(T entity) {
- Connection connection = null;
- PreparedStatement pstmt = null;
- ResultSet resultSet = null;
- try {
- connection = queryProperties.getConnectionDB();
- String querys = getCreateQuery();
- pstmt = connection.prepareStatement(querys, Statement.NO_GENERATED_KEYS);
- fillCreateStatement(pstmt, entity);
- pstmt.executeUpdate();
- resultSet = pstmt.getGeneratedKeys();
- if (resultSet.next()){
- Integer generatedId = resultSet.getInt(1);
- return getById(generatedId);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- if (resultSet != null) try { resultSet.close(); } catch (SQLException logOrIgnore) {}
- if (pstmt != null) try { pstmt.close(); } catch (SQLException logOrIgnore) {}
- if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {}
- }
- return null;
- }
- }
- public class PostDao extends AbstractDaoImpl<Post> {
- @Override
- public void fillCreateStatement(PreparedStatement pstmt, Post entity) {
- try {
- pstmt.setString(1, entity.getBody());
- pstmt.setString(2, entity.getSummary());
- pstmt.setString(3, entity.getTitle());
- pstmt.setLong(4, entity.getCategory().getId());
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public class Main {
- public static void main(String[] args) {
- PostDao postDao = new PostDao();
- Post post = new Post();
- Category category = new Category();
- category.setCategoryName("usdgliuidfl");
- post.setTitle("jgsdhjksddlgh");
- post.setSummary("skdjhgdgsd");
- post.setBody("jhgsdajkldgh");
- post.setCategory(category);
- postDao.create(post);
- System.out.println("Added!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement