package minus;
import java.io.*; import java.sql.*;
import conn.*;
public class SqlMinus { private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) { new SqlMinus().execute(); }
private void execute() { System.out.println("Oracle Database 11g Express Edition Release 11.2.0.2.0 - Production"); while(true) { System.out.print("Minus> "); try { String sql = br.readLine(); // 앞글자 6개를 잘라서 비교 if(sql.equalsIgnoreCase("exit")) { // sql에 들어오는 값이 대소문자 모두 포함 exit입력시 종료 System.out.println("Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - Production"); System.exit(0); //종료 해주는 메소드 } String cmd = sql.substring(0, 6); // 입력한값을 0부터 6까지 짤라주는 형식
switch(cmd.toLowerCase()) { // cmd에 입력한 값을 소문자로 변환!!
case "select" :
select(sql); break;
case "insert" :
insert(sql); break;
case "update" :
update(sql); break;
case "delete" :
delete(sql); break;
default :
other(sql); break; } } catch (IOException e) { e.printStackTrace(); } catch (SQLException e) { System.out.println(e.getMessage()); } } }
private void other(String sql) throws SQLException { try( Connection conn = MyConn.getConnection(); //데이터베이스 연결 해주기 Statement stmt = conn.createStatement(); //Statement 생성 해주기 ){ stmt.execute(sql); System.out.println("실행이 잘 되었습니다."); } }
private int dml(String sql) throws SQLException { try( Connection conn = MyConn.getConnection(); //데이터베이스 연결 해주기 Statement stmt = conn.createStatement(); //Statement 생성 해주기 ){ return stmt.executeUpdate(sql); } }
private void delete(String sql) throws SQLException { //삭제 해주는 메소드 System.out.println(dml(sql) + "행이 삭제되었습니다."); }
private void update(String sql) throws SQLException { //수정 해주는 메소드 int result = dml(sql); System.out.println(result + "행이 수정되었습니다."); }
private void insert(String sql) throws SQLException { //데이터 추가 해주는 메소드 int result = dml(sql); System.out.println(result + "행이 입력되었습니다."); }
private void select(String sql) throws SQLException { //조회 해주는 메소드 try( Connection conn = MyConn.getConnection(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); ) { ResultSetMetaData rsmd = rs.getMetaData(); int count = rsmd.getColumnCount();
for(int i=1; i<count+1; i++) { System.out.print(rsmd.getColumnLabel(i) + " "); } System.out.println();
int rowCount = 0;
while(rs.next()) { for(int i=1; i<count+1; i++) System.out.print(rs.getString(i) + " "); System.out.println(); rowCount++; } System.out.println(rowCount + "행이 선택되었습니다."); } } }
|