增值内容,请先登录
完整的J2SE桌面项目,从无到有完整的开发流程,涵盖全部52个知识点,154个开发步骤, 一共36个讲解视频,累计时长3小时10分59秒,大小1.94G,充实J2SE项目经验,为简历加上一个有吸引力的砝码
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import entity.Category;
import util.DBUtil;
public class CategoryDAO {
public int getTotal() {
int total = 0;
try (Connection c = DBUtil.getConnection(); Statement s = c.createStatement();) {
String sql = "select count(*) from category";
ResultSet rs = s.executeQuery(sql);
while (rs.next()) {
total = rs.getInt(1);
}
System.out.println("total:" + total);
} catch (SQLException e) {
e.printStackTrace();
}
return total;
}
public void add(Category category) {
String sql = "insert into category values(null,?)";
try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
ps.setString(1, category.name);
ps.execute();
ResultSet rs = ps.getGeneratedKeys();
if (rs.next()) {
int id = rs.getInt(1);
category.id = id;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void update(Category category) {
String sql = "update category set name= ? where id = ?";
try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
ps.setString(1, category.name);
ps.setInt(2, category.id);
ps.execute();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void delete(int id) {
try (Connection c = DBUtil.getConnection(); Statement s = c.createStatement();) {
String sql = "delete from category where id = " + id;
s.execute(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
public Category get(int id) {
Category category = null;
try (Connection c = DBUtil.getConnection(); Statement s = c.createStatement();) {
String sql = "select * from category where id = " + id;
ResultSet rs = s.executeQuery(sql);
if (rs.next()) {
category = new Category();
String name = rs.getString(2);
category.name = name;
category.id = id;
}
} catch (SQLException e) {
e.printStackTrace();
}
return category;
}
public List<Category> list() {
return list(0, Short.MAX_VALUE);
}
public List<Category> list(int start, int count) {
List<Category> categorys = new ArrayList<Category>();
String sql = "select * from category order by id desc limit ?,? ";
try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) {
ps.setInt(1, start);
ps.setInt(2, count);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Category category = new Category();
int id = rs.getInt(1);
String name = rs.getString(2);
category.id = id;
category.name = name;
categorys.add(category);
}
} catch (SQLException e) {
e.printStackTrace();
}
return categorys;
}
}
HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。
问答区域
2021-04-08
请问用truncate删除会更好一点吗
2 个答案
郁蒸十四 跳转到问题位置 答案时间:2021-04-25 truncate会删除掉所有的数据啊
how2j 跳转到问题位置 答案时间:2021-04-09 -_- 同学,请你先搞清楚 truncate 和 delete 的区别 ~
回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
2020-06-25
add方法的一点问题
回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
2020-03-27
add方法sql异常,无法返回自动生成的主键
提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
|