Java 일지 #9 New Project(20210826)
9.새로운 프로젝트 생성
-java_20210826 폴더 생성하기
*jscode 실행 후 왼쪽 하단의 maven 클릭하고 + 버튼을 누르면 사진과 같이 검색창이 나타난다.
*검색창에 mave을 입력한 뒤 maven ~~ quikstart 를 클릭한다.
*이후 1.4 버전 클릭, com.example, 파일명(java_20210826) 입력 그리고 설치 위치 지정
*이후 출력 값이 나오면 y 입력 후 엔터
-폴더 생성 후 세팅
*pom.xml 코드 입력
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>4.2.2</version>
</dependency>
<maven.compiler.source>11</maven.compiler.source> 에 값을 11로 입력 하단도 동일
mongodb dependency 입력
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>4.2.2</version>
</dependency>
*src에 entity, model, view 폴더 생성
-이전에 Frame 때 했던 것과 마찬가지로 작업 진행
*entity 폴더에 Item.java 생성
**내용은 Frame과 같음
*model 폴더에 ItemDB.java 생성
*ItemDB.java 코드 입력
package com.example.model;
import java.util.ArrayList;
import java.util.List;
import com.example.entity.Item;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Updates;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.InsertOneResult;
import com.mongodb.client.result.UpdateResult;
import org.bson.Document;
public class ItemDB {
private MongoDatabase db = null;
private MongoCollection<Document> collection = null;
private final String URL = "mongodb://id311:pw311@1.234.5.158:37017/id311";
//url은 바꾸지 않을 것이므로 final을 삽입
public ItemDB() { //db연동 컬렉션
MongoClient client = MongoClients.create(URL);
this.db = client.getDatabase("id311");
this.collection = db.getCollection("java_item");
}
public int insertItem( Item item ) { //등록
try {
Document document = new Document();
document.append("_id", item.getCode());
document.append("name", item.getName());
document.append("text", item.getText());
document.append("price", item.getPrice());
document.append("_quantity", item.getQuantity());
InsertOneResult result = collection.insertOne(document);
if(result.getInsertedId().asInt32().getValue()
== item.getCode()){
return 1;
}
return 0;
}
catch(Exception e) {
System.out.println(e.getMessage());
return -1;
}
}
public List<Item> selectItems() throws Exception{ //전체 목록 조회
MongoCursor<Document> cursor = this.collection.find().sort(Filters.eq("_id", 1)).cursor();
List<Item> list = new ArrayList<Item>(); //빈 리스트 생성
while(cursor.hasNext()) { //iterator, cursor
Document doc = cursor.next();
Item item = new Item();
item.setCode( (int) doc.get("_id") );
item.setName( (String) doc.get("name") );
item.setText( (String) doc.get("text") );
item.setPrice( (int) doc.get("price") );
item.setQuantity( (long) doc.get("quantity") );
list.add(item);
}
return list;
}
public int updateItem(Item item) throws Exception{ //수정
UpdateResult result = this.collection.updateOne(Filters.eq("_id", item.getCode()),
Updates.combine(
Updates.set("name", item.getName()),
Updates.set("text", item.getText()),
Updates.set("price", item.getPrice()),
Updates.set("quantity", item.getQuantity())
)
);
if(result.getMatchedCount() ==1) {
return 1;
}
return 0;
}
public int deleteItem(Item item) throws Exception { //삭제
DeleteResult result = this.collection.deleteOne(Filters.eq("_id", item.getCode()));
if(result.getDeletedCount() == 1) {
return 1;
}
return 0;
}
}
*view 폴더에 Frame, Panel.java 생성
*ItemFrame.java 코드 입력
package com.example.view;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
public class ItemFrame extends JFrame implements ActionListener{
//생성자
public ItemFrame(String title) throws HeadlessException {
super(title);
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.add("홈", new HomePanel());
tabbedPane.add("물품등록", new InsertPanel());
tabbedPane.add("물품수정", new SelectPanel());
tabbedPane.setSelectedIndex(0);
this.add(tabbedPane);
this.setSize(500, 400);
this.setVisible(true);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//클릭에 대한 이벤트 발생 시 자동 호출되는 메소드
@Override
public void actionPerformed(ActionEvent e) {
}
}
*나머지 Panel.java에 내용 입력
ex)
package com.example.view;
import javax.swing.JPanel;
public class InsertPanel extends JPanel{
}
*App.java 코드 입력
package com.example;
import com.example.view.ItemFrame;
public class App {
public static void main(String[] atgs) {
new ItemFrame("여기 시험문제");
}
}
*출력