package class06;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Test03 {
public static void main(String[] args) {
final String driverName="oracle.jdbc.driver.OracleDriver";
final String url="jdbc:oracle:thin:@localhost:1521:xe";
final String user="kim";
final String passwd="1234";
final String sql_selectOne="SELECT NAME FROM STUDENT WHERE NUM=3"; // PK 값으로 구분, 입력값을 받도록 설정해도 됨
final String sql_selectAll="SELECT * FROM STUDENT";
Connection conn=null;
Statement stmt=null;
try {
Class.forName(driverName);
conn=DriverManager.getConnection(url, user, passwd);
stmt=conn.createStatement();
/*
ResultSet rs=stmt.executeQuery(sql_selectOne); // bollean 타입이다.
if(rs.next()) {
System.out.println("이름: "+rs.getString("NAME"));
} // re.get자료형("칼럼명");
else {
System.out.println("결과 없음");
}
rs.close(); // rs를 다 사용했으면 닫아준다.
*/
ResultSet rs=stmt.executeQuery(sql_selectAll);
// forEach,Iterator, ... 와 비슷한 구조이다.
while(rs.next()) { // 자료들을 얼마만에 찾을 지 모르니까 while문 사용
System.out.println("이름: "+rs.getString("NAME"));
System.out.println("나이: "+rs.getInt("AGE"));
System.out.println("성적: "+rs.getInt("SCORE"));
System.out.println();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
stmt.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}