Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- java
- Spring
- Android SDK
- 예제
- ibatis parameter
- SQL
- 설치
- 스프링
- zipcode
- IO
- 개행처리
- ibatis 개행
- MVC
- ibatis bind
- 외래키
- 자바
- 연동
- 아이폰
- 우편번호
- Oracle
- jdbc
- 이클립스
- SEQUENCE
- 안드로이드
- 오라클
- Eclipse
- Android
- Objective C
- iPhone
- IT·컴퓨터
Archives
- Today
- Total
MisoBoy Blog...
[20110715] Java JDBC (JDBC 로 ORACLE 연동하여 ZIPCODE 프로그램) 본문
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.swing.JOptionPane; // 1. import public class JDBCDemo1 { public static void main(String [] args){ // 2. Driver Loading try{ Class.forName("oracle.jdbc.driver.OracleDriver"); System.out.println("Driver loading Success"); }catch(ClassNotFoundException ex){ System.out.println("Class Not Found"); } // 3. DB Connection Connection conn = null; Statement stmt = null; ResultSet rs = null; try{ conn = DriverManager.getConnection ("jdbc:oracle:thin:@192.168.0.94:1521/ORCL", "scott", "tiger"); System.out.println("Connection Success"); // 4. Statement 객체 생성 stmt = conn.createStatement(); // 5. Query 실행 String dongName = JOptionPane.showInputDialog ("찾고자 하는 이름을 입력하세요 : ").trim(); String sql = "SELECT zipcode, sido, gugun, dong, bunji "; sql += "FROM zipcode where dong LIKE '%" + dongName + "%'"; //System.out.println("sql = " + sql); //코딩하는 중간 중간에 TEST 해보는게 좋다 rs = stmt.executeQuery(sql); // select 할때 사용 //stmt.executeUpdate(sql); // update 할때 사용 while(rs.next()){ String zipcode = rs.getString(1); // 컬럼 위치별로 지정을 한다. String sido = rs.getString("sido"); // 컬럼명으로 작성한다면 더욱 명확 해지므로 사용해도 된다. String gugun = rs.getString(3); String dong = rs.getString("dong"); String bunji = rs.getString(5); System.out.println (zipcode+ " " +sido+ " " +gugun+ " " +dong+ " " +dong+ " " +bunji); } }catch(SQLException ex){ ex.printStackTrace(); // 에러 메세지의 발생 근원지를 찾아서 단계별로 에러를 출력한다. }finally{ // 7. DB Close try{ if(rs != null) rs.close(); // ResultSet 을 닫는다. if(stmt != null) stmt.close(); // Statement 을 닫는다. if(conn != null) conn.close(); // Connection 을 닫는다. }catch(SQLException ex){} } } }
'Java' 카테고리의 다른 글
[20110720] ResultSetMetaData 객체를 사용하여 출력하는 예제 (0) | 2011.07.20 |
---|---|
[20110720] JDBC 를 통한 DB 연동 (0) | 2011.07.20 |
FileOutputStream 클래스를 이용해서 바이트 데이터를 파일에 쓰는 프로그램 (0) | 2011.07.12 |
FileWriter 클래스를 이용하여 문자 데이터를 파일에 쓰는 프로그램 (0) | 2011.07.11 |
Java IO FileReader 를 활용한 텍스트 파일을 읽는 프로그램 (0) | 2011.07.11 |