1. ํ”„๋กœ์ ํŠธ ์šฐํด๋ฆญ ํ›„ Properties ํด๋ฆญ.

 

 

2.  JAVA Build Path -> Libraries -> Modulepath -> Add External Class Folder ์ˆœ์„œ๋กœ ํด๋ฆญ.

3. mysql-connector jar ํŒŒ์ผ ํด๋ฆญ

4.  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ด๋ฆ„ ์„ค์ • ํ›„  Apply and close ํด๋ฆญ.

 

5. ์„ฑ๊ณต์ ์œผ๋กœ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์ƒ์„ฑ ๋จ์„ ํ™•์ž„

 

6. ์ด์ œ ์ž๋ฐ”์ฝ”๋“œ๋ฅผ ํ†ตํ•ด JDBC ์—ฐ๊ฒฐ ์ง„ํ–‰ ๋ฐ ๊ธฐ์กด์— DB์— ์กด์žฌํ–ˆ๋˜ bbs ํ…Œ์ด๋ธ”์˜ mvcboard์˜ ๋‚ด์šฉ ํ™•์ธ.

 - ์†Œ์Šค์ฝ”๋“œ.

package jdbc;

import java.sql.*;
public class JDBC_conn {
	public static void main(String[] args) {
	 Statement stmt = null; //์ธํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์—†๋Š” ์ •์  ์ฟผ๋ฆฌ๋ฌธ ์‹คํ–‰
	 PreparedStatement psmt = null; // ์ธํŒŒ๋ผ๋งˆํ‹ฐํ„ฐ
	 ResultSet rs = null;
	
	 try {
		 Class.forName("com.mysql.cj.jdbc.Driver");
		 Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/DB๋ช…","DB id","DB PWD");
		 System.out.println("mysql db ์—ฐ๊ฒฐ ์„ฑ๊ณต");
		
		 
		 stmt = conn.createStatement();  // ์ฟผ๋ฆฌ ์‹คํ–‰์„ ์œ„ํ•œ statament ๊ฐ์ฒด ์ƒ์„ฑ
		 System.out.println("Statement ๊ฐ์ฒด ์ƒ์„ฑ ์„ฑ๊ณต");

		 rs =stmt.executeQuery("SELECT * FROM ์กฐํšŒ ํ•  ํ…Œ์ด๋ธ”๋ช…");
		 
		 while(rs.next()) {
			 String name =rs.getString("name");
			 String title =rs.getString("title");
			 
			 System.out.println(title);
			 System.out.println(name);
		 }
		 
		 
	 }
	 catch (ClassNotFoundException e) {
		 System.out.println("์—ฐ๊ฒฐ ์‹คํŒจ");
		 
	 }catch (SQLException e) {
		 System.out.println("ใ…‡db์ ‘์† ์‹œ๋ฅ˜ใ…");
	 }
	}
}

ํ™”๋ฉด ๊ฒฐ๊ณผ

 

+ Recent posts