대학 생활/JAVA
[JAVA] MySQL 이클립스에서 Java 로 연동하기
opid
2014. 8. 15. 11:04
1. mysql-connector Librarie추가
현재 프로젝트 오른쪽 클릭 - Properties - Java Build Path - Add External JARs... (mysql 설치시 Java Connector 도 같이 설치된다.) |
2. MySQL DB, Java 코드로 연동하기
public class TestClass { public static void main(String[] args) { Connection con = null; Statement st = null; ResultSet rs = null; try { con = DriverManager.getConnection("jdbc:mysql://localhost", "root", "1234"); st = con.createStatement(); rs = st.executeQuery("SHOW DATABASES"); if (st.execute("SHOW DATABASES")) { rs = st.getResultSet(); } while (rs.next()) { String str = rs.getNString(1); System.out.println(str); } } catch (SQLException sqex) { System.out.println("SQLException: " + sqex.getMessage()); System.out.println("SQLState: " + sqex.getSQLState()); } finally { if (rs != null) { try { rs.close(); } catch (Exception e) {}} if (st != null) { try { st.close(); } catch (Exception e) {}} if (conn != null) { try {conn.close();} catch (Exception e) {}} } } }
문제점
SQLException: No suitable driver found for jdbc:mysql://localhost▶ 해결방법 : 위 1번 드라이버 추가했는지 확인할 것.
SQLException: Access denied for user 'root'@'localhost' (using password: YES)▶ 해결방법 : 링크