package com.wbq.jdbc2;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
* @ClassName JDBCDaoImpl
* @Description TODO
* @Author Administrator
* @Date 2020/12/8 14:29
* @Version 1.0
*/
public class JDBCDaoImpl {
public static int update(Employee em) {
Connection con = null;
PreparedStatement ps = null;
try {
con = JDBCUtil.getConnection();
ps = con.prepareStatement("update employee set name=?,address=?,borthdate=? where code =?");
ps.setString(1, em.getName());
ps.setString(2, em.getAddress());
ps.setDate(3, DateUtils.utilDateToSqlDate(em.getBorthdate()));
ps.setInt(4, em.getCode());
return ps.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return 0;
}
public static int insert(Employee em) {
Connection con = null;
PreparedStatement ps = null;
try {
con = JDBCUtil.getConnection();
ps = con.prepareStatement(" insert into employee (name,address,borthdate)values(?,?,?)");
ps.setString(1, em.getName());
ps.setString(2, em.getAddress());
ps.setDate(3, em.getBorthdate());
return ps.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return 0;
}
public static int delete(int code) {
Connection con = null;
PreparedStatement ps = null;
try {
con = JDBCUtil.getConnection();
ps = con.prepareStatement("delete from employee where code = ?");
ps.setInt(1, code);
return ps.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return 0;
}
public static Employee query(int code) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
Employee em = new Employee();
try {
con = JDBCUtil.getConnection();
ps = con.prepareStatement("select * from employee where code = ?");
ps.setInt(1, code);
rs = ps.executeQuery();
while (rs.next()) {
int code1 = rs.getInt("code");
String name = rs.getString("name");
String address = rs.getString("address");
Date borthdate = rs.getDate("borthdate");
em.setCode(code1);
em.setName(name);
em.setAddress(address);
em.setBorthdate(borthdate);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return em;
}
public static List<Employee> query() {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<Employee> list = new ArrayList<>();
try {
con = JDBCUtil.getConnection();
ps = con.prepareStatement("select * from employee");
rs = ps.executeQuery();
while (rs.next()) {
int code = rs.getInt("code");
String name = rs.getString("name");
String address = rs.getString("address");
Date borthdate = rs.getDate("borthdate");
Employee em = new Employee(code, name, address, borthdate);
list.add(em);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return list;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
JDBC+mysql 学习示例.zip
共26个文件
java:10个
xml:9个
gitignore:2个
需积分: 5 0 下载量 78 浏览量
2024-02-22
09:37:32
上传
评论
收藏 2.55MB ZIP 举报
温馨提示
JDBC+mysql 学习示例
资源推荐
资源详情
资源评论
收起资源包目录
JDBC+mysql 学习示例.zip (26个子文件)
kwan1117
lib
hutool-all-5.6.2.jar 1.78MB
mysql-connector-java-5.1.49-bin.jar 983KB
javaJdbc.iml 586B
src
jdbc.properties 135B
com
wbq
jdbc2
DBTest2.java 1KB
JDBCDaoImpl.java 4KB
JDBCUtil.java 1KB
Employee.java 1KB
DateUtils.java 800B
DBTest.java 954B
test
Test.java 379B
jdbc
DBUtils.java 1KB
PreparadeJdbc.java 1008B
TestJdbc.java 1KB
.idea
sqldialects.xml 218B
qaplug_profiles.xml 41KB
libraries
hutool_all_5_5_8_javadoc.xml 238B
mysql_connector_java_5_1_49_bin.xml 252B
hutool_all_5_6_2.xml 222B
vcs.xml 180B
misc.xml 610B
inspectionProfiles
Project_Default.xml 1KB
modules.xml 256B
.gitignore 176B
$PROJECT_FILE$ 364B
.gitignore 29B
共 26 条
- 1
资源评论
Kwan的解忧杂货铺
- 粉丝: 1w+
- 资源: 3651
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功