Java

[JAVA] 오라클 DB연결

웨일파도 2023. 4. 4. 10:13
반응형
package db_test2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBC_Ex1 {

	public static void main(String[] args) {
		
		Connection conn;
		Statement stmt = null;
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			String db_url = "jdbc:oracle:thin:@localhost:1521:xe";
			String db_id = "SYSTEM";
			String db_pw = "test1234";
			conn = DriverManager.getConnection(db_url, db_id, db_pw);
			System.out.println("DB 연결 완료");
			
			
			stmt = conn.createStatement();
			ResultSet srs = stmt.executeQuery("select * from student");
			printData(srs, "name","id","dept");
			
			
			

		} catch (ClassNotFoundException e) {
			System.out.println("JDBC 드라이버 로드 오류");
		} catch (SQLException e) {
			System.out.println("DB 연결 오류");
		}
	}
	
	
	// 레코드의 각 열의 값 화면의 출력
	private static void printData(ResultSet srs, String col1, String col2, String col3)
			throws SQLException {
		while (srs.next()) {
			if (!col1.equals(""))
				System.out.print(srs.getString("stu_name"));
			if(!col2.equals(""))
				System.out.print("\t|\t" + srs.getString("stu_no"));
			if(!col3.equals(""))
				System.out.println("\t|\t" + srs.getString("stu_dept"));
			else
				System.out.println();
			
		}
	}
	
	

}

반응형

'Java' 카테고리의 다른 글

[JAVA] 오라클 DB연결3  (0) 2023.04.04
[JAVA] 오라클 DB연결2  (0) 2023.04.04
[Java] 다차원배열  (0) 2023.03.10
[Java] 배열 문제  (0) 2023.03.09
[Java] 배열에서 중복 값 제거하기  (0) 2023.03.09