1) JDBC๋?
JDBC ๊ฐ์
-
JDBC(Java Database Connectivity)์ ์ ์
- ์๋ฐ๋ฅผ ์ด์ฉํ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ ์๊ณผ SQL ๋ฌธ์ฅ์ ์คํ, ๊ทธ๋ฆฌ๊ณ ์คํ ๊ฒฐ๊ณผ๋ก ์ป์ด์ง ๋ฐ์ดํฐ์ ํธ๋ค๋ง์ ์ ๊ณตํ๋ ๋ฐฉ๋ฒ๊ณผ ์ ์ฐจ์ ๊ดํ ๊ท์ฝ
- ์๋ฐ ํ๋ก๊ทธ๋จ ๋ด์์ SQL๋ฌธ์ ์คํํ๊ธฐ ์ํ ์๋ฐ API
- SQL๊ณผ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์ ํตํฉ ์ ๊ทผ ์ค ํ ํํ -
JAVA๋ ํ์ค ์ธํฐํ์ด์ค์ธ JDBC API๋ฅผ ์ ๊ณต
-
๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ฒค๋, ๋๋ ๊ธฐํ ์จ๋ํํฐ์์๋ JDBC ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ๋๋ผ์ด๋ฒ(driver)๋ฅผ ์ ๊ณตํ๋ค.
JDBC ํ๊ฒฝ ๊ตฌ์ฑ
-
JDK ์ค์น
-
JDBC ๋๋ผ์ด๋ฒ ์ค์น
- Maven์ ๋ค์๊ณผ ๊ฐ์ ์์กด์ฑ์ ์ถ๊ฐํ๋ค. MySQL์ฌ์ดํธ์์ ๋ค์ด๋ก๋ ํ๋ค.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
mysql ๋ฒ์ ์ cmd์ฐฝ์ด๋ terminal์ฐฝ์์ mysql -V ์น๋ฉด ๋์ค๋ ๊ฒ์ผ๋ก pom.xml์ dependency ์์ .
JDBC๋ฅผ ์ด์ฉํ ํ๋ก๊ทธ๋๋ฐ ๋ฐฉ๋ฒ
-
import java.sql.*;
-
๋๋ผ์ด๋ฒ๋ฅผ ๋ก๋ ํ๋ค.
-
Connection ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.
-
Statement ๊ฐ์ฒด๋ฅผ ์์ฑ ๋ฐ ์ง์ ์ํ
-
SQL๋ฌธ์ ๊ฒฐ๊ณผ๋ฌผ์ด ์๋ค๋ฉด ResultSet ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.
-
๋ชจ๋ ๊ฐ์ฒด๋ฅผ ๋ซ๋๋ค.
// 1. IMPORT
import java.sql.*;
// 2. ๋๋ผ์ด๋ฒ ๋ก๋
Class.forName( "com.mysql.jdbc.Driver" );
com.mysql.cj.jdbc.Driver
// 3. Connection ์ป๊ธฐ
String dburl = "jdbc:mysql://localhost/dbName";
"jdbc:mysql://localhost:3306/connectdb?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC"
Connection con = DriverManager.getConnection ( dburl, ID, PWD );
// ์์ค์ฝ๋ ์์
public static Connection getConnection() throws Exception{
String url = "jdbc:oracle:thin:@117.16.46.111:1521:xe";
String user = "smu";
String password = "smu";
Connection conn = null;
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(url, user, password);
return conn;
}
// 4. Statement ์์ฑ
Statement stmt = con.createStatement();
// 5. ์ง์ ์ํ
ResultSet rs = stmt.executeQuery("select no from user" );
// ์ฐธ๊ณ
stmt.execute(“query”); //any SQL
stmt.executeQuery(“query”); //SELECT
stmt.executeUpdate(“query”); //INSERT, UPDATE, DELETE
// 6. ResultSet์ผ๋ก ๊ฒฐ๊ณผ ๋ฐ๊ธฐ
ResultSet rs = stmt.executeQuery( "select no from user" );
while ( rs.next() )
System.out.println( rs.getInt( "no") );
// 7. Close
rs.close();
stmt.close();
con.close();
JDBC ํด๋์ค์ ์์ฑ ๊ด๊ณ
JDBCํด๋์ค์ ์์ฑ๋จ๊ณ
// ์์ค์ฝ๋ ์์
public List<GuestBookVO> getGuestBookList(){
List<GuestBookVO> list = new ArrayList<>();
GuestBookVO vo = null;
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try{
conn = DBUtil.getConnection();
String sql = "select * from guestbook";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
vo = new GuestBookVO();
vo.setNo(rs.getInt(1));
vo.setId(rs.getString(2));
vo.setTitle(rs.getString(3));
vo.setConetnt(rs.getString(4));
vo.setRegDate(rs.getString(5));
list.add(vo);
}
}catch(Exception e){
e.printStackTrace();
}finally {
DBUtil.close(conn, ps, rs);
}
return list;
}
// ์์ค์ฝ๋ ์์
public int addGuestBook(GuestBookVO vo){
int result = 0;
Connection conn = null;
PreparedStatement ps = null;
try{
conn = DBUtil.getConnection();
String sql = "insert into guestbook values(guestbook_seq.nextval,?,?,?,sysdate)";
ps = conn.prepareStatement(sql);
ps.setString(1, vo.getId());
ps.setString(2, vo.getTitle());
ps.setString(3, vo.getConetnt());
result = ps.executeUpdate();
}catch(Exception e){
e.printStackTrace();
}finally {
DBUtil.close(conn, ps);
}
return result;
}
// ์์ค์ฝ๋ ์์
public static void close(Connection conn, PreparedStatement ps){
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {e.printStackTrace(); }
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {e.printStackTrace();}
}
}
์ค๋ฅ ์ก๊ธฐ
1. ์ค๋ฅ์ฐฝ(๊ฒฝ๊ณ ์ฐฝ): Establishing SSL connection without server's identity verification is not recommended.
ํด๊ฒฐ๋ฒ: dburl ์ ๊ณ ์นจ
/*
* ๊ฒฝ๊ณ ๋ค. ๊ทธ๋ฅ ๋์ด๊ฐ๋ค. ํ์ง๋ง ์คํ๋ง๋ค ๊ฒฝ๊ณ ๋ฅผ ๋ณด๊ณ ์ถ์ง ์์ผ๋ฉด ์ด๋ ๊ฒ ํ๋ฉด ๋๋ค.
*
* ์ฐ์ ์ ๋ด์ฉ์ ์ต์ mysql๋ฒ์ ์ ์ฌ์ฉํ ๊ฒฝ์ฐ, ๋ช
์์ ์ธ SSL ์ฐ๊ฒฐ์ ์ฌ์ฉํ์ง ์๋๋ค๊ณ ๋ช
์ํ์ง ์์ผ๋ฉด
* ๊ธฐ๋ณธ์ ์ผ๋ก SSL์ฐ๊ฒฐ์ ์ฌ์ฉํด์ผ ํ๊ธฐ ๋๋ฌธ์ ๋์ค๋ ์๋ฌ์ด๋ค. ์์ ์๋ ๊ทธ๋ฅ
* jdbc:mysql://localhost:3306/test์ด๋ฐ ์์ผ๋ก ์ ์ uri๋ฅผ ์ค์ ํ์๋ค.
* ์ฌ๊ธฐ์๋ ๋ช
์์ ์ธ SSL์ฐ๊ฒฐ์ค์ ์ด ์๋ค.
* ํด๊ฒฐ๋ฐฉ๋ฒ์ SSL์ ์ ๋ช
์์ ์ผ๋ก ์ฌ์ฉํ์ง ์๋๋ค๊ณ ์ค์ ํ๊ฑฐ๋ (useSSL=false) useSSL=true๋ก ์ค์ ํ ๊ฒฝ์ฐ๋
* ์๋ฒ์ธ์ฆ๊ฒ์ฆ์ ์ํ truststore๋ฅผ ์ ๊ณตํ๋ฉด ๋๋ค. ๊ฐ๋ฐ์์๋ ๊ทธ๋ฅ false๋ก ํ๋ฉด ํธํ๋ค. ์๋์ฒ๋ผ ํ๋ฉด ๋๋ค.
*
* private static String dburl = "jdbc:mysql://localhost:3306/connectdb?serverTimezone=Asia/Seoul&useSSL=false";
* ์ฌ๊ธฐ์์ connectdb๋ ๊ฐ์ ์ค์ ํด๋ db์ด๋ฆ์ ์ ๋๋ค
*/
2. ์ค๋ฅ์ฐฝ: java.sql.SQLException: Unable to load authentication plugin 'caching_sha2_password'.
ํด๊ฒฐ๋ฒ:
ALTER USER 'connectuser'@'%' IDENTIFIED WITH mysql_native_password BY 'connect123!@#';
flush privileges;
3. ์ํํ๋ค๋ฉด ํญ์ ์ ์ฅํ๊ณ maven → update project
4. XML๋ฌธ์์ ํ์ค์ w3c์์ ์ ํ๋ค๊ณ ํ๋ค.
'Spring > Boost Course Web' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
2.3 Servlet (0) | 2020.11.19 |
---|---|
2.2 ์น ๊ฐ๋ฐํ๊ฒฝ ์ค์ (0) | 2020.11.19 |
2.1 Web๊ฐ๋ฐ์ ์ดํด (0) | 2020.11.19 |
1.4 Maven (0) | 2020.11.19 |
1.3 ๊ฐ๋ฐํ๊ฒฝ ์ค์ (0) | 2020.11.09 |
1.2 SQL (0) | 2020.11.09 |