View
๋ฐ์ํ
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
public class JDBCBasic {
private static String driver = "com.mysql.cj.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3306/#DB๋ช
"; // jdbc:dbportocol://host:port/database์ด๋ฆ"
private static String user = "# ์ด๋ฆ ";
private static String password = "# ๋น๋ฒ ";
private static ArrayList<Department> deptList = new ArrayList<>();
public static void main(String[] args) throws Exception {
// 1.Driver Load - Class.forName("MySQL Driver์ด๋ฆ"); //com.mysql.jdbc.Drive
Class.forName(driver);
// 2.DB์ฐ๊ฒฐ - Connection conn = DriverMager.getConnection(url, id, password);
Connection conn = DriverManager.getConnection(url, user, password);
// 3.Query ์คํ - Statement stmt = conn.createStatement(); // Query์คํ ์ sql
// setting
// PreparedStatement pstmt = conn.prepareStatement(sql); // ๊ฐ์ฒด ์์ฑ์ sql setting -> data setting -> ์ฟผ๋ฆฌ ์คํ
// -> data setting ->
// execute()-DDL, executeUpdate()-INSERT, UPDATE, DELETE, executeQuery()-SELECT
String deptName = "Administrator";
int deptId = 10;
// String sqlUpdate = "UPDATE departments SET department_name=Administrator
// WHERE department _id=10";
String sqlUpdate = "UPDATE departments SET department_name=? WHERE department_id=?";
PreparedStatement pstmt = conn.prepareStatement(sqlUpdate); // sql: ? - in param
// data setting - in parm ์์๋๋กdata setting pstmt.setType(param์์, ๊ฐ );parm data sql ๋ก ๋ณํ X
pstmt.setString(1, deptName);
pstmt.setInt(2, deptId);
pstmt.executeUpdate(); // ์ฟผ๋ฆฌ ์คํ
Statement stmt = conn.createStatement();// sql ์์ด ๊ฐ์ฒด ์์ฑ
String sql = "SELECT department_id, department_name FROM departments WHERE department_id < 100";
ResultSet result = stmt.executeQuery(sql);// ์ฟผ๋ฆฌ ์คํ ์ sql setting , SQL Injection ์ทจ์ฝ()
// 4.Query ๊ฒฐ๊ณผ ์ฒ๋ฆฌ - ResultSet result = pstmt.executeQuery();
while (result.next()) {
deptList.add(new Department(result.getInt(1), result.getString(2)));
}
// 5. DB์ฐ๊ฒฐ ์ข
๋ฃ - result.close(), pstmt.close(), conn.close()
if (result != null)
result.close();
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
for (Department department : deptList) {
System.out.println(department.toString());
}
}
}
#Department.java
/*
DB์ Departments ํ
์ด๋ธ์ department_id, department_name ์ ๋ณด๋ฅผ ๋ด๋ ํด๋์ค
Entity Object
@author SSAFY * */
public class Department {
private int deptId;
private String deptName;
public Department(int deptId, String deptName) {
super();
this.deptId = deptId;
this.deptName = deptName;
}
public Department() {
super();
}
public int getDeptId() {
return deptId;
}
public void setDeptId(int deptId) {
this.deptId = deptId;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
@Override
public String toString() {
return "Department [deptId=" + deptId + ", deptName=" + deptName + "]";
}
}
Statement์ฌ์ฉํ๊ฒ ๋๋ค๋ฉด ๋ณ์๋ก ์ฃผ๊ฒ ๋๋ค๋ฉด SQL Injection ์ทจ์ฝ์ ์ทจ์ฝํ๊ธฐ ๋๋ฌธ์
Statement์ฌ์ฉ์ด ์๋ prepareStatement๋ก ๋ณ๊ฒฝํ์ฌ ์ฌ์ฉํจ → SQL ์ฟผ๋ฆฌ๋ฌธ์ผ๋ก ์คํ์ด ๋์ง ์์ !
๋ฐ์ํ
'CS > DB' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[MySQL] View์์ฑ ๋ฐ ์ญ์ (0) | 2022.09.14 |
---|---|
[MySQL] Mac์์ ์ดํด๋ฆฝ์ค MySQL ์ฐ๋ํ๊ธฐ (MySQL Connector Java) (0) | 2022.09.14 |
[DB] ๋งฅM1_MySQL ๋ค์ด Workbench (0) | 2022.09.12 |
[DB] ๋ฝ(Lock)์ด๋? (0) | 2022.09.08 |
[DB] ๐ก๋ ๋์ค(Redis)๋? (0) | 2022.09.08 |
reply