본문 바로가기
🍷DataBase/🎮Oracle

[Oracle]자바에 데이터베이스 연동하기 2(copyright by 전씨)

by 김말자 2022. 12. 21.
728x90
728x90
BIG

jre 시스템 마우스오른쪽 빌드패스 콘피규 빌드패스

에드 익스터널 자르

불러오기

 

package helloworld;

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

public class member {

	static PreparedStatement pstmt ; //null
	static Connection conn; // null, 인터페이스 형식
	private static String id;
	private static String name;
	private static int height;
	private static int weight;
	private static int age;
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//driver loading
		
			String driver ="oracle.jdbc.OracleDriver";
			String url = "jdbc:oracle:thin:@localhost:1521:xe";
			String usid = "sample"; //id
			String password = "1234"; // 비번
			try {
				//connection
				Class.forName(driver); //로딩
			System.out.println("오라클 드라이버 로딩 성공");
			
			conn = DriverManager.getConnection(url, usid, password);
			//System.out.println("오라클 연결 성공");
		
			Scanner sc = new Scanner(System.in);
			System.out.println("아이디,이름,키,몸무게,나이입력");
			String id =sc.next();
			String name =sc.next();
			int height = sc.nextInt();
			int weight = sc.nextInt();
			int age = sc.nextInt();
			//sql command ,걍 임포트해서 넘겨준다는 뜻
			 pstmt = conn.prepareStatement("insert into member values(?, ?, ?, ?, ?)");
			// 물음표가 뭔지 알려주는 게 있어야됨
			pstmt.setString(1, id);
			pstmt.setString(2, name);
			pstmt.setInt(3, height);
			pstmt.setInt(4, weight);
			pstmt.setInt(5, age);
			pstmt.executeUpdate();
			//result
		
			
			
			} catch (Exception e) {
			// TODO: handle exception
		e.printStackTrace();
		} finally {
			
		}
		//에러잡기, finally 써주는게 좋음 근데 생략가능
		
		
		
	

}}
728x90
반응형
BIG

댓글