2017 JAVA 교육수료/DAY
- 33DAY / mysql / DISTINCT , INNER JOIN 2017.05.22
- 30DAY / jQuery 2017.05.17
- 29DAY / JSP , JAVA / DTO, DAO insertform ,list 2017.05.16
- 28DAY / DAO , DTO 2017.05.15
33DAY / mysql / DISTINCT , INNER JOIN
생성
Order.java
OrderDao.java
Order_Customer.java : Order객체와 Customer객체를 SETTING / GETTING 하는 클래스
OrderList.jsp : 출력하는 JSP파일
INNER JOIN
SELECT
FROM
INNER JOIN
ON
select orders.id , orders.date, orders.amount, orders.customer_id, customers.name
from orders
inner join customers
on customers.id = orders.customer_id;
30DAY / jQuery
자바스크립트는 실행문
ready를 쓰면 스크립트문을 위에쓸수있음
- focus & blur
- animate
- toggle
DOM변경
- append
- remove & empty
AJAX
참고사이트
Gson gson = new Gson();
String json = gson.toJson(list);
System.out.println(json);
out.println(json);
<script>
$(document).ready(function(){
$('button').click(function(){
$.get("./studentList.jsp",function(list){
console.log(list);-->콘솔창출력
$(list).each(function(index,item){
$('tbody').append('<tr>');
$('tbody').append('<td>'+item.no+'</td>');
$('tbody').append('<td>'+item.category+'</td>');
$('tbody').append('<td>'+item.title+'</td>');
$('tbody').append('<td>'+item.content+'</td>');
$('tbody').append('<td>'+item.day+'</td>');
$('tbody').append('</tr>');
});
});
});
});
</script>
*studentList는 데이터베이스 select하는 jsp문
29DAY / JSP , JAVA / DTO, DAO insertform ,list
입력폼과 리스트처리 _DAO
public ArrayList<Member> mAllSelect() throws ClassNotFoundException, SQLException{
DriverDB db = new DriverDB();
conn = db.driverDbcon();
ArrayList<Member> alm = new ArrayList<Member>();
pstmt = conn.prepareStatement("select * from tb_member");
rs = pstmt.executeQuery();
while(rs.next()){
Member m = new Member();
m.setM_id(rs.getString("m_id"));
m.setM_pw(rs.getString("m_pw"));
m.setM_level(rs.getString("m_level"));
m.setM_name(rs.getString("m_name"));
m.setM_email(rs.getString("m_email"));
System.out.println(m);
alm.add(m);
}
pstmt.close();
conn.close();
rs.close();
return alm;
}
ArrayList를 쓰는이유?
<%
Mdao dao = new Mdao();
ArrayList<Member> get_alm = dao.mAllSelect();
for(int i=0;i<get_alm.size();i++){
Member m = get_alm.get(i);
%>
<tr>
<td><%=m.getM_id()%></td>
<td><%=m.getM_pw()%></td>
<td><%=m.getM_level()%></td>
<td><%=m.getM_name()%></td>
<td><%=m.getM_email()%></td>
<td>
</tr>
<%}%>
28DAY / DAO , DTO
DAO(Data Access Object) : 데이터 베이스에 접속, 명령전송을 전담하는 클래스
package DTO;
public class Member {
//전역변수 = 필드 = 멤버 = 멤버필드 = 프로필터
private String m_id;
private String m_pw;
private String m_level;
private String m_name;
private String m_email;
public String getM_id() {
return m_id;
}
public void setM_id(String m_id) {
this.m_id = m_id;
}
public String getM_pw() {
return m_pw;
}
public void setM_pw(String m_pw) {
this.m_pw = m_pw;
}
public String getM_level() {
return m_level;
}
public void setM_level(String m_level) {
this.m_level = m_level;
}
public String getM_name() {
return m_name;
}
public void setM_name(String m_name) {
this.m_name = m_name;
}
public String getM_email() {
return m_email;
}
public void setM_email(String m_email) {
this.m_email = m_email;
}
}
package DAO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import driverdb.DriverDB;
import DTO.Member;
public class Mdao {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs=null;
public void mInsert(Member m) throws ClassNotFoundException, SQLException{
DriverDB db = new DriverDB();
conn = db.driverDbcon();
try{
pstmt = conn.prepareStatement(
"INSERT INTO tb_member VALUES (?, ?, ?, ?, ?)");
pstmt.setString(1, m.getM_id());
pstmt.setString(2, m.getM_pw());
pstmt.setString(3, m.getM_level());
pstmt.setString(4, m.getM_name());
pstmt.setString(5, m.getM_email());
pstmt.executeUpdate();
}
finally{
if (pstmt != null) try { pstmt.close(); } catch(SQLException ex) {}
if (conn != null) try { conn.close(); } catch(SQLException ex) {}
}
}
public void mDelete(String id) throws ClassNotFoundException, SQLException{
DriverDB db = new DriverDB();
conn = db.driverDbcon();
try{
pstmt = conn.prepareStatement(
"delete from tb_member where m_id=?");
pstmt.setString(1, id);
pstmt.executeUpdate();
}
finally{
if (pstmt != null) try { pstmt.close(); } catch(SQLException ex) {}
if (conn != null) try { conn.close(); } catch(SQLException ex) {}
}
}
public void mUpdate(Member m) throws ClassNotFoundException, SQLException{
DriverDB db = new DriverDB();
conn = db.driverDbcon();
pstmt = conn.prepareStatement(
"UPDATE tb_member SET m_pw=?,m_level=?,m_name=?,m_email=? WHERE m_id=?");
pstmt.setString(1, m.getM_pw());
pstmt.setString(2, m.getM_level());
pstmt.setString(3, m.getM_name());
pstmt.setString(4, m.getM_email());
pstmt.setString(5, m.getM_id());
pstmt.executeUpdate();
pstmt.close();
conn.close();
}
public ArrayList<Member> mAllSelect() throws ClassNotFoundException, SQLException{
DriverDB db = new DriverDB();
conn = db.driverDbcon();
ArrayList<Member> alm = new ArrayList<Member>();
pstmt = conn.prepareStatement("select * from tb_member");
rs = pstmt.executeQuery();
while(rs.next()){
Member m = new Member();
m.setM_id(rs.getString("m_id"));
m.setM_pw(rs.getString("m_pw"));
m.setM_level(rs.getString("m_level"));
m.setM_name(rs.getString("m_name"));
m.setM_email(rs.getString("m_email"));
System.out.println(m);
alm.add(m);
System.out.println(alm + "<---alm");
}
pstmt.close();
conn.close();
rs.close();
return alm;
}
public Member mSelectforUpdate(String mid) throws SQLException, ClassNotFoundException{
DriverDB db = new DriverDB();
conn = db.driverDbcon();
Member m = new Member();
pstmt = conn.prepareStatement("select * from tb_member where m_id=?");
pstmt.setString(1, mid);
rs = pstmt.executeQuery();
if(rs.next()){
System.out.println("select 쿼리 실행 결과 있다");
m.setM_id(rs.getString("m_id"));
m.setM_pw(rs.getString("m_pw"));
m.setM_level(rs.getString("m_level"));
m.setM_name(rs.getString("m_name"));
m.setM_email(rs.getString("m_email"));
}
pstmt.close();
conn.close();
rs.close();
return m;
}
}
DAO와 DTO를 이용하면 jsp문이 간단해진다.
ex) insert_pro.jsp
<%@ page import = "DAO.Mdao"%>
<% request.setCharacterEncoding("euc-kr"); %>
<jsp:useBean id="m" class="Dto.Member"/>
<jsp:setProperty name="m" property="*"/>
<%
Udao udao = new Udao();
udao.uUpdate(m);
response.sendRedirect(request.getContextPath() + "/usersearch/user_search_list.jsp");
%>