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().getContextClassLoader();
InputStream inputStream = loader.getResourceAsStream("db.properties");
p = new Properties();
p.load(inputStream);
Class.forName(p.getProperty("driverClassName"));
} catch (Exception e) {
e.printStackTrace();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
该项目是一个基于Java和HTML的Spring5框架设计源码实例,包含420个文件,其中包括292个Java源文件、57个XML配置文件、46个class文件、13个iml项目文件、4个Markdown文件、4个属性文件、3个HTML文件以及1个Git忽略文件。
资源推荐
资源详情
资源评论
收起资源包目录
基于Java和HTML的Spring5设计源码实例 (417个子文件)
PassportForThirdAdapter.class 3KB
PowerAdapter.class 1KB
PowerAdapter.class 1KB
SigninForThirdService.class 1KB
SinginForThirdService.class 1KB
Member.class 1KB
ResultMsg.class 1KB
FactoryMethodTest.class 1KB
SiginForThirdServiceTest.class 1KB
AbstractFactoryTest.class 1KB
SimpleFactoryTest.class 939B
CourseFactory.class 930B
SiginService.class 902B
RegistForQQAdapter.class 895B
ObjectAdapterTest.class 872B
PythonCourseFactory.class 869B
PassportTest.class 866B
PowerAdapterTest.class 862B
SigninForThirdServiceTest.class 860B
JavaCourseFactory.class 859B
LoginForWechatAdapter.class 828B
LoginForTokenAdapter.class 825B
AC220.class 823B
LoginForSinaAdapter.class 822B
LoginForTelAdapter.class 819B
AC220.class 818B
LoginForQQAdapter.class 816B
PassportService.class 682B
PythonVideo.class 652B
PythonNote.class 646B
JavaVideo.class 644B
JavaNote.class 638B
PythonCourseFactory.class 621B
JavaCourseFactory.class 613B
PythonCourse.class 608B
JavaCourse.class 600B
IPassportForThird.class 464B
CourseFactory.class 322B
RegistAdapter.class 321B
LoginAdapter.class 319B
ICourseFactory.class 220B
IVideo.class 165B
INote.class 161B
DC5.class 161B
DC5.class 160B
ICourse.class 151B
.gitignore 226B
500.html 375B
first.html 336B
404.html 248B
gupaoedu-vip-pattern-adapter.iml 3KB
gupaoedu-vip-pattern-decorator.iml 2KB
gupaoedu-vip-pattern-observer.iml 2KB
gupaoedu-vip-spring.iml 1KB
gupaoedu-vip-pattern-proxy.iml 1KB
gupaoedu-vip-pattern-delegate.iml 1KB
gupaoedu-vip-spring (1) (com.gupaoedu.vip).iml 1KB
gupaoedu-vip-pattern-strategy.iml 1KB
gupaoedu-vip-pattern-factory.iml 873B
gupaoedu-vip-pattern-template.iml 850B
gupaoedu-vip-pattern-singleton.iml 741B
gupaoedu-vip-pattern-prototype.iml 658B
spring5-samples-master.iml 336B
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
DispatcherServlet.java 4KB
GPHandlerAdapter.java 4KB
Pool.java 4KB
GPMethodInvocation.java 3KB
JdbcTemplate.java 2KB
MyAction.java 2KB
JdbcTemplate.java 2KB
GPView.java 2KB
OrderServiceDynamicProxy.java 2KB
EventLisenter.java 2KB
ConcurrentExecutor.java 2KB
EnumSingletonTest.java 2KB
PassportForThirdAdapter.java 2KB
ConManager.java 2KB
Event.java 2KB
LogAspect.java 1KB
QiTianDaSheng.java 1KB
GPClassLoader.java 1KB
EventLisenter.java 1KB
MouseEventCallback.java 1KB
Mouse.java 1KB
GPJdkDynamicAopProxy.java 1KB
NetworkCourse.java 1KB
MouseEventCallback.java 1KB
DemoAction.java 1KB
共 417 条
- 1
- 2
- 3
- 4
- 5
资源评论
xyq2024
- 粉丝: 2788
- 资源: 5533
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功