JDBC 연습문제-Dao 연습
12577 단어 ***
package com.test.vo;
import java.util.Date;
/**
* person
* @author dingshuangen
*
*/
public class Person {
private Integer personId;
private String personName;
private String degree;
private Date birth;
private Integer sal;
public Integer getPersonId() {
return personId;
}
public void setPersonId(Integer personId) {
this.personId = personId;
}
public String getPersonName() {
return personName;
}
public void setPersonName(String personName) {
this.personName = personName;
}
public String getDegree() {
return degree;
}
public void setDegree(String degree) {
this.degree = degree;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Integer getSal() {
return sal;
}
public void setSal(Integer sal) {
this.sal = sal;
}
public Person() {
super();
}
public Person(String personName, String degree, Date birth, Integer sal) {
super();
this.personName = personName;
this.degree = degree;
this.birth = birth;
this.sal = sal;
}
@Override
public String toString() {
return "Person [personId=" + personId + ", personName=" + personName + ", degree=" + degree + ", birth=" + birth
+ ", sal=" + sal + "]";
}
}
BaseDao 클래스
package com.test.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
* @author dingshuangen
*
*/
public class BaseDao {
/**
*
* @return
*/
public Connection getConnection() {
Connection conn=null;
try {
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc","root","0325");
return conn;
}catch(Exception e) {
e.printStackTrace();
}
return conn;
}
/**
* AutoCloseable
* @param auto
*/
public void close(AutoCloseable auto) {
if(auto!=null) {
try {
auto.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* sql sql
* @param sql
* @param objs
*/
public void executeSql(String sql,Object...objs) {
Connection conn=this.getConnection();
PreparedStatement ps=null;
try {
ps=conn.prepareStatement(sql);
// sql ?
for(int i=0;i> executQuery(String sql,Object...objs){
List
Dao 커넥터:
package com.test.dao;
import java.util.List;
import com.test.vo.Person;
public interface PersonDao {
/**
*
* @param person:
*/
public void insert(Person person);
/**
* :
* @param person: , ,
*/
public void update(Person person);
/**
*
* @param ids:
*/
public void delete(int[] ids);
/**
* , List
* @return :
*/
public List getAll();
/**
*
* @param pagesize:
* @param pageOrder: ( )
* @return
*/
public List getPersonForPage(int pagesize,int pageOrder);
/**
* ,
* @return
*/
public Person getPersonById(int id) ;
}
Dao 인터페이스의 구현 클래스:
package com.test.dao;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.test.vo.Person;
public class PersonDaoImpl extends BaseDao implements PersonDao {
@Override
public void insert(Person person) {
this.executeSql("insert into Person values(null,?,?,?,?)", person.getPersonName(),person.getDegree(),person.getBirth(),person.getSal());
}
@Override
public void update(Person person) {
this.executeSql("update person set personname=?,degree=?,birth=?,sal=? where personid=?", person.getPersonName(),person.getDegree(),person.getBirth(),person.getSal(),person.getPersonId());
}
@Override
public void delete(int[] ids) {
for(int i=0;i getAll() {
List> l=this.executQuery("select * from person order by personid");
return this.convert(l);
}
@Override
public List getPersonForPage(int pagesize, int pageOrder) {
return this.convert(this.executQuery("select * from person limit ?,? ", (pageOrder-1)*pagesize,pagesize));
}
@Override
public Person getPersonById(int id) {
Person p=null;
List list= this.convert(this.executQuery("select * from person where personid=?", id));
if(list!=null) {
p=list.get(0);
}
return p;
}
/**
* , List> List
* @param l
* @return
*/
private List convert(List> l){
List list=new ArrayList();
for (Map map : l) {
Person p=new Person();
// Person
p.setPersonId((Integer)map.get("personid"));
p.setPersonName((String)map.get("personname"));
p.setBirth((Date)map.get("birth"));
p.setDegree((String)map.get("degree"));
p.setSal((Integer)map.get("sal"));
list.add(p);
}
return list;
}
}
테스트 방법:
package com.test.test;
import java.sql.Date;
import com.test.dao.PersonDao;
import com.test.dao.PersonDaoImpl;
import com.test.vo.Person;
public class PersonDaoTest {
public static void main(String[] args) {
PersonDao dao=new PersonDaoImpl();
System.out.println(" :
"+dao.getAll());
System.out.println("-------------------------------------------------");
Date d=Date.valueOf("1997-02-20");
dao.insert(new Person(" "," ",d,5820));
System.out.println("-------------------------------------------------");
dao.insert(new Person(" "," ",Date.valueOf("1998-03-18"),9852));
System.out.println("-------------------------------------------------");
System.out.println(" :
"+dao.getAll());
System.out.println("-------------------------------------------------");
System.out.println(" id :
"+dao.getPersonById(2));
System.out.println("-------------------------------------------------");
System.out.println(" :");
Person p=new Person(" "," ",Date.valueOf("2000-8-9"),6300);
p.setPersonId(2);
dao.update(p);
System.out.println(" id :
"+dao.getPersonById(2));
System.out.println("-------------------------------------------------");
System.out.println(" :");
System.out.println(dao.getPersonForPage(5, 2));
System.out.println("-------------------------------------------------");
System.out.println(" :");
dao.delete(new int[]{1,2,3});
System.out.println("-------------------------------------------------");
System.out.println(" :
"+dao.getAll());
System.out.println("-------------------------------------------------");
}
}
실행 테스트 방법 결과:
:
[Person [personId=1, personName=gggg, degree= , birth=1996-03-15, sal=9690]
, Person [personId=2, personName=hhhh, degree= , birth=1989-03-26, sal=8900]
, Person [personId=3, personName=iiii, degree= , birth=1990-06-08, sal=7800]
, Person [personId=4, personName=XIAO, degree=SS, birth=1997-08-09, sal=5800]
, Person [personId=5, personName= , degree= , birth=1998-03-18, sal=9852]
, Person [personId=6, personName= , degree= , birth=1997-02-20, sal=5820]
, Person [personId=7, personName= , degree= , birth=1998-08-09, sal=6000]
, Person [personId=8, personName= , degree= , birth=1997-02-20, sal=5820]
, Person [personId=9, personName= , degree= , birth=1998-03-18, sal=9852]
, Person [personId=10, personName= , degree= , birth=1997-02-20, sal=5820]
, Person [personId=11, personName= , degree= , birth=1998-03-18, sal=9852]
]
-------------------------------------------------
***** ( insert into Person values(null,?,?,?,?) ) *****
-------------------------------------------------
***** ( insert into Person values(null,?,?,?,?) ) *****
-------------------------------------------------
:
[Person [personId=1, personName=gggg, degree= , birth=1996-03-15, sal=9690]
, Person [personId=2, personName=hhhh, degree= , birth=1989-03-26, sal=8900]
, Person [personId=3, personName=iiii, degree= , birth=1990-06-08, sal=7800]
, Person [personId=4, personName=XIAO, degree=SS, birth=1997-08-09, sal=5800]
, Person [personId=5, personName= , degree= , birth=1998-03-18, sal=9852]
, Person [personId=6, personName= , degree= , birth=1997-02-20, sal=5820]
, Person [personId=7, personName= , degree= , birth=1998-08-09, sal=6000]
, Person [personId=8, personName= , degree= , birth=1997-02-20, sal=5820]
, Person [personId=9, personName= , degree= , birth=1998-03-18, sal=9852]
, Person [personId=10, personName= , degree= , birth=1997-02-20, sal=5820]
, Person [personId=11, personName= , degree= , birth=1998-03-18, sal=9852]
, Person [personId=15, personName= , degree= , birth=1997-02-20, sal=5820]
, Person [personId=16, personName= , degree= , birth=1998-03-18, sal=9852]
]
-------------------------------------------------
id :
Person [personId=2, personName=hhhh, degree= , birth=1989-03-26, sal=8900]
-------------------------------------------------
:
***** ( update person set personname=?,degree=?,birth=?,sal=? where personid=? ) *****
id :
Person [personId=2, personName= , degree= , birth=2000-08-09, sal=6300]
-------------------------------------------------
:
[Person [personId=6, personName= , degree= , birth=1997-02-20, sal=5820]
, Person [personId=7, personName= , degree= , birth=1998-08-09, sal=6000]
, Person [personId=8, personName= , degree= , birth=1997-02-20, sal=5820]
, Person [personId=9, personName= , degree= , birth=1998-03-18, sal=9852]
, Person [personId=10, personName= , degree= , birth=1997-02-20, sal=5820]
]
-------------------------------------------------
:
***** ( delete from person where personid=? ) *****
***** ( delete from person where personid=? ) *****
***** ( delete from person where personid=? ) *****
-------------------------------------------------
:
[Person [personId=4, personName=XIAO, degree=SS, birth=1997-08-09, sal=5800]
, Person [personId=5, personName= , degree= , birth=1998-03-18, sal=9852]
, Person [personId=6, personName= , degree= , birth=1997-02-20, sal=5820]
, Person [personId=7, personName= , degree= , birth=1998-08-09, sal=6000]
, Person [personId=8, personName= , degree= , birth=1997-02-20, sal=5820]
, Person [personId=9, personName= , degree= , birth=1998-03-18, sal=9852]
, Person [personId=10, personName= , degree= , birth=1997-02-20, sal=5820]
, Person [personId=11, personName= , degree= , birth=1998-03-18, sal=9852]
, Person [personId=15, personName= , degree= , birth=1997-02-20, sal=5820]
, Person [personId=16, personName= , degree= , birth=1998-03-18, sal=9852]
]
-------------------------------------------------
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
빠른 정렬python 구현: 귀속과 비귀속텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.