Main.class

package databaseLab;


import java.util.Scanner;


public class main {


public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner sc = new Scanner(System.in);

DB db = new DB(); // 데이터베이스 객체 생성

int input;

while(true){

System.out.println("입력 하실 데이터베이스 쿼리의 타입을 설정하세요");

System.out.println("1번 모든 교수님 성함 출력(미리 설정된 쿼리 실행)");

System.out.println("2번 학과 이름을 파라미터로 주어 학과 교수님 성함 출력");

System.out.println("3번 쿼리자체를 메소드의 파라미터로 주어 실행");

try{

input = sc.nextInt();

System.out.println(input+"번 입력하셨습니다.");

switch(input)

{

case 1:

db.selectQueryOne();

break;

case 2:

System.out.println("학과 이름을 입력해 주세요. Finance,Music,Physics,History");

sc.nextLine();

String deptName = sc.nextLine();

db.selectQueryTwo(deptName);

break;

case 3:

System.out.println("쿼리를 입력해주세요");

sc.nextLine();

String paramQuery = sc.nextLine();

db.selectQueryThree(paramQuery);

break;

default:

System.out.println("잘못 입력하셨습니다");

continue;

}//switch

}catch(Exception e){

System.out.println("숫자를 입력하지 않으셨습니다. 다시 입력하세요.");

sc.nextLine();

}//catch

}//while loop

}//main function

}//main class




DB.Class


DB.classpackage databaseLab;


import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;


public class DB {

private Connection con;

private Statement stmt;


public DB()

{

//JDBC설정

try {

String driverName ="org.gjt.mm.mysql.Driver"; // JDBC 드라이버에 따라 다르게 설정

Class.forName(driverName); // 드라이버이름 설정

String url = "jdbc:mysql://localhost:3306/university";

con = DriverManager.getConnection(url, "root", "1234"); //  connection 설정

stmt= con.createStatement(); //쿼리문을 실행할 statement 설정

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}// constructor

public void selectQueryOne()

{

String query="select name from instructor"; // attribute와 table이름에 따라 다르게 설정하세요.

ResultSet rs;

try {

rs = stmt.executeQuery(query); // 쿼리를 실행 시킵니다.

while(rs.next()) //쿼리 실행 결과 다수의 라인을 포함하는 경우 반복문을 통하여 결과를 가져옵니다.

{

System.out.println("hi");

System.out.println(rs.getString(1)); //쿼리의 실행 결과를 출력합니다.

}//while

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} //catch

}//selectQueryone

public void selectQueryTwo(String paramDept)

{

ResultSet rs;

String query="";


query="select distinct name from instructor where dept_name='"+paramDept+"'";

// 쿼리의 조건문을 파라미터로 받아 쿼리를 설정합니다

try {

rs = stmt.executeQuery(query);

while(rs.next())

{

System.out.println(rs.getString(1)); //쿼리의 결과를 출력합니다.

}//while

} catch (SQLException e) {

// TODO Auto-generated catch block

System.out.println("파라미터를 잘못 입력하셨습니다");

}//catch


}//selectQueryTwo

public void selectQueryThree(String paramQuery)

{

String query = paramQuery; //쿼리를 메소드의 파라미터로 받아와 설정합니다

ResultSet rs;


try {

rs=stmt.executeQuery(query);

while(rs.next())

{

System.out.println(rs.getString(1)); //쿼리의 결과를 출력합니다.

}//while

} catch (SQLException e) {

// TODO Auto-generated catch block

System.out.println("쿼리를 잘못 입력하셨습니다");

}//catch

}//method

}//class