JDBC Mariadb 연결

ganadara·2023년 3월 21일
0

project

목록 보기
8/9

os

  • Windows 11
  • eclipse IDE
  • jdk-18.0.2.1
  • tomcat 9.0
  • mariadb-java-client-3.1.2.jar

톰캣설치

  1. https://tomcat.apache.org/download-90.cgi 톰캣(9.0) 다운로드
  2. bin/startup.bat 실행
  3. http://localhost:8080/ 접속되면 성공

Eclipse EE

Eclipse IDE는 기본적인 툴이라서 sever를 다루기 위해서는 Eclopse EE을 설치해야 한다.
Eclipse Java EE Developer Tools 설치

설치 후 재실행하면 sever 생김!

tomcat추가


  • 서버 실행 ctrl + alt + R

Spring initializr

JDBC Mariadb 연결에는 사용하지는 않았습니다.
https://start.spring.io/

MySql Workbench 오류

# mariadb 모든 권한 부여
grant all privileges on *.* to 'test'@'localhost';
select * from mysql.user where user='test';

mysql workbench 외부 구성 요소에서 예외를 throw했습니다

1. 파일 삭제

2. 비밀번호 오류

tomcat이랑 eclipse 연결

  1. Dynamic Web Project 생성
  2. project name 이름 적기
  3. project/webapp에 JSP파일 만들기
  4. body태그안에 "우리조최고야"적기
  5. Server실행

    이 상태가 되면 성공

성공

import java.sql.Connection; 밑줄생기는 오류

[Java] 이클립스와 Mysql 8.0 연동하기 (JDBC 연결)
따라해서 오류 해결

mariadb-java-client 이용해서 mariadb연결하기

  • test.java파일
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();
	}
}

성공

DB SELECT

  • test.java

    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();
			}
		}

	}
}

성공

profile
DL 공부중

0개의 댓글