본문 바로가기
솔루션모음/파워자바 프로그래밍

[파워자바] 27장 프로그래밍 솔루션 답지

by 이얏호이야호 2023. 2. 16.

1.

(1)

import java.sql.*;
import java.util.*;
public class SQLSelectTest {
	public static Connection makeConnection(){
		String url = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
		String id = "cinema";
		String password = "pl518";
		Connection con = null;
		try{
			Class.forName("oracle.jdbc.driver.OracleDriver");
			System.out.println("드라이버 적재 성공");
			con = DriverManager.getConnection(url, id , password);
			System.out.println("데이터베이스 연결 성공");
		}catch(ClassNotFoundException e){
			System.out.println("드라이버를 찾을 수 없습니다.");
		}catch(SQLException e){
			System.out.println("연결에 실패하였습니다.");
		}
		return con;
	}
	public static void main(String[] args) throws SQLException{
		String title, day, time1, time2;
		int num;
		Scanner s = new Scanner(System.in);
		Connection con = makeConnection();
		Statement stmt = con.createStatement();
		while(true){
			System.out.println("스케줄 제목, 날짜, 시작시간, 종료시간");
			title = s.nextLine();
			day = s.nextLine();
			time1 = s.nextLine();
			time2 = s.nextLine();
			ResultSet rs = stmt.executeQuery("insert into schedule(title, 
					day, time1, time2)" +
					"values('"+title+"','"+day+"','"+time1+"','"+ time2 +"')");
			System.out.println("계속하시려면 1 끝내시려면 0");
			num = s.nextInt();
			if(num == 0)
				break;
		}
		ResultSet rs = stmt.executeQuery("select *from schedule");
		while(rs.next()){
			String t = rs.getString("title");
			System.out.print(t);
			String d = rs.getString("day");
			System.out.print(" " + d);
			String t1 = rs.getString("time1");
			System.out.print(" " + t1);
			String t2 = rs.getString("time2");
			System.out.print(" " + t2);
			System.out.println();
		}
	}
}

(2)

mport java.sql.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyFrame extends JFrame implements ActionListener{
	JTextField title, day, startTime, endTime;
	JButton viewButton, nextButton;
	String title1, day1, time1, time2;
	int num;
	static Connection con = null;
	static Statement stmt = null;
	public MyFrame(){
		super("Database Viewer");
		con = makeConnection();
		try {
			stmt = con.createStatement();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		setSize(400,200);
		setLayout(new GridLayout(0,2));
		add(new JLabel("TITLE", JLabel.CENTER));
		add(title = new JTextField());
		add(new JLabel("DAY", JLabel.CENTER));
		add(day = new JTextField());
		add(new JLabel("StartTime", JLabel.CENTER));
		add(startTime = new JTextField());
		add(new JLabel("EndTime", JLabel.CENTER));
		add(endTime = new JTextField());
		add(viewButton = new JButton("View"));
		add(nextButton = new JButton("Next"));
		viewButton.addActionListener(this);
		nextButton.addActionListener(this);
		setVisible(true);
	}
	public void actionPerformed(ActionEvent e) {
		ResultSet rs = null;
		if(e.getSource()==nextButton){
			title1 = title.getText();
			day1 = day.getText();
			time1 = startTime.getText();
			time2 = endTime.getText();
			try {
				rs = stmt.executeQuery("insert into schedule(title, day, time1, time2)" +
						"values('"+title1 +"','"+ day1 + "','" + time1 +"','" + time2 +"')");
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			title.setText("");
			day.setText("");
			startTime.setText("");
			endTime.setText("");
		}
		else if(e.getSource() == viewButton){
			try {
				new MyFrame2();
			} catch (SQLException e2) {
				e2.printStackTrace();
			}
		}
	}
	public static Connection makeConnection(){
		String url = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
		String id = "cinema";
		String password = "pl518";
		Connection con = null;
		try{
			Class.forName("oracle.jdbc.driver.OracleDriver");
			System.out.println("드라이버 적재 성공");
			con = DriverManager.getConnection(url, id , password);
			System.out.println("데이터베이스 연결 성공");
		}catch(ClassNotFoundException e){
			System.out.println("드라이버를 찾을 수 없습니다.");
		}catch(SQLException e){
			System.out.println("연결에 실패하였습니다.");
		}
		return con;
	}
}
class MyFrame2 extends JFrame{
	JLabel label1 ,label2, label3, label4;
	JLabel label5 ,label6, label7, label8;
	public MyFrame2() throws SQLException{
		super("Database Viewer");
		Connection con =  makeConnection();
		Statement stmt = con.createStatement();
		ResultSet rs = stmt.executeQuery("select *from schedule");
		setSize(450,650);
		label1 = new JLabel("제목");
		label2 = new JLabel("날짜");
		label3 = new JLabel("시작시간");
		label4 = new JLabel("끝나는 시간");
		setLayout(new GridLayout(0,4));
		add(label1);
		add(label2);
		add(label3);
		add(label4);
		try {
			while(rs.next()){
				String t = rs.getString("title");
				label5 = new JLabel();
				label5.setText(t);
				add(label5);
				String d = rs.getString("day");
				label6 = new JLabel();
				label6.setText(d);
				add(label6);
				String t1 = rs.getString("time1");
				label7 = new JLabel();
				label7.setText(t1);
				add(label7);
				String t2 = rs.getString("time2");
				label8 = new JLabel();
				label8.setText(t2);
				add(label8);
			}
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		setVisible(true);
	}
	public static Connection makeConnection(){
		String url = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
		String id = "cinema";
		String password = "pl518";
		Connection con = null;
		try{
			Class.forName("oracle.jdbc.driver.OracleDriver");
			System.out.println("드라이버 적재 성공");
			con = DriverManager.getConnection(url, id , password);
			System.out.println("데이터베이스 연결 성공");
		}catch(ClassNotFoundException e){
			System.out.println("드라이버를 찾을 수 없습니다.");
		}catch(SQLException e){
			System.out.println("연결에 실패하였습니다.");
		}
		return con;
	}
}
public class InterfaceTest {
	public static void main(String[] args) throws SQLException{
		String title, day, time1, time2;
		int num;
		new MyFrame();
	}
}

댓글