Web09_MySQL多表&JDBC
使用JDBC发送insert语句完成单表【添加】操作
使用JDBC发送update语句完成单表【更新】操作
使用JDBC发送delete语句完成单表【删除】操作
使用JDBC发送select语句完成单表【查询】操作
JDBCUtils_V3.java
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties; /**
* 提供获取连接和释放资源的方法
*/
public class JDBCUtils_V3 {
private static String driver;
private static String url;
private static String username;
private static String password; /*
* 静态代码块加载配置文件信息
*/
static { try {
// 1.通过当前类获取类加载器
ClassLoader classLoader = JDBCUtils_V3.class.getClassLoader();
// 2.通过类加载器的方法获得一个输入流
InputStream is = classLoader.getResourceAsStream("db.properties");
// 3.创建一个properties对象
Properties props = new Properties();
// 4.加载输入流
props.load(is);
// 5.获取相关参数的值
driver = props.getProperty("driver");
url = props.getProperty("url");
username = props.getProperty("username");
password = props.getProperty("password");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} /**
* 获取连接方法
*
* @return
*/
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
} public static void release(Connection conn, PreparedStatement pstmt, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
TestUtils.java
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; import org.junit.Test; import cn.itheima.jdbc.JDBCUtils_V1;
import cn.itheima.jdbc.JDBCUtils_V2;
import cn.itheima.jdbc.JDBCUtils_V3; /**
* 测试工具类
*/
public class TestUtils { /*
* 根据id更新信息方法
*/
@Test
public void testUpdateyId() {
Connection conn = null;
PreparedStatement pstmt = null; try {
// 1.获取连接
conn = JDBCUtils_V3.getConnection();
// 2.编写sql语句
String sql = "update tbl_user set upassword=? where uid=?";
// 3.获取执行sql语句对象
pstmt = conn.prepareStatement(sql);
// 4.设置参数
pstmt.setString(1, "999");
pstmt.setInt(2, 1);
// 5.执行更新操作
int row = pstmt.executeUpdate();
if (row > 0) {
System.out.println("更新成功!");
} else {
System.out.println("更新失败!");
}
// 释放资源放在此处行么?【不行滴!】
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
// 6.释放资源
JDBCUtils_V2.release(conn, pstmt, null);
}
} /*
* 根据id删除信息方法
*/
@Test
public void testDeleteById() {
Connection conn = null;
PreparedStatement pstmt = null; try {
// 1.获取连接
conn = JDBCUtils_V3.getConnection();
// 2.编写sql语句
String sql = "delete from tbl_user where uid=?";
// 3.获取执行sql语句对象
pstmt = conn.prepareStatement(sql);
// 4.设置参数
pstmt.setInt(1, 3);
// 5.执行删除操作
int row = pstmt.executeUpdate();
if (row > 0) {
System.out.println("删除成功!");
} else {
System.out.println("删除失败!");
}
// 释放资源放在此处行么?【不行滴!】
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
// 6.释放资源
JDBCUtils_V2.release(conn, pstmt, null);
}
} /*
* 添加用户信息方法
*/
@Test
public void testAdd() {
Connection conn = null;
PreparedStatement pstmt = null; try {
// 1.获取连接
conn = JDBCUtils_V2.getConnection();
// 2.编写sql语句
String sql = "insert into tbl_user values(null,?,?)";
// 3.获取执行sql语句对象
pstmt = conn.prepareStatement(sql);
// 4.设置参数
pstmt.setString(1, "lisi");
pstmt.setString(2, "lisi");
// 5.执行插入操作
int row = pstmt.executeUpdate();
if (row > 0) {
System.out.println("添加成功!");
} else {
System.out.println("添加失败!");
}
// 释放资源放在此处行么?【不行滴!】
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
// 6.释放资源
JDBCUtils_V2.release(conn, pstmt, null);
}
} /**
* 根据id查询用户信息
*/
@Test
public void testFindUserById() {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
// 1.获取连接
conn = JDBCUtils_V1.getConnection();
// 2.编写sql语句
String sql = "select * from tbl_user where uid=?";
// 3.获取执行sql语句对象
pstmt = conn.prepareStatement(sql);
// 4.设置参数
pstmt.setInt(1, 1);
// 5.执行查询操作
rs = pstmt.executeQuery();
// 6.处理结果集
while (rs.next()) {
System.out.println(rs.getString(2) + "----" + rs.getString("upassword"));
}
// 释放资源放在此处行么?【不行滴!】
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 7.释放资源
JDBCUtils_V1.release(conn, pstmt, rs);
}
}
}
Web09_MySQL多表&JDBC的更多相关文章
- 【JAVAWEB学习笔记】09_MySQL多表&JDBC(包含MySQL数据库思维导图)
今天晨读单词: order:订单constraint:(强制)约束foreign key:外键references:指向orderitem:订单项join:加入resourceBundle:资源捆绑c ...
- 数据库基础和JDBC
一SQL查询 练习: 1.在grade表中查找80-90分的学生学号和分数 select studentid 学号,score 分数 form grade where socre between 80 ...
- java_web学习(12)JDBC
数据持久化 持久化(persistence):把数据保存到可掉电式存储设备中以供之后使用.大多数情况下,特别是企业级应用,数据持久化意味着将内存中的数据保存到硬盘上加以”固化”,而持久化的 ...
- 回滚的意义---JDBC事务回滚探究
JDBC手动事务提交回滚的常见写法一直是rollback写在commit的catch之后: try{ conn.setAutoCommit(false); ps.executeUpdate(); ps ...
- JDBC API阐述
JDBC API JDBC API 是一系列的接口,它使得应用程序能够进行数据库联接,执行SQL语句,并且得到返回结果. Driver 接口 Java.sql.Driver 接口是所有 JDBC 驱动 ...
- Quartz动态配置表达的方法
在项目中有一个需求,需要灵活配置调度任务时间,并能自由启动或停止调度.有关调度的实现我就第一就想到了Quartz这个开源调度组件,因为很多项目使用过,Spring结合Quartz静态配置调度任务时间, ...
- 20175221《Java程序设计》第9周学习总结
20175221 <Java程序设计>第9周学习总结 教材学习内容总结 第十一章主要内容有: MySQL数据库管理系统 下载mysql-8.0.16-winx64 启动MySQL数据库 ...
- 2018-2019 2 20175230《Java程序设计》第九周学习总结
<Java程序设计>第九周学习总结 主要内容 MySQL数据库管理系统 1.下载 2.安装 启动MySQL数据库服务器 1.启动 2.root用户 MySQL客户端管理工具 建立连接 建立 ...
- hive笔记:复杂数据类型-map结构
map 结构 1. 语法:map(k1,v1,k2,v2,…) 操作类型:map ,map类型的数据可以通过'列名['key']的方式访问 案例: select deductions['Feder ...
随机推荐
- 测开常见面试题什么是redis
企业中redis是必备的性能优化中间件,也是常见面试题,首先Redis是由意大利人Salvatore Sanfilippo(网名:antirez)开发的一款内存高速缓存数据库.Redis全称为:Rem ...
- oracle 清空当前用户所有对象
BEGIN FOR REC IN (SELECT OBJECT_NAME,OBJECT_TYPE FROM USER_OBJECTS WHERE OBJECT_TYPE='PROCEDURE' OR ...
- GO (待更新)
日期20190531,GO AND TOOLS FOR HOME 0 环境搭建 https://golang.org/dl/ Install the Go tools If you are upgr ...
- ffmpeg函数03__av_seek_frame()
当需要把视频跳转到N秒的时候可以使用下面的方法:int64_t timestamp = N * AV_TIME_BASE; av_seek_frame(fmtctx, index_of_video, ...
- PMBOK :美国的项目管理知识体系
PMBOK 是Project Management Body Of Knowledge的缩写, 指项目管理知识体系的意思,具体是美国项目管理协会(PMI)对项目管理所需的知识.技能和工具进行的概括性描 ...
- Nginx动静分离经典案例配置
随着Nginx高性能Web服务器大量被使用,目前Nginx最新稳定版为1.2.6,张宴兄在实际应用中大量使用Nginx,并分享Nginx高性能Web服务器知识,使得Nginx在国内也是飞速的发展.那今 ...
- Apache工作流程
一个经典的Apache处理php页面的流程 需要连接mysql数据库并处理的流程 网站是一系列网页的组合 从用户角度看就是访问诸如 hhtp://www.baidu.com -----url 这是互联 ...
- C# out关键字
在c#中"out"关键字可以通过参数一次返回多个值. using System; namespace ConsoleApplication1 { internal class Pr ...
- 一组相关联的问题:“sudo: unable to resolve host ###: Connection timed out”、软件启动速度超慢、IPv6无法使用
造冰箱的大熊猫@cnblogs 2018/9/15 近日陆续发现计算机出现几个问题,最终发现这些问题实际上是由同一个原因导致的 问题1:无法使用IPv6 问题2:无论是启动Emacs GUI还是在命令 ...
- 【HDOJ5447】Good Numbers(数论)
题意: 思路:From https://blog.csdn.net/qq_36553623/article/details/76683438 大概就是把1e6里面的质因子能除的都除光之后借助两者gcd ...