국비 18일차_1

강지수·2024년 1월 9일
0

국비교육

목록 보기
34/97

지난 시간 복습

ArrayList

추가 : add, index number로 추가하게 되면 기존 index number를 뒤로 밀면서 추가
삭제 : remove, index number로 삭제하게되면 뒷 number가 앞으로 오면서 채움

따라서 빈번한 data 변화가 있는 저장공간으로서는 적합하지 않음.

LinkedList

인접한 요소들 간의 관계만 사슬처럼 연결되어 있어 특정 data 변화가 전체에 영향을 미치지 않음.

따라서 빈번한 data 변화가 있는 곳에 적합함.

Set

집합의 개념, data의 중복 저장 불가.

합집합 : addAll
교집합 : retainAll
차집합 : removeAll

Queue

선입선출 (FIFO)

Stack

후입선출 (LIFO)

db

data 불러오기 : excuteQuery, Resultset 으로 리턴
data 추가하기 : excuteUpdate, 리턴값 x


semi-project

24.01.10 ~ 24.01.15
수요일~다음주 월요일 오전까지

db까지 구성

상품 table - data 끌어오기

주문버튼 누르면 db에 쌓이게 하기

상품 list 관리자 권한으로 등록, 수정 등등

사용자 page 우선, 관리자 page 는 가능하면 하고 아니면 못하고.


map 에서의 중복 data 카운트

package com.tech.gt002;

import java.util.HashMap;
import java.util.Map;

public class WordCountTest {
	public static void main(String[] args) {
//		중복 데이터의 갯수 카운트
		Map<String, Integer> m=new HashMap<String, Integer>();
		String[] sample= {"to","be","or","not","to","be","is","a","problem","to","be"};
		for (String a : sample) {
//			값을 가져올 수 있으면 1, 없으면 null
			System.out.println(m.get(a));
			Integer freq=m.get(a);
			m.put(a, (freq==null)?1:freq+1); // value 가 누적
		}
		System.out.println(m);
	}
}

결과

null
null
null
null
1
1
null
null
null
2
2
{a=1, not=1, be=3, or=1, problem=1, is=1, to=3}

db 에 data 추가하기


추가 전 db table


추가 후 db table


db 에서 'backno' 기준으로 data 삭제하기


삭제 전 db table


삭제 후 db table


TextField clear 기능


db 에서 원하는 value 에 맞는 data select 하기

PreparedStatement 를 사용하면 특정 값을 불러올 수 있다.


미션
BoatsStatementViewer 와 BoatsPreparedStatementViewer 출력하기

table name : Sailors
sid : number
sname : varchar2
rating : number
age : number

db table 생성, 값 추가


BoatsStatementViewer.java


BoatsPreparedStatementViewer.java


인 미션인줄 알았는데, jframe 활용한 출력까지 하라 해서 다시 만듦

package com.tech.gt005.mission;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class BoatsStatementViewer extends JFrame{
	JTextField sid,sname,rating,age;
	JButton privButton,nextButton,insertButton,deleteButton,clearButton;
	ResultSet rs;
	Statement stmt;
	public BoatsStatementViewer() throws Exception {
//		화면구성
		setTitle("BoatsStatementViewer");
		setSize(350,200);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		Connection con=makeConnection();
		stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
		rs=stmt.executeQuery("select * from sailors");
		
		setLayout(new GridLayout(0,2));
		add(new JLabel("sid"));
		add(sid=new JTextField(""));
		add(new JLabel("sname"));
		add(sname=new JTextField(""));
		add(new JLabel("rating"));
		add(rating=new JTextField(""));
		add(new JLabel("age"));
		add(age=new JTextField(""));
		
		privButton=new JButton("priv");
		privButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("priv");
				try {
					rs.previous();
					sid.setText(rs.getInt("sid")+"");
					sname.setText(rs.getString("sname"));
					rating.setText(rs.getInt("rating")+"");
					age.setText(rs.getInt("age")+"");
				} catch (SQLException e1) {
					System.out.println("자료없음");
				}
			}
		});
		add(privButton);
		
		nextButton=new JButton("next");
		nextButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("next");
				try {
					rs.next();
					sid.setText(rs.getInt("sid")+"");
					sname.setText(rs.getString("sname"));
					rating.setText(rs.getInt("rating")+"");
					age.setText(rs.getInt("age")+"");
				} catch (SQLException e1) {
					System.out.println("자료없음");
				}
			}
		});
		add(nextButton);
		
		insertButton=new JButton("insert");
		insertButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("insert");
				
				int ssid=Integer.parseInt(sid.getText());
				String ssname=sname.getText();
				int srating=Integer.parseInt(rating.getText());
				int sage=Integer.parseInt(age.getText());
				String sql="insert into sailors values("+ssid+",'"+ssname+"','"+srating+",'"+sage+"')";
				
				try {
					stmt=con.createStatement();
					stmt.executeUpdate(sql);
					stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
					rs=stmt.executeQuery("select * from sailors");
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		add(insertButton);
		
		deleteButton=new JButton("delete");
		deleteButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("delete");
				
				int ssid=Integer.parseInt(sid.getText());
				String sql="delete from sailors where sid="+ssid;
				
				try {
					stmt=con.createStatement();
					stmt.executeUpdate(sql);
					stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
					rs=stmt.executeQuery("select * from sailors");
				} catch(SQLException e1) {
					e1.printStackTrace();
				}
			}
		});
		add(deleteButton);
		
		clearButton=new JButton("clear");
		clearButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("clear");
				
				sid.setText("");
				sname.setText("");
				rating.setText("");
				age.setText("");
			}
		});
		add(clearButton);
		
		
		setVisible(true);
	}
	public static void main(String[] args) throws Exception {
		new BoatsStatementViewer();
	}
//	public static void main(String[] args) throws SQLException {
//		Connection con=makeConnection();
//		Statement stmt=con.createStatement();
//		ResultSet rs=stmt.executeQuery("select * from sailors");
//		while (rs.next()) {
//			int sid=rs.getInt("sid");
//			String sname=rs.getString("sname");
//			int rating=rs.getInt("rating");
//			int age=rs.getInt("age");
//			
//			System.out.println("sid:"+sid+", sname:"+sname+", rating:"+rating+", age:"+age);
//		}
//	}
	
	public static Connection makeConnection() {
		Connection con = null;
		String url="jdbc:oracle:thin:@localhost:1521:xe";
		String id="hr";
		String pass="123456";
		
		try {
			con=DriverManager.getConnection(url,id,pass);
			System.out.println("db 연결 성공");
		} catch (SQLException e) {
			System.out.println("db 연결 실패");
			e.printStackTrace();
		}
		
		return con;
	}
}

BoatsStatementViewer.java

결과

data 출력, priv/next/insert/delete/clear 다 작동함.


package com.tech.gt005.mission;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class BoatsPreparedStatementViewer extends JFrame{
	JTextField sid,sname,rating,age;
	JButton searchButton,clearButton;
	ResultSet rs;
	Statement stmt;
	
	public BoatsPreparedStatementViewer() throws Exception {
//		화면구성
		setTitle("BoatsStatementViewer");
		setSize(350,200);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		Connection con = makeConnection();
		stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
		rs=stmt.executeQuery("select * from sailors");

		
//		stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
//		rs=stmt.executeQuery("select * from sailors");
		
		setLayout(new GridLayout(0,2));
		add(new JLabel("sid"));
		add(sid=new JTextField(""));
		add(new JLabel("sname"));
		add(sname=new JTextField(""));
		add(new JLabel("rating"));
		add(rating=new JTextField(""));
		add(new JLabel("age"));
		add(age=new JTextField(""));
		
		searchButton=new JButton("search");
		searchButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("search");
				String sql = "select sid,sname,rating,age from sailors where sid=?";
				try {
					PreparedStatement pstmt = con.prepareStatement(sql);
					pstmt.setInt(1, Integer.parseInt(sid.getText()));
					rs = pstmt.executeQuery();
					rs.next();
					sid.setText(rs.getInt("sid")+"");
					sname.setText(rs.getString("sname"));
					rating.setText(rs.getInt("rating")+"");
					age.setText(rs.getInt("age")+"");
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		});
		add(searchButton);

		clearButton=new JButton("clear");
		clearButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("clear");
				
				sid.setText("");
				sname.setText("");
				rating.setText("");
				age.setText("");
			}
		});
		add(clearButton);		
		
		setVisible(true);
	}
	
	public static void main(String[] args) throws Exception {
		new BoatsPreparedStatementViewer();
	}
//		Connection con=makeConnection();
//		String sql="select sid,sname,rating,age from sailors where sid=? and sname=?";
//		
//		try {
//			PreparedStatement pstmt=con.prepareStatement(sql);
//			pstmt.setInt(1, 32);
//			pstmt.setString(2, "Andy");
//			ResultSet rs=pstmt.executeQuery();
//			rs.next();
//			System.out.println(rs.getInt("sid")+":"+rs.getString("sname")+":"+rs.getInt("rating")
//			+":"+rs.getInt("age"));
//			
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}

	public static Connection makeConnection() {
		Connection con=null;
		String url="jdbc:oracle:thin:@localhost:1521:xe";
		String id="hr";
		String pass="123456";
		
		try {
			con=DriverManager.getConnection(url,id,pass);
			System.out.println("db 연결 성공");
		} catch (Exception e1) {
			System.out.println("오류");
		}
		return con;
	}
}

BoatsPreparedStatementViewer.java

결과

초기화면

sid 를 기입하고 search 하면

결과가 나옴 / clear 하면 TextField 초기화


profile
개발자 준비의 준비준비중..

0개의 댓글