JAVA/chapter27_preparestatement

chapter05 : select

GAWON 2023. 5. 31. 09:39
package org.joonzis.ex;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.joonzis.db.DBConnection;

public class Ex04_select {
	public static void main(String[] args) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null; 

		try {
			conn = DBConnection.getConnection();
			ps = (PreparedStatement) conn.createStatement();

			String sql = "select * from person";
			
			rs = ps.executeQuery(sql);

		

			while (rs.next()) {
				System.out.print(rs.getString(1) + ",");
				System.out.print(rs.getInt(2) + ",");
				System.out.println(rs.getString(3));
			}

			
		} catch (Exception e) {
			e.printStackTrace();
			
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (ps != null) {
					ps.close();
				}
				if (conn != null) {
					conn.close();
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}

	}
}