Import project java vào eclipse bị lỗi tiếng việt năm 2024
Việc load một project có sẵn lên cửa sổ làm việc của Eclipse tuy đơn giản nhưng không phải ai cũng biết cách. Nếu Bạn có sẵn một project viết trên Java, bạn có thể load toàn bộ project trên bất kỳ máy nào có cài đặt Eclipse. Đầu tiên, cần sao chép toàn bộ folder project cần của mình vào thư mục làm việc của Eclipse (còn gọi là Workspace). Để biết thư mục này ở đâu, thực hiện như sau: Trên cửa sổ làm việc của Eclipse, vào menu Window > chọn Preferences > trên cửa sổ hiện ra, mở rộng cây thư mục General > chọn Workspace như hình dưới và xem đường dẫn đến thư mục này: Sau khi biết đường dẫn đến thư mục làm việc của Eclipse, tiếp tục sao chép toàn bộ folder project vào thư mục này. Trên cửa sổ làm việc của Eclipse, vào menu File > Chọn Import > Trên cửa sổ hiện ra, chọn Existing projects into Workspace như hình dưới > chọn Next để tiếp tục. Trên cửa sổ hiện ra, chọn đường daanx đến project cần import (project nằm trong workspace vừa copy vào ở bước trên) và check vào mục chọn Add project to working sets > Click nút Finish để kết thúc. Bởi vì Hibernate không được cấu hình sẵn trong eclipse nên chúng ta phải tạo 1 Eclipse User Library. Các thư viện của Hibernate chúng ta có thể download tại http://www.hibernate.org/6.html. Chúng ta cần 2 thư viện Hibernate Core và Hibernate EntityManage. Sau khi download về, ta giải nén vào 1 thư mục nào đó, chẳng hạn như C:\javaSoft. Trong môi trường Eclipse Ganymede, chọn menu Windows->References, rồi tiếp tục chọn Java -> Build Path -> User Libraries. Nhấn nút New để thêm mới 1 thư viện, đặt tên là Hibernate. Nhấn nút Add JARs… để thêm vào các thư viện sau:
Kết quả như hình sau: 2. Tạo DatabaseTạo data base tên subscription bằng cách chạy lệnh sau trong MySQL Comand Promt
hoặc dùng “MySQL Query Browser” để tạo database. Chạy script sau để sinh ra table DROP TABLE IF EXISTS `subscription`.`account`; CREATE TABLE `subscription`.`account` ( `id` int(10) unsigned NOT NULL auto_increment, `email` varchar(128) NOT NULL, `firstname` varchar(128) NOT NULL, `lastname` varchar(128) NOT NULL, `company` varchar(128) NOT NULL, `phone` varchar(128) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1; 3. Tạo Eclipse projectTrong Eclipse, tạo 1 JPA project bằng cách vào menu File->New->Project, sau đó tìm đến JPA project như hình Nhấn Next, thiết lập các tùy chọn như hình sau: Nhấn link “Add conection” để tạo 1 connection mới. Chọn My Sql như hình Mở rộng các node của project trong cửa sổ project explorer ta có kết quả như hình. Hiệu chỉnh file persistence.xml với nội dung sau: Có thể eclipse sẽ thông báo lỗi cho file xml này, mặc kệ nó, bạn tiếp tục làm theo các bước sau Tạo package com.vovanhai.wordpress.account Tạo lớp Account.java trong package này với nội dung sau package com.vovanhai.wordpress.account; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = “account”, schema = “subscription”) public class Account implements Serializable { /*private static final Logger logger = Logger.getLogger( “com.vovanhai.wordpress.account.Account”);*/ private static final long serialVersionUID = -7201186159122319296L; @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String email; private String firstname; private String lastname; private String company; private String phone; public Account() { this(“”,””,””,””,””); } public Account(String email, String firstname,String lastname, String company,String phone) { this.email=email; this.firstname=firstname; this.lastname=lastname; this.company=company; this.phone=phone; //logger.info(“Load records”);//in vết } @Column(name=”email”) public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Column(name=”firstname”) public String getFirstname() { return firstname; } public void setFirstname(String firstname) { this.firstname = firstname; } @Column(name=”lastname”) public String getLastname() { return lastname; } public void setLastname(String lastname) { this.lastname = lastname; } @Column(name=”company”) public String getCompany() { return company; } public void setCompany(String company) { this.company = company; } @Column(name=”phone”) public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } @Column(name=”id”) public int getId() { return id; } public void setId(int id) { this.id = id; } @Override public String toString() { return id+”; “+ firstname+”; “+lastname+”;”+ email +company+”;”+phone; } } Vậy là chúng ta đã tạo 1 đối tượng POJO ánh xạ giữa đối tượng Account với bảng account của database. Tiếp theo, bạn thêm 1 tham chiếu đến MySQL driver vào trong project bằng cách nhấn phải chuột lên project, chọn properties, chọn đến mục “Java Build Path”, nhấn nút “Add External JARs…” rồi chọn MySQL connector – file mysql-connector-java-5.1.6-bin.jar. |