PreparedStatement 실습

4399 단어 JDBCJDBC

쿼리문 전송 실습 1

package com.test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;

import com.util.DBConn;

public class Test001
{
	public static void main(String[] args)
	{
		try
		{
			Connection conn = DBConn.getConnection();
			
			if (conn != null)
			{
				System.out.println("데이터베이스 연결 성공!");
				
				try
				{
					/*
					Statement stmt = conn.createStatement();
					
					String sql = "INSERT INTO TBL_MEMBER(SID, NAME, TEL)"
							  + " VALUES(MEMBERSEQ.NEXTVAL, '한충희', '010-5555-5555')";
					int result = stmt.executeUpdate(sql);
					
					if (result > 0)
						System.out.println("데이터 입력 성공!");
					
					stmt.close();
					DBConn.close();
					*/
					
					String sql = "INSERT INTO TBL_MEMBER(SID, NAME, TEL)"
					//		  + " VALUES(MEMBERSEQ.NEXTVAL, '?', '?')";
							  + " VALUES(MEMBERSEQ.NEXTVAL, ?, ?)";   // preparedStatement로 넘겨줄땐 따옴표가 없어야한다.
					                                                  // 따옴표가 들어가냐 아니냐는 setString이냐 setInt냐로
					                                                  // 결정된다
					
					
					PreparedStatement pstmt = conn.prepareStatement(sql);
					
					// IN 매개변수 넘겨주기
					pstmt.setString(1, "박현수");
					pstmt.setString(2, "010-6666-6666");
					
					int result = pstmt.executeUpdate(); // executeUpdate는 적용된 행의 '갯수' 반환..
					if (result > 0)
					{
						System.out.println("데이터 입력 성공!");
					}
					pstmt.close();
					DBConn.close();
					
				} catch (Exception e)
				{
					System.out.println(e.toString());
					// TODO: handle exception
				}
			}
		} catch (Exception e)
		{
			System.out.println(e.toString());
		}
	}
}

쿼리문 전송 실습 2

package com.test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Scanner;

import com.util.DBConn;

public class Test002
{
	public static void main(String[] args)
	{
		try
		{
			Scanner sc = new Scanner(System.in);
			Connection conn = DBConn.getConnection();
			
			do
			{
				System.out.print("번호 입력(-1 종료) : ");
				String sid = sc.next();
				
				if (sid.equals("-1"))
				{
					break;
				}
				System.out.print("이름 입력 : ");
				String name = sc.next();
				
				System.out.print("전화번호 입력 : ");
				String tel = sc.next();
				
				if (conn != null)
				{
					System.out.println("데이터베이스 연결 성공!");
					
					try
					{
						String sql = "INSERT INTO TBL_MEMBER(SID, NAME, TEL)"
								  + " VALUES(?, ?, ?)";  // PreparedStatment에선 따옴표가 없음 check~!!
						
						PreparedStatement pstmt = conn.prepareStatement(sql);
						
						pstmt.setInt(1, Integer.parseInt(sid));
						pstmt.setString(2, name);
						pstmt.setString(3, tel);
						
						int result = pstmt.executeUpdate();
						if (result > 0)
						{
							System.out.println("회원 데이터 입력 완료");
						}
						
					} catch (Exception e)
					{
						System.out.println(e.toString());
					}
				}
			} while (true);
			
			DBConn.close();
			
			System.out.println();
			System.out.println("데이터베이스 연결 닫힘");
			System.out.println("프로그램 종료!");
			
		} catch (Exception e)
		{
			System.out.println(e.toString());
		}
	}
}

쿼리문 전송 실습 3

package com.test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.util.DBConn;

public class Test003
{
	public static void main(String[] args)
	{
		try
		{
			Connection conn = DBConn.getConnection();
			
			if (conn != null)
			{
				System.out.println("데이터베이스 연결 성공");
				
				try
				{
					// 쿼리문 준비
					String sql = "SELECT SID, NAME, TEL FROM TBL_MEMBER ORDER BY SID";
					
					// 작업 객체 생성(쿼리문 전달)
					PreparedStatement pstmt = conn.prepareStatement(sql);
				
					ResultSet rs = pstmt.executeQuery();
					
					while (rs.next())
					{
						int sid = rs.getInt("SID");
						String name = rs.getString("NAME");
						String tel = rs.getString("TEL");
						
						String str = String.format("%3d %7s %10s", sid, name, tel);
						
						System.out.println(str);
					}
					rs.close();
					pstmt.close();
					
					
				} catch (Exception e)
				{
					System.out.println(e.toString());
				}
			}
			
		} catch (Exception e)
		{
			System.out.println(e.toString());
		}
		
		DBConn.close();
		
		System.out.println("\n데이터베이스 연결 닫힘");
		System.out.println("프로그램 종료");
	}
}

좋은 웹페이지 즐겨찾기