국비 17일차_2

강지수·2024년 1월 8일
0

국비교육

목록 보기
33/97

데이터베이스 프로그래밍



미션
Oracle SQL
table 이름 : telinfo

table 만들기


eclipse 에서 불러오기


외부에서 data 를 insert 해 준 모습, 외부에서 data 처리를 하면 commit 을 하지 않아도 된다.


package com.tech.gt004.db2;

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 SoccerViewer extends JFrame{
	JTextField backno,name,pos;
	JButton privButton,nextButton,insertButton,deleteButton,clearButton;
	ResultSet rs;
	Statement stmt;
	
	public SoccerViewer() throws Exception {
//		화면구성
		setTitle("SoccerViewer");
		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 soccer");
		
		setLayout(new GridLayout(0,2));
		add(new JLabel("backno"));
		add(backno=new JTextField(""));
		add(new JLabel("name"));
		add(name=new JTextField(""));
		add(new JLabel("pos"));
		add(pos=new JTextField(""));
		
		privButton=new JButton("priv");
		privButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("priv");
				try {
					rs.previous();
					backno.setText(rs.getInt("backno")+"");
					name.setText(rs.getString("name"));
					pos.setText(rs.getString("pos"));
				} 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();
					backno.setText(rs.getInt("backno")+"");
					name.setText(rs.getString("name"));
					pos.setText(rs.getString("pos"));
				} 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");
			}
		});
		add(insertButton);
		
		deleteButton=new JButton("delete");
		deleteButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("delete");
			}
		});
		add(deleteButton);
		
		clearButton=new JButton("clear");
		clearButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("clear");
			}
		});
		add(clearButton);
		
		
		setVisible(true);
	}
	
	private 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 e) {
			System.out.println("db 연결 실패");
			e.printStackTrace();
		}
		return con;
	}

	public static void main(String[] args) throws Exception {
		new SoccerViewer();
	}

}

결과

swing 을 이용해서 db 를 불러오고 next/priv 버튼의 역할까지 부여해서 출력


Linux 미션
생략


Windows 미션
tellinfoviewer 만들어서 출력해보기
코드를 텍스트로 메일로 제출
메일제목 : 텔뷰어_홍길동

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 TellInfoViewer extends JFrame {
	JTextField no, compName, ceoname, ceophone, addr, managername;
	JButton privButton, nextButton, insertButton, deleteButton, clearButton;
	ResultSet rs;
	Statement stmt;

	public TellInfoViewer() throws Exception {
//			화면구성
			setTitle("TellInfoViewer");
			setSize(350,300);
			setDefaultCloseOperation(EXIT_ON_CLOSE);
			
			Connection con=makeConnection();
			stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
			rs=stmt.executeQuery("select * from telinfo");
			
			setLayout(new GridLayout(0,2));
			add(new JLabel("No"));
			add(no=new JTextField(""));
			add(new JLabel("CompName"));
			add(compName=new JTextField(""));
			add(new JLabel("CEOName"));
			add(ceoname=new JTextField(""));
			add(new JLabel("CEOPhone"));
			add(ceophone=new JTextField(""));
			add(new JLabel("Addr"));
			add(addr=new JTextField(""));
			add(new JLabel("ManagerName"));
			add(managername=new JTextField(""));
			
			privButton=new JButton("priv");
			privButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					System.out.println("priv");
					try {
						rs.previous();
						no.setText(rs.getInt("No")+"");
						compName.setText(rs.getString("CompName"));
						ceoname.setText(rs.getString("CEOName"));
						ceophone.setText(rs.getString("CEOPhone"));
						addr.setText(rs.getString("Addr"));
						managername.setText(rs.getString("ManagerName"));
					} 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();
						no.setText(rs.getInt("No")+"");
						compName.setText(rs.getString("CompName"));
						ceoname.setText(rs.getString("CEOName"));
						ceophone.setText(rs.getString("CEOPhone"));
						addr.setText(rs.getString("Addr"));
						managername.setText(rs.getString("ManagerName"));
					} 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");
				}
			});
			add(insertButton);
			
			deleteButton=new JButton("delete");
			deleteButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					System.out.println("delete");
				}
			});
			add(deleteButton);
			
			clearButton=new JButton("clear");
			clearButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					System.out.println("clear");
				}
			});
			add(clearButton);
			
			
			setVisible(true);
		}

	private 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 e) {
			System.out.println("db 연결 실패");
			e.printStackTrace();
		}
		return con;
	}

	public static void main(String[] args) throws Exception {
		new TellInfoViewer();
	}

}

결과


키오스크 미션 혼자 진행중


import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class KioskMission extends JFrame{
	Container main;
	CardLayout mainCard;
	JPanel Home, OrderTab1, OrderTab2;
	JLabel HomeImage, HomeImage2;
	JButton HomeButton, HomeButton2, mainButton, NextButton;
	
	public KioskMission() {
		setTitle("KioskMission");
		setSize(640,600);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		main=getContentPane();
		mainCard=new CardLayout();
		main.setLayout(mainCard);
		
		Home=new JPanel(null);
		
		JPanel h1=new JPanel();
		h1.setSize(640,500);
		h1.setLocation(0, 0);
		h1.setBackground(Color.black);
		HomeImage=new JLabel("Home Image");
		h1.add(HomeImage);
		Home.add(h1);
		
		HomeButton=new JButton("HOME");
		HomeButton.setSize(70,50);
		HomeButton.setLocation(530,30);
		HomeButton.setBackground(Color.yellow);
		Home.add(HomeButton);
		
		mainButton=new JButton(">> 이곳을 눌러 주문해주세요 <<");
		mainButton.setSize(625,60);
		mainButton.setLocation(0, 500);
		mainButton.setBackground(Color.red);
		mainButton.setFont(new Font("Gothic", Font.BOLD, 30));		
		Home.add(mainButton);
		
		OrderTab1=new JPanel(null);
		JPanel h2=new JPanel();
		h2.setSize(640,500);
		h2.setLocation(0, 0);
		h2.setBackground(Color.black);
		HomeImage2=new JLabel("Home Image2");
		h2.add(HomeImage2);
		
		HomeButton2=new JButton("HOME");
		HomeButton2.setSize(70,50);
		HomeButton2.setLocation(530,30);
		HomeButton2.setBackground(Color.yellow);
		OrderTab1.add(HomeButton2);
		
		OrderTab1.add(h2);
		
		NextButton=new JButton("다음 페이지");
		NextButton.setSize(625,60);
		NextButton.setLocation(0, 500);
		NextButton.setBackground(Color.red);
		NextButton.setFont(new Font("Gothic", Font.BOLD, 30));		
		OrderTab1.add(NextButton);

		main.add("1",Home);
		main.add("2",OrderTab1);
		HomeButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				mainCard.first(main);
				
			}
		});
		HomeButton2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				mainCard.first(main);
				
			}
		});
		
		mainButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				mainCard.next(main);
				
			}
		});
		NextButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				mainCard.next(main);
				
			}
		});
		
		
		setVisible(true);
	}
	public static void main(String[] args) {
		new KioskMission();
	}
}

화면단 home image / 버튼 나누고 누르면 다음페이지 로 이동
HOME 버튼 누르면 초기 화면 이동.


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

0개의 댓글