반응형
package demo2.book;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JLabel;
public class BookMgr3 {
private JFrame frame;
private JTextField searchField;
private String searchText;
private String searchsql;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue. invokeLater( new Runnable() {
public void run() {
try {
BookMgr3 window = new BookMgr3();
window. frame.setVisible( true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public BookMgr3() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
JTextArea textArea = new JTextArea();
JLabel lblNewLabel = new JLabel( "");
searchText = "book_no";
frame = new JFrame();
frame.setBounds(100, 100, 310, 303);
frame.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE );
frame.getContentPane().setLayout( null);
JComboBox comboBox = new JComboBox();
comboBox.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
searchText = (String)comboBox .getSelectedItem();
//String category = (String)categorycomboBox .getSelectedItem();
System. out.println( searchText);
}
});
comboBox .setModel(new DefaultComboBoxModel( new String[] {"\uBC88\uD638" ,"\uC81C\uBAA9", "\uC791\uAC00"})) ;
comboBox.setBounds(12, 10, 65, 21);
frame.getContentPane().add( comboBox);
searchField = new JTextField();
searchField.setBounds(89, 10, 102, 21);
frame.getContentPane().add( searchField);
searchField.setColumns(10);
JButton btnNewButton = new JButton( "\uAC80\uC0C9");
btnNewButton.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
if( searchText.equals( "제목" )){
searchsql = "book_title";
} else if( searchText.equals( "작가" )){
searchsql = "book_publisher";
} else if( searchText.equalsIgnoreCase( "번호" )){
searchsql = "book_no";
} else {
searchsql = "book_no";
}
String sql = "SELECT book_no, book_title, book_author, book_publisher, book_price, book_pubdate, book_summary "
+ "FROM sample_book "
+ "WHERE " + searchsql + " LIKE ? ";
try{
Class. forName( "oracle.jdbc.OracleDriver");
Connection con = DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:xe", "hr", "zxcv1234");
System. out.println( sql);
PreparedStatement pstmt = con.prepareStatement( sql);
pstmt. setString(1, searchField.getText());
ResultSet rs = pstmt.executeQuery();
int i = 0;
String title;
String author;
String publisher;
int price;
String pubdate;
String summary;
while( rs.next()){
title = rs.getString( "book_title");
author = rs.getString( "book_author");
publisher = rs.getString( "book_publisher");
price = rs.getInt( "book_price");
pubdate = rs.getString( "book_pubdate");
summary = rs.getString( "book_summary");
textArea.setText( title + author + publisher + price + pubdate + summary + "\t");
i++;
}
lblNewLabel.setText( "총건수 : " + String.valueOf( i));
} catch(Exception ex){
ex.printStackTrace();
JOptionPane. showMessageDialog( null, "책 검색 실패");
}
}
});
btnNewButton.setBounds(203, 9, 76, 23);
frame.getContentPane().add( btnNewButton);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(12, 50, 267, 175);
frame.getContentPane().add( scrollPane);
scrollPane.setViewportView( textArea);
lblNewLabel.setBounds(12, 231, 267, 24);
frame.getContentPane().add( lblNewLabel);
}
}
package demo2.book;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;
import javax.swing.JButton;
public class BookMgr4 {
private JFrame frame;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue. invokeLater( new Runnable() {
public void run() {
try {
BookMgr4 window = new BookMgr4();
window. frame.setVisible( true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public BookMgr4() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
JTextArea textArea = new JTextArea();
frame = new JFrame();
frame.setBounds(100, 100, 311, 119);
frame.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE );
frame.getContentPane().setLayout( null);
JLabel lblNewLabel = new JLabel( "\uCC45\uC815\uBCF4 \uC0AD\uC81C");
lblNewLabel.setFont( new Font( "굴림" , Font.BOLD , 28));
lblNewLabel.setBounds(62, 6, 166, 37);
frame.getContentPane().add( lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("\uCC45\uBC88\uD638" );
lblNewLabel_1.setBounds(10, 53, 52, 15);
frame.getContentPane().add( lblNewLabel_1);
textField = new JTextField();
textField.setBounds(62, 50, 150, 21);
frame.getContentPane().add( textField);
textField.setColumns(10);
JButton btnNewButton = new JButton( "\uC0AD\uC81C");
btnNewButton.setBounds(224, 49, 60, 23);
btnNewButton.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
BookDAO dao = new BookDAO();
try{
int no = Integer.parseInt( textField.getText());
dao.removeBookByNo( no);
JOptionPane. showMessageDialog( null, no+ "행 삭제 완료~" );
} catch(Exception ex){
ex.printStackTrace();
JOptionPane. showMessageDialog( null, "책 삭제 실패" );
}
}
});
frame.getContentPane().add( btnNewButton);
}
}
package demo2.book;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class BookMgr5 {
private JFrame frame;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue. invokeLater( new Runnable() {
public void run() {
try {
BookMgr5 window = new BookMgr5();
window. frame.setVisible( true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public BookMgr5() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
JTextArea textArea = new JTextArea();
frame = new JFrame();
frame.setBounds(100, 100, 218, 315);
frame.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE );
frame.getContentPane().setLayout( null);
JLabel lblNewLabel = new JLabel( "\uBC88\uD638");
lblNewLabel.setBounds(10, 10, 34, 22);
frame.getContentPane().add( lblNewLabel);
JLabel label = new JLabel( "\uC81C\uBAA9");
label.setBounds(10, 38, 34, 22);
frame.getContentPane().add( label);
JLabel label_1 = new JLabel( "\uC800\uC790");
label_1.setBounds(10, 64, 34, 22);
frame.getContentPane().add( label_1);
JLabel label_2 = new JLabel( "\uCD9C\uD310\uC0AC" );
label_2.setBounds(10, 96, 46, 22);
frame.getContentPane().add( label_2);
JLabel label_3 = new JLabel( "\uAC00\uACA9");
label_3.setBounds(10, 126, 34, 22);
frame.getContentPane().add( label_3);
JLabel label_4 = new JLabel( "\uC124\uBA85");
label_4.setBounds(10, 154, 34, 22);
frame.getContentPane().add( label_4);
textField = new JTextField();
textField.setBounds(56, 11, 56, 22);
frame.getContentPane().add( textField);
textField.setColumns(10);
JButton btnNewButton = new JButton( "\uC870\uD68C");
btnNewButton.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
BookDAO dao = new BookDAO();
Book book = new Book();
int no = Integer.parseInt( textField.getText());
book = dao.findBookByNo( no);
textField.setText(String. valueOf( book.getNo()));
textField_1.setText( book.getTitle());
textField_2.setText( book.getAuthor());
textField_3.setText( book.getPublisher());
textField_4.setText(String. valueOf( book.getPrice()));
textArea.setText( book.getSummary());
} catch(Exception ex){
ex.printStackTrace();
JOptionPane. showMessageDialog( null, "조회 오류" );
}
}
});
btnNewButton.setBounds(124, 10, 67, 23);
frame.getContentPane().add( btnNewButton);
textField_1 = new JTextField();
textField_1.setColumns(10);
textField_1.setBounds(56, 38, 135, 22);
frame.getContentPane().add( textField_1);
textField_2 = new JTextField();
textField_2.setColumns(10);
textField_2.setBounds(56, 68, 135, 22);
frame.getContentPane().add( textField_2);
textField_3 = new JTextField();
textField_3.setColumns(10);
textField_3.setBounds(56, 96, 135, 22);
frame.getContentPane().add( textField_3);
textField_4 = new JTextField();
textField_4.setColumns(10);
textField_4.setBounds(56, 126, 135, 22);
frame.getContentPane().add( textField_4);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(56, 158, 142, 83);
frame.getContentPane().add( scrollPane);
scrollPane.setViewportView( textArea);
JButton button = new JButton( "\uC218\uC815");
button.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
Book book = new Book();
BookDAO dao = new BookDAO();
//입력된 책정보 가져오기
int no = Integer.parseInt( textField.getText());
String title = textField_1.getText();
String author = textField_2.getText();
String publisher = textField_3.getText();
int price = Integer.parseInt( textField_4.getText());
String summary = textArea.getText();
//책정보를 Book객체에 담기
book.setNo( no);
book.setTitle( title);
book.setAuthor( author);
book.setPublisher( publisher);
book.setPrice( price);
book.setPubdate( new java.sql.Date (2015, 4, 13));
book.setSummary( summary);
//BookDao에 Book객체 전달하기
dao.updateBookByNo( book);
} catch(Exception ex){
ex.printStackTrace();
JOptionPane. showMessageDialog( null, "수정 오류" );
}
}
});
button.setBounds(128, 252, 70, 23);
frame.getContentPane().add( button);
}
}
싱글턴객체(singletion instance)
- 객체를 하나만 만들어서 재사용하는 것
- 주로 기능이 정의되어 있는 효율측면에서 유리하다
context 객체를 미리 만들어서 담아두고 있는곳
container 동일한 의미
package demo3.singleton;
public class Dao {
//DAO를 실글턴 객체로 사용되도록 하기
//DAO객체를 담는 STATIC 변수를 만든다.
private static Dao d = new Dao();
//생성자를 private로 만들기
private Dao() {}
//DAO 객체를 제공하는 static 메소드를 만든다.
public static Dao getInstance(){
return d;
}
public void add(){
System. out.println( "새로운 정보 추가" );
}
public void remove(){
System. out.println( "기존 정보 삭제" );
}
}
package demo3.singleton;
public class NonSingletonDemo {
public static void main(String[] args) {
//다른 클래스에서 Dao d = new Da (); //이코드를 사용할수 없다.
//-> 함부로 객체를 생성하지 못하게 만들었다
//미리 객체를 생성해서 변수안에 담아둔다.
//->Dao안에 변수를 하나 만들어서 객체를 생성한 다음 담아둔다.
//-->getInstance()에서 접근할 수 있도록 static으로 변수를 만든다.
//대신, 미리 생성된 객체를 얻을 수 있는 메서드를 제공한다
//->DaogetInstance()를 실행하면 미리 생성된 객체를 얻을 수 있다.
//->getInstance()를 static 메소드를 만든이유는? static 메소드는 객체 생성하지 않고도 사용할 수 있으니까
Dao d1 = Dao. getInstance();
d1.remove();
Dao d2 = Dao. getInstance();
d2.add();
Dao d3 = Dao. getInstance();
d3.add();
System. out.println( d1);
System. out.println( d2);
System. out.println( d3);
/**새로 객체를 생성하지 않고 생성된 객체를 제사용한다. 메모리 효울 UP 소스관리 UP
* 결과화면______________________
* 기존 정보 삭제
새로운 정보 추가
새로운 정보 추가
demo3.singleton.Dao@19e0bfd
demo3.singleton.Dao@19e0bfd
demo3.singleton.Dao@19e0bfd
*/
}
}
package demo3.init;
public class StaticBlock {
//static 초기화 블록
//한번만 수행하고 싶은 작업을 정의하는 곳
static{
System. out.println( "이 코드는 클래스가 메모리에 로딩될 때 딱 한번만 실행됩니다.");
}
public StaticBlock() {
super();
System. out.println( "생성자는 객체가 생성될때마다 실행됩니다.");
}
}
package demo3.init;
public class StaticDemo {
public static void main(String[] args) throws Exception{
//StaticBlock 클래스를 메모리에 로딩하기
Class. forName( "demo3.init.StaticBlock");
StaticBlock s1 = new StaticBlock();
StaticBlock s2 = new StaticBlock();
StaticBlock s3 = new StaticBlock();
/**결과화면____________________________________
* 이 코드는 클래스가 메모리에 로딩될 때 딱 한번만 실행됩니다.
생성자는 객체가 생성될때마다 실행됩니다.
생성자는 객체가 생성될때마다 실행됩니다.
생성자는 객체가 생성될때마다 실행됩니다.
*/
}
}
반응형