Eclipse IDE는 기본적인 툴이라서 sever를 다루기 위해서는 Eclopse EE을 설치해야 한다.
Eclipse Java EE Developer Tools 설치
설치 후 재실행하면 sever 생김!
tomcat추가
JDBC Mariadb 연결에는 사용하지는 않았습니다.
https://start.spring.io/
# mariadb 모든 권한 부여
grant all privileges on *.* to 'test'@'localhost';
select * from mysql.user where user='test';
mysql workbench 외부 구성 요소에서 예외를 throw했습니다
1. 파일 삭제
2. 비밀번호 오류
[Java] 이클립스와 Mysql 8.0 연동하기 (JDBC 연결)
따라해서 오류 해결
package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class test{
private static final String DB_DRIVER_CLASS = "org.mariadb.jdbc.Driver";
private static final String DB_URL = "jdbc:mariadb://!본인의 아이피 입력하세요!:3306/";
private static final String DB_USERNAME = "root";
private static final String DB_PASSWORD = "!본인의 비번입력하세요!";
private static Connection conn;
PreparedStatement pstmt = null;
private static void connectDB() {
try {
Class.forName(DB_DRIVER_CLASS);
Connection connection = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
System.out.println("연결성공");
} catch (ClassNotFoundException e) {
System.out.println("드라이브 로딩 실패");
} catch (SQLException e) {
System.out.println("DB 연결 실패");
}
}
public static void main(String[] args) {
test test = new test();
test.connectDB();
}
}
String sql = "select * from rnr.hotellist"; 에서
from DB명.TABLE명
package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class test {
public static void main(String[] args) {
final String DB_DRIVER_CLASS = "org.mariadb.jdbc.Driver";
final String DB_URL = "jdbc:mariadb://!본인의 아이피 입력하세요!:3306/";
final String DB_USERNAME = "root";
final String DB_PASSWORD = "!본인의 비밀번호를 입력하세요!";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Class.forName(DB_DRIVER_CLASS);
conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
if (conn != null) {
System.out.println("DB 접속 성공");
}
} catch (ClassNotFoundException e) {
System.out.println("드라이버 로드 실패");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("DB 접속 실패");
e.printStackTrace();
}
try {
String sql = "select * from rnr.hotel_list";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
String userId = null;
String password = null;
String name = null;
while (rs.next()) {
userId = rs.getString(1);
password = rs.getString(2);
name = rs.getString(3);
}
System.out.println(userId);
System.out.println(password);
System.out.println(name);
} catch (SQLException e) {
System.out.println("error: " + e);
} finally {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null && !conn.isClosed()) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}