기본 환경: IDE: Eclipse, Language: Java

 

 

발생 Error

Java에서 다음 Source Code를 실행할 경우,

package dbtest.dao;

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

public class UpdateMain {
	private Connection conn;
	private PreparedStatement pstmt;
	
	private String driver = "oracle.jdbc.driver.OracleDriver"; // path 설정
	private String url = "jdbc:oracle:thin:@localhost:1521:xe";
	private String userName = "C##JAVA";
	private String passWord = "1234";
	
	public UpdateMain() {
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		System.out.println("Driver Loading");
	} // default Constructor;
	
	public void getConnection() {
		try {
			conn = DriverManager.getConnection(url, userName, passWord);
			System.out.println("conn Success");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	} // getConnection();
	
	public void updateArticle() {
		Scanner scan = new Scanner(System.in);
		System.out.print("검색할 이름 입력: ");
		String search_name = scan.next();
		String ss_name = "%" + search_name + "%";
		
		this.getConnection();
		
		String sql = "UPDATE DBTEST SET AGE=AGE+1 HEIGHT=HEIGHT+1 WHERE NAME LIKE '?'";
	
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, ss_name);
			
			int num_update = pstmt.executeUpdate();
			System.out.println(num_update + "개의 행이 업데이트되었습니다.");
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if(pstmt!=null) {pstmt.close();}
				if(conn!=null) {conn.close();}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
	
	public static void main(String[] args) {
		UpdateMain um = new UpdateMain();
		um.updateArticle();
	}
}


/*
검색할 이름 입력 : 홍 -> %홍%
홍이 들어간 레코드의 나이와 키를 1씩 증가
*/

java.sql.SQLException: 부적합한 열 인덱스 발생

 

 

Exception 원인

UPDATE DBTEST SET AGE=AGE+1 HEIGHT=HEIGHT+1 WHERE NAME LIKE ?

UPDATE를 실행 할 col을 구분하지 않음(SQL 구문 입력 실수)

 

 

해결 방법

AGE=AGE+1 HEIGHT=HEIGHT+1

-> AGE=AGE+1, HEIGHT=HEIGHT+1

		String sql = "UPDATE DBTEST SET AGE=AGE+1, HEIGHT=HEIGHT+1 WHERE NAME LIKE ?";
	
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, "%" + search_name + "%");
			
			int num_update = pstmt.executeUpdate();
			System.out.println(num_update + "개의 행이 업데이트되었습니다.");
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if(pstmt!=null) {pstmt.close();}
				if(conn!=null) {conn.close();}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

 기본 환경: IDE: Eclipse, Language: Java

 

발생 Error

Java에서 다음 Source Code를 실행할 경우,

public class PrimitiveError {
    public static void main(String[] args) {
    
        double a = 1.1;
        System.out.println(a.getClass());
    
    }

}

 Unresolved compilation problem: 

Cannot invoke getClass() on the primitive type double 발생

 

 

Error 원인

Primitive type variable: getClass() 호출 불가능

Primitive type Wrapper Class
boolean
char
byte / short / int / long
float / double
Boolean
Character
Byte / Short / Integer / Long
Float / Double

 

 

해결 방법

객체 선언을 위한 Wrapper class 사용: Double a = New Double(1.1);

public class PrimitiveError {
    public static void main(String[] args) {
    
        // double a = 1.1;
        Double a = new Double(1.1);
        System.out.println(a.getClass());
    
    }

}

// class java.lang.Double

 

 

 

 

 기본 환경: IDE: Eclipse, Language: Java

 

발생 Error

Java에서 다음 Source Code를 실행할 경우,

public class ArgsNull {

	public static void main(String[] args) {
		String[] strArr = {"Com", "pu", "ter"};
		String inputId = args[0];
	}
}

Index 0 out of bounds for length 0

java.lang.ArrayIndexOutOfBoundsException 발생

 

 

Error 원인

String[] args에 배열값을 입력하지 않음

 

 

해결 방법

Run→Run Configuration→Arguments에 값 입력

⚠️ Run Configuration하기 전에 Run을 적어도 1번은 해야 해당 java 파일이 보임

⚠️ Names에 작업하려는 java file이 맞는지 확인

 

 기본 환경: IDE: Eclipse, Language: Java

 

 

발생 Exception

Java에서 다음 Source Code를 실행할 경우,

package member;

import java.io.Serializable;

public class MemberDTO implements Comparable<MemberDTO>, Serializable { 
	private String name;
	private int age;
	private String phone_num;
	private String address;
	
	public MemberDTO() {}
	
	public MemberDTO(String name, int age, String phone_num, String address) {
		this.name = name;
		this.age = age;
		this.phone_num = phone_num;
		this.address = address;
	}
	
	public void setName(String name) {this.name=name;}
	public void setAge(int age) {this.age=age;}
	public void setPhone_num(String phone_num) {this.phone_num=phone_num;}
	public void setAddress(String address) {this.address=address;}
	
	public String getName() {return name;}
	public int getAge() {return age;}
	public String getPhone_num() {return phone_num;}
	public String getAddress() {return address;}
	
	@Override
	public String toString() {
		return name		 + "\t"
			 + age 		 + "\t"
			 + phone_num + "\t"
			 + address;
	}

	@Override
	public int compareTo(MemberDTO mDTO) {
		return name.compareTo(mDTO.getName());
	}
	
}
package member;

import java.util.List;
import java.util.Scanner;

import java.util.Comparator;
import java.util.Collections;

public class MemberNameAsc implements Member {

	Scanner scan = new Scanner(System.in);

	@Override
	public void execute(List<MemberDTO> list) {
		
		// Comparable
		Collections.sort(list);
		
		System.out.println("\n이름\t나이\t핸드폰\t주소");
		for(MemberDTO mDTO : list) {
			System.out.println(mDTO + "  ");
		}
		
	} // execute

} // class

java.io.InvalidClassException 발생

 

 

Exception 원인

MemberDTO에서 comparator로 정렬된 파일을, Comparable을 적용시켜 해당 파일을 불러올 경우 발생

(저장할 때 사용한 serialVersionUID과 load할 때 사용한 serialVersionUID이 상이하여 발생)

 

 

해결 방법

사용할 정렬 기준(Comparable, Comparator)으로 파일을 저장한 후 같은 기준에서 해당 파일을 불러와야 함

 

 

참고 자료

📑 컴퓨터 학원 동기

 

 기본 환경: IDE: Eclipse, Language: Java

 

 

발생 Exception

Java에서 다음 Source Code를 실행할 경우,

package io;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.IOException;

public class ObjectMain {
	public static void main(String[] args) throws IOException {
		PersonDTO pDTO = new PersonDTO("홍길동", 25, 185.3);
		
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("result.txt"));
		oos.writeObject(pDTO);
		oos.close();
		
	}
}

java.io.NotSerializableException 발생

 

 

Exception 원인

입출력 처리 시, Object(객체)는 파일이나 네트워크로 전송이 되지 않음

객체를 byte[]화하여, 전송해야 함(Serializable)

 

 

해결 방법

Object type인 PersonDTO class에서 Serializable interface 구현

package io;

import java.io.Serializable;

public class PersonDTO implements Serializable { // abstract method X

	private String name;
	private int age;
	private double height;

	public PersonDTO(String name, int age, double height) {
		this.name = name;
		this.age = age;
		this.height = height;
	}
	
	public void setName(String name) {this.name = name;}
	public void setAge(int age) {this.age = age;}
	public void setHeight(double height) {this.height = height;}
	
	public String getName() {return name;}
	public int getAge() {return age;}
	public double getHeight() {return height;}
	
}
package io;

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.IOException;

public class ObjectMain {
	public static void main(String[] args) throws IOException {
		PersonDTO pDTO = new PersonDTO("홍길동", 25, 185.3);
		
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("result.txt"));
		oos.writeObject(pDTO);
		oos.close();
		
	}
}

/*
Result
Name: 홍길동
Age: 25
Height: 185.3
*/