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)▶ 해결방법 : 링크
'대학 생활 > JAVA' 카테고리의 다른 글
| [JAVA] Swing GTKLookAndFeel JAVA1.8에서 실행할 때 (0) | 2014.09.26 |
|---|---|
| [JAVA] 데몬 스레드(Daemon Thread) (0) | 2014.09.06 |
| [JAVA] 식사하는 철학자, 세마포어, 모니터 구현 (0) | 2014.07.30 |
| [JAVA] List 중복값 제거후 정렬하기 (0) | 2014.07.23 |