자바 DB 연동

min seung moon·2021년 7월 11일
0

이것이자바다 13 ~

목록 보기
4/5

1. Oracle

package com.company.design.ioc;

import java.sql.*;

public class Main {

    public static void main(String[] args) {

        String url = "jdbc:oracle:thin:@localhost:1521:xe";
        String user = "root";
        String password = "1234";
        String sql = "";

        Connection conn = null;
        ResultSet rs = null;
        PreparedStatement pstmt = null;

        try {
            Class.forName("oracle.jdbc.driver.OracleRiver");
            conn = DriverManager.getConnection(url, user, password);
            System.out.println("success");

            sql = "selct * from board";
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();

            while(rs.next()) {
                
            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }

    }
}

2. mysql

package com.company.design.ioc;

import java.sql.*;

public class Main {

    public static void main(String[] args) {

        String url = "jdbc:mysql://localhost:3306/TUTORIAL?useSSL=false&serverTimezone=UTC";
        String user = "root";
        String password = "1234";
        String sql = "";

        Connection conn = null;
        ResultSet rs = null;
        PreparedStatement pstmt = null;

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(url, user, password);
            System.out.println("success");

            sql = "selct * from board";
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();

            while(rs.next()) {

            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }

    }
}
profile
아직까지는 코린이!

0개의 댓글