[JSP] JDBC + Bean (+ Vector) : 데이터 입력(INSERT)하기


JDBC + Bean (+ Vector) : 데이터 조회(SELECT)하기



pack_Sample.DataBean.java


package pack_Sample;

public class DataBean {

	private int no;
	private String name;
	private int age;
	private String addr;
	
	public int getNo() {
		return no;
	}
	public void setNo(int no) {
		this.no = no;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getAddr() {
		return addr;
	}
	public void setAddr(String addr) {
		this.addr = addr;
	}
}



pack_Sample.Test.java


package pack_Sample;

import java.sql.*;
import java.util.Vector;

public class Test {
	
	public Vector mtdDBCP() {
		
		Connection 	conn = null;
		Statement	stmt = null;
		ResultSet	rs	 = null;
		
		Vector vector = new Vector();

		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
			
			String url = "jdbc:mysql://localhost:3306/silsp?";
			url += "useSSL=false&"; 			
			url += "serverTimezone=Asia/Seoul&"; 
			url += "useUnicode=true&";			
			url += "characterEncoding=UTF-8&"; 
			url += "allowPublicKeyRetrieval=true";
		
			String user = "root";
			String password = "1234";
			
			conn = DriverManager.getConnection(url, user, password);
			
			stmt = conn.createStatement();
			String sql = "select * from member order by no desc";
			rs = stmt.executeQuery(sql);

			if (rs != null) {
				while (rs.next()) {

					DataBean dataBean = new DataBean();
					
					dataBean.setNo(rs.getInt("no"));
					dataBean.setName(rs.getString("name"));
					dataBean.setAge(rs.getInt("age"));
					dataBean.setAddr(rs.getString("addr"));
					
					vector.add(dataBean);
				}
			}
			
			rs.close();
			stmt.close();
			conn.close();
			
		} catch (ClassNotFoundException e) {
			System.out.println("DB 연동 Class 이슈 : " + e.getMessage());
		} catch (SQLException e) {
			System.out.println("SQL 이슈 : " + e.getMessage());
		}
		
		return vector;
	}
	
}



UsingJDBC_JSP.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.sql.*"%>

<%
	Connection			conn	=	null;
	PreparedStatement 	pstmt	=	null;
	
	int 		no 		= 	4;
	String 		name 	= 	"유이아";
	int 		age		= 	25;
	String 		addr	=	"서대문구 대현동";

	int cnt		 		=	0;

	try {
		
		Class.forName("com.mysql.cj.jdbc.Driver");
		String url = "jdbc:mysql://localhost:3306/silsp?";
		url += "useSSL=false&"; 			
		url += "serverTimezone=Asia/Seoul&"; 
		url += "useUnicode=true&";			
		url += "characterEncoding=UTF-8&"; 
		url += "allowPublicKeyRetrieval=true";
	
		String user = "root";
		String password = "1234";
	
		conn = DriverManager.getConnection(url, user, password);
	
		String sql = "insert into member values (?, ?, ?, ?)";
		pstmt = conn.prepareStatement(sql);
		pstmt.setInt(1, no);
		pstmt.setString(2, name);
		pstmt.setInt(3, age);
		pstmt.setString(4, addr);
		
		int resNum = pstmt.executeUpdate();
%>

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JDBC에서 DB 연동하기 - INSERT</title>
</head>
<body>
	<%
		String msg = "";
	
		if(resNum < 1) {
			msg = "입력 내용이 없습니다.";
		} else {
	%>
		<h1><%=resNum %>줄이 새로 입력되었습니다.</h1>
	<% 
		}
	%>
		<h1><%=msg %></h1>

</body>
</html>

<%
	pstmt.close();
	conn.close();
	
	} catch(ClassNotFoundException e){
		out.println(e.getMessage());
	} catch(SQLException e){
		out.println(e.getMessage());
	}
%>


다시 select를 해 보면

정상적으로 데이터가 입력된 걸 볼 수 있다.

Categories:

JSP/Servlet