View

[DB] JDBC ํ…Œ์ŠคํŠธ

์ฑ…์ฝ๋Š” ๊ฐ์ž 2022. 9. 14. 14:04
๋ฐ˜์‘ํ˜•
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 ์ฟผ๋ฆฌ๋ฌธ์œผ๋กœ ์‹คํ–‰์ด ๋˜์ง€ ์•Š์Œ !

๋ฐ˜์‘ํ˜•
Share Link
reply
๋ฐ˜์‘ํ˜•
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30