import java.sql.*;
public class a_search {
//////////////////////student의 모든 정보를 조회해라
public static void main(String[] args) {
// 오라클 드라이버 변수 선언 및 사용 클래스 정의
final String JDBC_DRIVER = "oracle.jdbc.driver.OracleDriver";
//
//접속할 데이터베이스 url 지정'
final String DB_URL = "jdbc:oracle:thin:@192.168.0.232:1521:xe";
//접속할 계정 정보
final String USER = "C##EUNSUK";
final String PASSWORD = "root";
//커넥션 변수 선언
Connection conn = null;
//쿼리 구문 변수 선언
Statement statement = null;
try{
//jdbc 드라이버 클래스 로드
Class.forName(JDBC_DRIVER);
//데이터베이스 연결
conn = DriverManager.getConnection(DB_URL,USER,PASSWORD);
//SQL 쿼리 생성
statement = conn.createStatement();
String sql = "SELECT * FROM STUDENT where sname like '김%'";
//결과를 담아줄 resultset변수 선언
//select -> executequery(sql);
//insert, update, delete -> executeupdate(sql)
ResultSet resultset = statement.executeQuery(sql);
//결과출력
while(resultset.next()){
String sno = resultset.getString("sno");
String sname = resultset.getString("sname");
System.out.println("학번 " + sno + " 이름 " + sname);
}
resultset.close();
statement.close();
conn.close();
}catch ( SQLException se){
System.out.println(se.getMessage());
}catch (Exception e){
System.out.println(e.getMessage());
}finally {
//정상종료되든 에러가 발생하든 객체를 제거
if(statement !=null){
try {
statement.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
if(conn !=null){
try {
conn.close();
} catch (SQLException se) {
System.out.println(se.getMessage());
}
}
}
}
}
댓글 영역