Java/JDBC
[JAVA_JDBC] JDBC ์ฐ๋ ๋ฐ ์ฟผ๋ฆฌ ์คํ
์ฝ๋ฆฐ์ด_ํฑ
2023. 4. 28. 21:41
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์ ์ ์๋ฅใ
");
}
}
}