mysql jdbc 연결 관련해서 테스트 중인데요 0 2 547

by 준일정 [2018.03.14 15:17:03]


mysql.PNG (32,960Bytes)
mysql2.PNG (89,073Bytes)

members table의 모든 데이터 들입니다. 

 

자바단에서 2번째 컬럼인 email 컬럼을 뽑아지긴하는데여 

jdbc설정 부분인 드라이버 로딩 부분에서 에러가 뜸에도 불구하고 데이터는 뽑히는데 왜 이런걸까요

by 아발란체 [2018.03.14 20:05:55]

String driver = "com.mysql.jdbc.Driver";
Class.forName(driver);

오류가 발생한 구문은 요즘 자바 버전은 실행이 되지 않아도 됩니다.

try { 즉 해당 구문이 에러나거나 실행이 되지 않아도 되기 때문에 } catch(Exception) { } 구조로 다음 라인을 실행하여 정상 수행이 됩니다.

 

import java.sql.*;

public class Test_JDBC {
	public static void main(String[] args) { 
         Connection conn = null;
         Statement stmt = null;
         ResultSet rs = null;
         
         try {
        	 //String driver = "com.mysql.jdbc.Driver";
        	 //Class.forName(driver);
        	 conn = DriverManager.getConnection("jdbc:mysql://localhost/mysql", "root", "열려라참께");
        	 stmt = (Statement)conn.createStatement();
        	 String sql = "select 1";
    		 rs = stmt.executeQuery(sql);
    		 
    		 if(rs.next() == true) {
    			 System.out.println(rs.getInt(1));
    		 }
    		 
    		 if(rs != null) rs.close();
    		 if(stmt != null) stmt.close();
    		 if(conn != null) conn.close();
    		 
         } catch(Exception e) {
        	 e.printStackTrace();
        	 
         } finally {
        	 try {
        		 if(rs != null) rs.close();
        		 if(stmt != null) stmt.close();
        		 if(conn != null) conn.close();
        	 } catch(SQLException e) {
        		 e.printStackTrace();
        	 }
		}
		

	}

}

 


by 준일정 [2018.03.16 09:26:21]

답변 감사드립니다^^

댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입