}
package com.bdqn.dao;
/**
* 基类属性
* @author Administrator
*
*/
public class Base {
public static final String DRIVER="com.mysql.jdbc.Driver";
public static final String URL="jdbc:mysql://localhost:3306/student";
public static final String USERNAME="root";
public static final String PWD="root";
}
package com.bdqn.dao.impl;
import java.util.ArrayList;
import java.util.List;
import com.bdqn.entity.Dept;
/**
* 接口
* @author Administrator
*
*/
public interface DeptImpl {
//修改数据
public int update(Dept dept);
//增加数据
public int insertSql(Dept dept);
//删除数据
public int delete(int did);
//查询
public List
public List
}
package com.bdqn.entity;
/**
* @author Administrator
*
*/
public class Dept {
private int did;
private String dname;
private int dage;
private String dmesc;
public int getDid() {
return did;
}
public void setDid(int did) {
this.did = did;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public int getDage() {
return dage;
}
public void setDage(int dage) {
this.dage = dage;
}
public String getDmesc() {
return dmesc;
}
public void setDmesc(String dmesc) {
this.dmesc = dmesc;
}
public Dept(int did, String dname, int dage, String dmesc) {
this.did = did;
this.dname = dname;
this.dage = dage;
this.dmesc = dmesc;
}
public Dept() {
}
@Override
public String toString() {
return "Dpet [did=" + did + ", dname=" + dname + ", dage=" + dage
+ ", dmesc=" + dmesc + "]";
}
}
package com.bdqn.test;
import java.util.List;
import com.bdqn.entity.Dept;
import com.bdqn.util.DeptUtil;
/**
* 测试类
* @author Administrator
*
*/
public class Test {
public static void main(String[] args) {
DeptUtil du=new DeptUtil();
/*Dept dept=new Dept(4, "Tom_C", 22, "hsgyrtf");
du.insertSql(dept);*/
Dept dept=new Dept();
List
for (Dept dept2 : select) {
System.out.println(dept2.toString());
}
}
}
package com.bdqn.util;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.bdqn.dao.BaseDao;
import com.bdqn.dao.impl.DeptImpl;
import com.bdqn.entity.Dept;
/**
* 实现类
* @author Administrator
*
*/
public class DeptUtil extends BaseDao implements DeptImpl {
@Override
public int update(Dept dept) {
String sql="update dept set dname=? where did=?";
Object[] objs={dept.getDname(),dept.getDid()};
int num = this.insert(sql, objs);
return num;
}
@Override
public int insertSql(Dept dept) {
String sql="insert into dept values(?,?,?,?)";
Object[] objs={dept.getDid(),dept.getDname(),dept.getDage(),dept.getDmesc()};
int num = this.insert(sql, objs);
return num;
}
@Override
public int delete(int did) {
String sql="delete from dept where did=?";
Object[] objs={did};
int num = this.insert(sql, objs);
return num;
}
@Override
public List
String sql="select * from dept where did=?";
Object[] objs={did};
ResultSet rs = this.getResultSet(sql, objs);
List
try {
list=new ArrayList
while(rs.next()){
Dept dept1=new Dept();
int did1 = rs.getInt("did");
String dname = rs.getString("dname");
int dage = rs.getInt("dage");
String dmesc = rs.getString("dmesc");
dept1.setDid(did1);
dept1.setDname(dname);
dept1.setDage(dage);
dept1.setDmesc(dmesc);
list.add(dept1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
BaseDao.close(conn, null, rs);
}
return list;
}
@Override
public List
String sql="select * from dept";
ResultSet rs = this.getResultSet(sql, null);
List
try {
list=new ArrayList
while(rs.next()){
Dept dept1=new Dept();
int did = rs.getInt("did");
String dname = rs.getString("dname");
int dage = rs.getInt("dage");
String dmesc = rs.getString("dmesc");
dept1.setDid(did);
dept1.setDname(dname);
dept1.setDage(dage);
dept1.setDmesc(dmesc);
list.add(dept1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
BaseDao.close(conn, null, rs);
}
return list;
}
}