编写配置文件【db.properties】:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/jdbcStudy?useUnicode=true&characterEncoding=utf8&useSSL=true
username=root
password=admin#1234

编写连接数据库的工具类【JdbcUtils】

package com.yeyue.lesson02;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties; public class JdbcUtils { private static String dirver = null;
private static String url = null;
private static String username = null;
private static String password = null; static {
try{
//读取到配置文件
InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");
Properties properties = new Properties();
properties.load(in);
//获取到配置文件中的有用参数
dirver = properties.getProperty("driver");
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
//加载驱动
Class.forName(dirver); } catch (Exception e) {
e.printStackTrace();
}
} public static Connection getConnection() throws SQLException {
//连接数据库
return DriverManager.getConnection(url,username,password);
} public static void release(Connection conn, Statement st, ResultSet rs){
//关闭连接
if(rs!=null){
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(st!=null){
try {
st.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}

删除【delete】

package com.yeyue.lesson03;

import com.yeyue.lesson02.JdbcUtils;

import java.sql.*;

public class TestDelete {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement st = null;
ResultSet rs = null;
try {
conn = JdbcUtils.getConnection(); String sql = "DELETE FROM users WHERE id = ?"; st = conn.prepareStatement(sql); st.setInt(1,6); int i = st.executeUpdate();
if(i>0){
System.out.println("删除成功");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
JdbcUtils.release(conn,st,rs);
}
}
}

新增【insert】

package com.yeyue.lesson03;

import com.yeyue.lesson02.JdbcUtils;
import java.util.Date;
import java.sql.*; public class TestInsert {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement st = null;
ResultSet rs = null;
try {
conn = JdbcUtils.getConnection();
//INSERT INTO `users` (id,NAME,PASSWORD,email) VALUES (6,'wewwqw','2323','323wwqd')
String sql = "INSERT INTO `users` (id,NAME,PASSWORD,email,birthday) VALUES (?,?,?,?,?)"; st = conn.prepareStatement(sql); //预编译sql 不执行 st.setInt(1,7);
st.setString(2,"wewwqw");
st.setString(3,"2323");
st.setString(4,"323wwqd");
//java.sql.Date 数据库 java.util.Date java new Date().getTime() 获取时间戳
st.setDate(5,new java.sql.Date(new Date().getTime())); int i = st.executeUpdate();
if(i>0){
System.out.println("插入成功");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
JdbcUtils.release(conn,st,rs);
}
}
}

改【update】

package com.yeyue.lesson03;

import com.yeyue.lesson02.JdbcUtils;

import java.sql.*;

public class TestUpdate {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement st = null;
ResultSet rs = null;
try {
conn = JdbcUtils.getConnection(); String sql = "UPDATE users SET NAME = 'yeyue1111' WHERE id = ?"; st = conn.prepareStatement(sql); st.setInt(1,4); int i = st.executeUpdate();
if(i>0){
System.out.println("更新成功");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
JdbcUtils.release(conn,st,rs);
}
}
}

mysql之PreparedStatement的增删改的更多相关文章

  1. jdbc 09: preparedStatement实现增删改查

    jdbc连接mysql,利用preparedStatement实现增删改查 package com.examples.jdbc.o9_preparedStatement实现增删改; import ja ...

  2. Vc数据库编程基础MySql数据库的表增删改查数据

    Vc数据库编程基础MySql数据库的表增删改查数据 一丶表操作命令 1.查看表中所有数据 select * from 表名 2.为表中所有的字段添加数据 insert into 表名( 字段1,字段2 ...

  3. python操作三大主流数据库(2)python操作mysql②python对mysql进行简单的增删改查

    python操作mysql②python对mysql进行简单的增删改查 1.设计mysql的数据库和表 id:新闻的唯一标示 title:新闻的标题 content:新闻的内容 created_at: ...

  4. 使用 NodeJS+Express+MySQL 实现简单的增删改查

    关于node.js暂时记录如下,以后有时间一定学习 文章来自简书,作者:sprint,2016-07 使用 Node.js + Express+MySQL 实现简单的增删改查 https://www. ...

  5. mysql 的基本操作总结--增删改查

    本文只是总结一下mysql 的基本操作,增删改查,以便忘记的时候可以查询一下 1.创建数据库 语法:CREATE DATABASES 数据库名; 例子: CREATE DATABASES studen ...

  6. php总结8——mysql函数库、增删改

    8.1 mysql函数库 php的函数   .php中用来操作mysql函数库的函数 常用函数 mysql_connect("主机名称/ip","用户名",&q ...

  7. Mysql数据表的增删改查

    ---恢复内容开始--- Mysql数据表的增删改查 1.创建表   语法:CREATE TABLE 表名(字段1,字段2,字段3.......) CREATE TABLE `users` ( `us ...

  8. Python进阶----数据库的基础,关系型数据库与非关系型数据库(No SQL:not only sql),mysql数据库语言基础(增删改查,权限设定)

    day37 一丶Python进阶----数据库的基础,mysql数据库语言基础(增删改查,权限设定) 什么是数据库:    简称:DataBase ---->DB    数据库即存放数据的仓库, ...

  9. MySQL的DML语言(增删改)

    MySQL的DML语言(增删改) 补充说明,外键:不要使用外键,一切外键概念都在应用层解决. 补充说明,数据库的列,也就是字段名,尽量带上飘符号` 数据库存在的意义:数据存储和数据管理. 数据库:行( ...

  10. MySQL数据库安装,MySQL数据库库的增删改查,表的增删改查,表数据的基本数据类型

    一 MySQL的安装 MySQL现在属于甲骨文公司,所以和java语言匹配度较高,同时甲骨文公司的另一种数据库为Oracle,两者同为关系型数据库,即采用关系模型来组织数据,以行和列的方法来存储数据的 ...

随机推荐

  1. 『玩转Streamlit』--表单Form

    在Streamlit中,Form组件是一种特殊的UI元素,允许用户输入数据而不立即触发应用的重新运行. 这对于创建需要用户输入多个参数后再进行处理的交互式表单非常有用. 1. 概要 Form组件的主要 ...

  2. HarmonyOS Next 入门实战 - 基础组件、页面实现

    基础组件 常用组件 Text:显示文本内容 Image:显示图片 Button:显示一个按钮 Column: 纵向布局 Row:横向布局 List:列表 各组件的用法 Text("文本组件& ...

  3. ChatGPT生成测试用例的最佳实践(四)

    通常情况下还应该进行测试用例外部评审.将已完成的基于百度关键字搜索业务的功能和安全测试用例集的存放位置告知项目团队成员,需要预留出一定的时间,便于项目组研发.产品人员阅读,以免在项目团队测试用例评审会 ...

  4. 理解 ASP.NET Core:Cookie 认证

    理解 ASP.NET Core:Cookie 认证 ASP.NET Core 内置提供了基于 Cookie 的认证支持.在使用 Cookie 验证的时候,相关的三要素: 认证模式名称:CookieAu ...

  5. 夜莺 v8 第一个版本来了,开始做有意思的功能了

    夜莺 v8 大版本已经启动开发,预计 25 年 7.8 月份发正式版,相比 v7 大概会做四五个大功能,每个功能做完了做稳定了都会提前放出来供大家体验,虽然以 beta 来命名,实际是稳定的,大家可以 ...

  6. Cesium 在线地图访问总结

    参考:https://deyihu.github.io/src/maptalks-tileLayercollection/examples/?tdsourcetag=s_pcqq_aiomsg 以下u ...

  7. RabbitMQ-限流

    1.简介 为什么要对消费端进行限流? 其实很好理解,比如我们常能接触到的消费场景:春运期间12306火车票的抢购,双11期间的下单等.这些场景都有一个共同点就是都会导致短暂时间内请求数激增,如果我们的 ...

  8. 编译树莓派Linux内核

    1.建议边看视频边跟着教程走 https://www.bilibili.com/video/av91990721?zw 2.准备工作 下载官方提供的交叉编译工具链 git clone https:// ...

  9. 特斯拉CEO埃隆马.斯克的五步工作法,怎么提高工程效率加速产品开发?

    简介 在<埃隆·马斯克传>这本书中,有两个章节写到了特斯拉 CEO 埃隆马斯克为了在一段时间内,提升特斯拉汽车 model 3 的产能到每个月 5000 辆这个数量级,在书中叫 " ...

  10. Docker npm install:npm ERR! code UNABLE_TO_GET_ISSUER_CERT_LOCALLY 。。reason: unable to get local issuer certificate 解决办法

    这个是需要证书导致无法连接,临时解决办法是设置 npm set strict-ssl=false 在 Dockerfile文件里的  RUN npm install 之前添加 RUN npm set ...