整体项目结构
bean代码
package bean;
public class Apply {
private long id;
private String sname;
private String qq;
private long enterTime;
private String type;
private String school;
private long number;
private String repLink;
private String goal;
@Override
public String toString() {
return "Apply{" +
"id=" + id +
", sname='" + sname + '\'' +
", qq='" + qq + '\'' +
", enterTime=" + enterTime +
", type='" + type + '\'' +
", school='" + school + '\'' +
", number=" + number +
", repLink='" + repLink + '\'' +
", goal='" + goal + '\'' +
'}';
}
public Apply() {
}
public Apply(int id, String sname, String qq, long enterTime, String type, String school, long number, String repLink, String goal) {
this.id = id;
this.sname = sname;
this.qq = qq;
this.enterTime = enterTime;
this.type = type;
this.school = school;
this.number = number;
this.repLink = repLink;
this.goal = goal;
}
public Apply(String sname, String qq, long enterTime, String type, String school, long number, String repLink, String goal) {
this.sname = sname;
this.qq = qq;
this.enterTime = enterTime;
this.type = type;
this.school = school;
this.number = number;
this.repLink = repLink;
this.goal = goal;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
public long getEnterTime() {
return enterTime;
}
public void setEnterTime(long enterTime) {
this.enterTime = enterTime;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public long getNumber() {
return number;
}
public void setNumber(long number) {
this.number = number;
}
public String getRepLink() {
return repLink;
}
public void setRepLink(String repLink) {
this.repLink = repLink;
}
public String getGoal() {
return goal;
}
public void setGoal(String goal) {
this.goal = goal;
}
}
接口代码
package dao.impl;
import bean.Apply;
import java.util.List;
public interface IApplyDao {
int getTotal();
void add(Apply apply);
void delete(long id);
void update(Apply apply);
Apply get(long id);
List getAll();
}
实现代码
package dao.impl.impl;
import bean.Apply;
import dao.impl.IApplyDao;
import utils.UtilDAO;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class ApplyDAOImpl implements IApplyDao {
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// 添加数据
// Apply apply = new Apply(2, "nick", "43523", 3452341, "Html", "SCHOOL", 4, "hgw3204", "DAY DAY UP");
// new ApplyDAOImpl().add(apply);
// 删除数据
// new ApplyDAOImpl().delete(13);
// 更新数据
// new ApplyDAOImpl().update(apply);
// 获取对象
// final Apply apply = new ApplyDAOImpl().get(5);
// System.out.println(apply);
// 获取所有对象
final List all = new ApplyDAOImpl().getAll();
for (Apply apply :
all) {
System.out.println(apply);
}
}
@Override
public int getTotal() {
String sql = "select count(*) from applytable";
int total = 0;
try (final Connection c = UtilDAO.getConnection(); Statement st = c.createStatement();) {
final ResultSet rs = st.executeQuery(sql);
if (rs.next())
total = rs.getInt(1);
} catch (SQLException e) {
e.printStackTrace();
}
return total;
}
@Override
public void add(Apply apply) {
String sql = "insert into applytable" +
"(id,sname, qq, entertime, `type`, school,`number`,replink,goal) " +
"values (null, ?, ?, ?, ?, ?, ?, ?, ?)";
try (final Connection c = UtilDAO.getConnection(); PreparedStatement ps = c.prepareStatement(sql)) {
ps.setString(1, apply.getSname());
ps.setString(2, apply.getQq());
ps.setLong(3, apply.getEnterTime());
ps.setString(4, apply.getType());
ps.setString(5, apply.getSchool());
ps.setLong(6, apply.getNumber());
ps.setString(7, apply.getRepLink());
ps.setString(8, apply.getGoal());
ps.execute();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void delete(long id) {
String sql = "delete from applytable where id = " + id;
try (final Connection c = UtilDAO.getConnection(); final Statement st = c.createStatement()) {
st.execute(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void update(Apply apply) {
String sql = "update applytable set sname = ?, qq = ?, " +
"entertime = ?, `type` = ?, school = ?,`number` = ?, replink = ?, goal = ?" +
"where id = ?";
try (final Connection c = UtilDAO.getConnection(); final PreparedStatement ps = c.prepareStatement(sql)){
ps.setString(1, apply.getSname());
ps.setString(2, apply.getQq());
ps.setLong(3, apply.getEnterTime());
ps.setString(4, apply.getType());
ps.setString(5, apply.getSchool());
ps.setLong(6, apply.getNumber());
ps.setString(7, apply.getRepLink());
ps.setString(8, apply.getGoal());
ps.setLong(9, apply.getId());
ps.execute();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public Apply get(long id) {
String sql = "select * from applytable where id = " + id;
Apply apply = null;
try (final Connection c = UtilDAO.getConnection(); final Statement st = c.createStatement()) {
final ResultSet rs = st.executeQuery(sql);
while (rs.next()) {
apply = new Apply();
apply.setId(id);
apply.setSname(rs.getString("sname"));
apply.setQq(rs.getString("qq"));
apply.setEnterTime(rs.getLong("entertime"));
apply.setType(rs.getString("type"));
apply.setSchool(rs.getString("school"));
apply.setNumber(rs.getLong("number"));
apply.setRepLink(rs.getString("replink"));
apply.setGoal(rs.getString("goal"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return apply;
}
@Override
public List getAll() {
String sql = "select * from applytable order by id desc";
List applies = new ArrayList<>();
try (final Connection c = UtilDAO.getConnection(); final Statement st = c.createStatement()){
final ResultSet rs = st.executeQuery(sql);
while ((rs.next())) {
Apply apply = new Apply();
apply.setId(rs.getLong("id"));
apply.setSname(rs.getString("sname"));
apply.setQq(rs.getString("qq"));
apply.setEnterTime(rs.getLong("entertime"));
apply.setType(rs.getString("type"));
apply.setSchool(rs.getString("school"));
apply.setNumber(rs.getLong("number"));
apply.setRepLink(rs.getString("replink"));
apply.setGoal(rs.getString("goal"));
applies.add(apply);
}
} catch (SQLException e) {
e.printStackTrace();
}
return applies;
}
}
工具类代码
package utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class UtilDAO {
static final String URL = "jdbc:mysql://127.0.0.1:3306/xiuzhenyuan?characterEncoding=UTF-8";
static final String USERNAME = "root";
static final String PWD = "admin";
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL, USERNAME, PWD);
}
}