package Query;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import oracle.jdbc.pool.OracleDataSource;
public class QueryAnalyzer {
BufferedReader br;
OracleDataSource ds;
Connection con;
PreparedStatement pstmt;
Statement stmt;
ResultSet rs;
public QueryAnalyzer() {
br = new BufferedReader(
new InputStreamReader(System.in));
try {
ds = new OracleDataSource();
ds.setURL("jdbc:oracle:thin:@211.234.53.68:1521:orcl");
} catch (SQLException e) {
e.printStackTrace();
}
}
private void execute() {
try {
String sql = br.readLine();
//앞에 6글자만 잘라내기.
String Query = sql.substring(0, 6);
con=ds.getConnection("scott","tiger");
//String.equalsIgnoreCase("문자열 대소문자 구분없이 비교함.");
if(Query.equalsIgnoreCase("select")){
select(sql);
}else if(Query.equalsIgnoreCase("insert")||
Query.equalsIgnoreCase("update")||
Query.equalsIgnoreCase("delete")){
update(sql);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void select(String sql) {
try {
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
ResultSetMetaData rsmd = rs.getMetaData();
int count = rsmd.getColumnCount();
//데이터 뿌려주기
while(rs.next()){
for(int i=1; i<count+1; i++){
System.out.printf("%10s",rs.getString(i)+" ");
}
System.out.println();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private void update(String sql) {
try {
pstmt = con.prepareStatement(sql);
int co = pstmt.executeUpdate();
System.out.println(co+"행이 수정되었습니다.");
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new QueryAnalyzer().execute();
}
}
'웹 개발 > JDBC 기초' 카테고리의 다른 글
[JDBC] JNDI설정 및 initServlet 과 Enc Filter설정.. (0) | 2009.06.26 |
---|---|
[JDBC 예제] OracleDataSouce를 사용한 PreparedStatement [?] 사용하기. (0) | 2009.06.23 |
[JDBC 예제] OracleDataSource을 사용하여 sqlplus처럼 query처리. (0) | 2009.06.23 |
[JDBC 예제] OracleDataSource를 사용하여 insert하기. (0) | 2009.06.23 |
[JDBC 예제] OracleDatasource를 사용하여 dept 데이터 뽑기. (0) | 2009.06.23 |