package query;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import oracle.jdbc.pool.OracleDataSource;
public class Query3 {
OracleDataSource ds;
public Query3() {
try {
ds = new OracleDataSource();
ds.setURL("jdbc:oracle:thin:@211.234.53.67:1521:orcl");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void execute() {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = ds.getConnection("scott", "tiger");
stmt = conn.createStatement();
BufferedReader br =
new BufferedReader
(new InputStreamReader(System.in));
String sql = br.readLine();
rs = stmt.executeQuery(sql);
ResultSetMetaData rsmd = rs.getMetaData();
int count = rsmd.getColumnCount(); // 컬럼의 개수
// 컬럼명을 찍어주는 부분
for(int i=1; i<count+1; i++) {
System.out.print(rsmd.getColumnName(i)
+ rsmd.getColumnTypeName(i) + " ");
}
System.out.println();
while(rs.next()) {
for(int i=1; i<count+1; i++) {
System.out.print(rs.getString(i) + " ");
}
System.out.println();
}
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(rs != null) rs.close();
if(stmt != null) stmt.close();
if(conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
new Query3().execute();
}
}
'웹 개발 > JDBC 기초' 카테고리의 다른 글
[JDBC 예제] OracleDataSource를 사용한 QueryAnalyzer. (0) | 2009.06.23 |
---|---|
[JDBC 예제] OracleDataSouce를 사용한 PreparedStatement [?] 사용하기. (0) | 2009.06.23 |
[JDBC 예제] OracleDataSource를 사용하여 insert하기. (0) | 2009.06.23 |
[JDBC 예제] OracleDatasource를 사용하여 dept 데이터 뽑기. (0) | 2009.06.23 |
[JDBC 예제] OracleDatasource를 사용하여 emp테이블 뽑기. (0) | 2009.06.23 |