public void save(Student stu){
String sql="INSERT INTO t_student(name,age) VALUES(?,?)";
Connection conn=null;
Statement st=null;
try{
// 1. 加载注册驱动
Class.forName("com.mysql.jdbc.Driver");
// 2. 获取数据库连接
conn=DriverManager.getConnection("jdbc:mysql:///jdbcdemo","root","root");
// 3. 创建语句对象
PreparedStatement ps=conn.prepareStatement(sql);
ps.setObject(1,stu.getName());
ps.setObject(2,stu.getAge());
// 4. 执行SQL语句
ps.executeUpdate();
// 5. 释放资源
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(st!=null)
st.close();
}catch(SQLException e){
e.printStackTrace();
}finally{
try{
if(conn!=null)
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
// 删除学生信息
public void delete(Long id){
String sql="DELETE FROM t_student WHERE id=?";
Connection conn=null;
Statement st=null;
try{
// 1. 加载注册驱动
Class.forName("com.mysql.jdbc.Driver");
// 2. 获取数据库连接
conn=DriverManager.getConnection("jdbc:mysql:///jdbcdemo","root","root");
// 3. 创建语句对象
PreparedStatement ps=conn.prepareStatement(sql);
ps.setObject(1,id);
// 4. 执行SQL语句
ps.executeUpdate();
// 5. 释放资源
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(st!=null)
st.close();
}catch(SQLException e){
e.printStackTrace();
}finally{
try{
if(conn!=null)
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
// 修改学生信息
public void update(Student stu){
String sql="UPDATE t_student SET name=?,age=? WHERE id=?";
Connection conn=null;
Statement st=null;
try{
// 1. 加载注册驱动
Class.forName("com.mysql.jdbc.Driver");
// 2. 获取数据库连接
conn=DriverManager.getConnection("jdbc:mysql:///jdbcdemo","root","root");
// 3. 创建语句对象
PreparedStatement ps=conn.prepareStatement(sql);
ps.setObject(1,stu.getName());
ps.setObject(2,stu.getAge());
ps.setObject(3,stu.getId());
// 4. 执行SQL语句
ps.executeUpdate();
// 5. 释放资源
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(st!=null)
st.close();
}catch(SQLException e){
e.printStackTrace();
}finally{
try{
if(conn!=null)
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
//工具类
public class JdbcUtil {
private JdbcUtil() { }
static {
// 1. 加载注册驱动
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
try {
// 2. 获取数据库连接
return DriverManager.getConnection("jdbc:mysql:///jdbcdemo", "root", "root");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//释放资源
public static void close(ResultSet rs, Statement st, Connection conn) {
try {
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (st != null)
st.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
// 增加学生信息
public void save(Student stu) {
String sql = "INSERT INTO t_student(name,age) VALUES(?,?)";
Connection conn = null;
PreparedStatement ps=null;
try {
conn = JDBCUtil.getConnection();
// 3. 创建语句对象
ps = conn.prepareStatement(sql);
ps.setObject(1, stu.getName());
ps.setObject(2, stu.getAge());
// 4. 执行SQL语句
ps.executeUpdate();
// 5. 释放资源
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtil.close(null, ps, conn);
}
}
// 删除学生信息
public void delete(Long id) {
String sql = "DELETE FROM t_student WHERE id=?";
Connection conn = null;
PreparedStatement ps = null;
try {
conn=JDBCUtil.getConnection();
// 3. 创建语句对象
ps = conn.prepareStatement(sql);
ps.setObject(1, id);
// 4. 执行SQL语句
ps.executeUpdate();
// 5. 释放资源
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtil.close(null, ps, conn);
}
}
// 修改学生信息
public void update(Student stu) {
String sql = "UPDATE t_student SET name=?,age=? WHERE id=?";
Connection conn = null;
PreparedStatement ps = null;
try {
conn=JDBCUtil.getConnection();
// 3. 创建语句对象
ps = conn.prepareStatement(sql);
ps.setObject(1, stu.getName());
ps.setObject(2, stu.getAge());
ps.setObject(3, stu.getId());
// 4. 执行SQL语句
ps.executeUpdate();
// 5. 释放资源
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtil.close(null, ps, conn);
}
}
public Student get(Long id) {
String sql = "SELECT * FROM t_student WHERE id=?";
Connection conn = null;
Statement st = null;
ResultSet rs = null;
PreparedStatement ps=null;
try {
conn = JDBCUtil.getConnection();
// 3. 创建语句对象
ps = conn.prepareStatement(sql);
ps.setObject(1, id);
// 4. 执行SQL语句
rs = ps.executeQuery();
if (rs.next()) {
String name = rs.getString("name");
int age = rs.getInt("age");
Student stu = new Student(id, name, age);
return stu;
}
// 5. 释放资源
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtil.close(rs, ps, conn);
}
return null;
}
public List<Student> list() {
List<Student> list = new ArrayList<>();
String sql = "SELECT * FROM t_student ";
Connection conn = null;
Statement st = null;
ResultSet rs = null;
PreparedStatement ps=null;
try {
conn=JDBCUtil.getConnection();
// 3. 创建语句对象
ps = conn.prepareStatement(sql);
// 4. 执行SQL语句
rs = ps.executeQuery();
while (rs.next()) {
long id = rs.getLong("id");
String name = rs.getString("name");
int age = rs.getInt("age");
Student stu = new Student(id, name, age);
list.add(stu);
}
// 5. 释放资源
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtil.close(rs, ps, conn);
}
return list;
}
static {
// 1. 加载注册驱动
try {
ClassLoader loader = Thread.currentThread
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
该项目为《Spring5核心原理与30个类手写实战》的配套代码示例,共包含320个文件,涵盖292个Java源代码文件、17个XML配置文件、4个Markdown文档、4个属性文件、3个HTML文件,旨在深入理解Spring5框架的核心原理。
资源推荐
资源详情
资源评论
收起资源包目录
基于Spring5核心原理的30个类手写实战设计源码 (317个子文件)
500.html 387B
first.html 348B
404.html 257B
JDBC.java 13KB
GPDispatcherServlet.java 11KB
GPDispatcherServlet.java 10KB
GPApplicationContext.java 9KB
GPDispatcherServlet.java 9KB
GPDispatcherServlet.java 6KB
ExamCode.java 6KB
GPProxy.java 5KB
DBConnectionPool.java 5KB
GPAdvisedSupport.java 5KB
GPBeanDefinitionReader.java 5KB
DataBaseCmd.java 4KB
GPHandlerAdapter.java 4KB
Pool.java 4KB
DispatcherServlet.java 4KB
GPMethodInvocation.java 3KB
MyAction.java 3KB
JdbcTemplate.java 2KB
JdbcTemplate.java 2KB
GPView.java 2KB
OrderServiceDynamicProxy.java 2KB
EventLisenter.java 2KB
ConManager.java 2KB
ConcurrentExecutor.java 2KB
EnumSingletonTest.java 2KB
PassportForThirdAdapter.java 2KB
Event.java 2KB
LogAspect.java 1KB
QiTianDaSheng.java 1KB
GPClassLoader.java 1KB
GPJdkDynamicAopProxy.java 1KB
EventLisenter.java 1KB
MouseEventCallback.java 1KB
DemoAction.java 1KB
Mouse.java 1KB
NetworkCourse.java 1KB
MouseEventCallback.java 1KB
Event.java 1KB
GPAbstractAspectAdvice.java 1KB
CglibTest.java 1KB
PrototypeTest.java 1KB
Mouse.java 1KB
PromotionActivityTest.java 1KB
SeriableSingletonTest.java 1KB
SiginForThirdService.java 1KB
SigninForThirdService.java 1KB
BattercakeTest.java 1KB
PromotionStrategyFactory.java 1KB
SigninForThirdService.java 1KB
GPMethodBeforeAdviceInterceptor.java 1KB
CGlibMeipo.java 1KB
GPHandlerMapping.java 1KB
OpenCloseTest.java 1KB
OrderServiceStaticProxy.java 1KB
MemberDao.java 1KB
LazyInnerClassSingleton.java 1KB
SinginForThirdService.java 1KB
GPAfterReturningAdviceInterceptor.java 1KB
GPViewResolver.java 1KB
CourseFactory.java 1KB
MemberDao.java 1005B
GPMeipo.java 1001B
JDKMeipo.java 1000B
LazyInnerClassSingletonTest.java 999B
ISiginForThirdService.java 984B
LiskovSubstitutionTest.java 975B
ISigninForThirdService.java 973B
SimpleTest.java 960B
Member.java 957B
GPAfterThrowingAdviceInterceptor.java 952B
Member.java 947B
PayStrategy.java 947B
Order.java 946B
ConcretePrototypeA.java 936B
DbRouteProxyTest.java 911B
IPassportForThird.java 894B
PayStrategyTest.java 892B
JDKProxyTest.java 884B
SeriableSingleton.java 883B
ContainerSingleton.java 882B
DependencyInversionTest.java 866B
HungrySingleton.java 863B
GPBeanDefinition.java 859B
ContainerSingletonTest.java 858B
Teacher.java 788B
DecoratorTest.java 783B
GPModelAndView.java 780B
Member.java 779B
Member.java 777B
ModifyService.java 769B
Teacher.java 759B
ThreadLocalSingletonTest.java 751B
SingleResponsibilityTest.java 749B
LazyDoubleCheckSingleton.java 740B
GPer.java 739B
Method.java 738B
MouseEventTest.java 735B
共 317 条
- 1
- 2
- 3
- 4
资源评论
lsx202406
- 粉丝: 2342
- 资源: 5562
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功