jdbc笔记(一) 使用Statement对单表的CRUD操作
jdbc连接mysql并执行简单的CRUD的步骤:
1.注册驱动(需要抛出/捕获异常)
- Class.forName("com.mysql.jdbc.Driver");
2.建立连接需要抛出/捕获异常)
- connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb", "root", "Danny2036");
3.创建statement
- statement = connection.createStatement();
4.创建sql语句
- String sql = "insert into user values(10, \"noname\", 31)";
5.执行sql语句,如果是查询需要用ResultSet接收结果集
- int affectrows = statement.executeUpdate(sql);
- System.out.println(affectrows);
6.关闭资源
- if (statement != null) {
- statement.close();
- }
- if (connection != null) {
- connection.close();
- }
————————————————————————分割线——————————————————————————————————
最后贴出全部代码(包括insert,selectOne和selectAll)
- package com.colin.dao;
- import java.sql.*;
- public class JdbcTest {
- /**
- * 插入语句
- * @param id 主键id int
- * @param name 姓名 String
- * @param age 年龄 int
- */
- public static void update(int id, String name, int age) throws SQLException {
- long starttime = System.currentTimeMillis();
- Connection connection = null;
- Statement statement = null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- connection = DriverManager.getConnection(
- "jdbc:mysql://localhost:3306/testdb",
- "root", "Danny2036");
- statement = connection.createStatement();
- String sql = "update user set name = \'" + name + "\', age = "+ age +" where id = " + id;
- System.out.println(sql);
- int affectrows = statement.executeUpdate(sql);
- System.out.println(affectrows);
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- if (connection != null) {
- connection.close();
- }
- if (statement != null) {
- statement.close();
- }
- }
- System.out.println(System.currentTimeMillis() - starttime);
- }
- public static void selectOne(int id) throws ClassNotFoundException, SQLException {
- Statement statement = null;
- Connection connection = null;
- try {
- // 注册驱动
- Class.forName("com.mysql.jdbc.Driver");
- // 建立连接
- connection = DriverManager.getConnection(
- "jdbc:mysql://localhost:3306/testdb",
- "root", "Danny2036"
- );
- // 创建statement
- statement = connection.createStatement();
- // 定义sql语句
- String sql = "SELECT id, name, age FROM user where id = " + id;
- // 执行sql语句并获得结果
- ResultSet resultSet = statement.executeQuery(sql);
- // 遍历结果集
- while (resultSet.next()) {
- int idResult = (int) resultSet.getInt("id");
- String nameResult = (String) resultSet.getString("name");
- int ageResult = (int) resultSet.getInt("age");
- System.out.println(idResult + "\t" + nameResult + "\t" + ageResult);
- }
- } finally {
- if (statement != null) {
- statement.close();
- }
- if (connection != null) {
- connection.close();
- }
- }
- }
- public static void selectAll() throws ClassNotFoundException, SQLException {
- Statement statement = null;
- Connection connection = null;
- try {
- // 注册驱动
- Class.forName("com.mysql.jdbc.Driver");
- // 建立连接
- connection = DriverManager.getConnection(
- "jdbc:mysql://localhost:3306/testdb",
- "root", "Danny2036"
- );
- // 创建statement
- statement = connection.createStatement();
- // 定义sql语句
- String sql = "SELECT id, name, age FROM user";
- // 执行sql语句并获得结果
- ResultSet resultSet = statement.executeQuery(sql);
- // 遍历结果集
- while (resultSet.next()) {
- int idResult = (int) resultSet.getInt("id");
- String nameResult = (String) resultSet.getString("name");
- int ageResult = (int) resultSet.getInt("age");
- System.out.println(idResult + "\t" + nameResult + "\t" + ageResult);
- }
- } finally {
- if (statement != null) {
- statement.close();
- }
- if (connection != null) {
- connection.close();
- }
- }
- }
- public static void main(String[] args) throws SQLException, ClassNotFoundException {
- selectAll();
- }
- /*
- // 向上抛异常的方式
- public static void main(String[] args)
- throws ClassNotFoundException, SQLException {
- long starttime = System.currentTimeMillis();
- Connection connection = null;
- Statement statement = null;
- try {
- //注册驱动
- Class.forName("com.mysql.jdbc.Driver");
- //建立连接
- connection = DriverManager.getConnection(
- "jdbc:mysql://localhost:3306/testdb",
- "root", "Danny2036");
- //创建statement
- statement = connection.createStatement();
- //定义sql
- String sql = "insert into user values(10, \"noname\", 31)";
- //执行sql,如果是查询需返回结果集
- int affectrows = statement.executeUpdate(sql);
- System.out.println(affectrows);
- } finally {
- //关闭资源
- if (statement != null) {
- statement.close();
- }
- if (connection != null) {
- connection.close();
- }
- }
- System.out.println("总执行时间: " + (System.currentTimeMillis() - starttime));
- }
- */
- /*
- // 捕获异常的方式
- public static void main(String[] args) throws SQLException {
- long starttime = System.currentTimeMillis();
- Statement statement = null;
- Connection connection = null;
- try {
- // 注册驱动
- Class.forName("com.mysql.jdbc.Driver");
- // 建立连接
- connection = DriverManager.getConnection(
- "jdbc:mysql://localhost:3306/testdb?useSSL=true",
- "root", "Danny2036");
- // 创建statement
- statement = connection.createStatement();
- // 定义sql
- String sql = "insert into user values(9, \"ahaha\", 22)";
- // 执行sql,如果是查询,遍历结果集
- int affectrows = statement.executeUpdate(sql);
- System.out.println(affectrows);
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- // 关闭资源
- if (statement != null) {
- statement.close();
- }
- if (connection != null) {
- connection.close();
- }
- }
- System.out.println("总用时 : " + (System.currentTimeMillis() - starttime));
- }
- */
- }
——————————————————**********最后附上bean代码***********————————————————————
- package com.colin.bean;
- public class User {
- private int id;
- private String name;
- private int age;
- public User(int id, String name, int age) {
- this.id = id;
- this.name = name;
- this.age = age;
- }
- public User(String name, int age) {
- this.name = name;
- this.age = age;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- @Override
- public String toString() {
- return "User{" +
- "id=" + id +
- ", name='" + name + '\'' +
- ", age=" + age +
- '}';
- }
- }
jdbc笔记(一) 使用Statement对单表的CRUD操作的更多相关文章
- jdbc笔记(二) 使用PreparedStatement对单表的CRUD操作
首先声明,本文只给出代码,并不是做教程用,如有不便之处,还请各位见谅. PreparedStatement相较于Statement,概括来说,共有三个优势: 1. 代码的可读性和易维护性:Prepar ...
- 【Java EE 学习 44】【Hibernate学习第一天】【Hibernate对单表的CRUD操作】
一.Hibernate简介 1.hibernate是对jdbc的二次开发 2.jdbc没有缓存机制,但是hibernate有. 3.hibernate的有点和缺点 (1)优点:有缓存,而且是二级缓存: ...
- 6.单表的CRUD操作
1.插入后用新id初始化被插入对象 <insert id="insertStudentCatchId"> insert into student (age,name,s ...
- SQLServer学习笔记<>.基础知识,一些基本命令,单表查询(null top用法,with ties附加属性,over开窗函数),排名函数
Sqlserver基础知识 (1)创建数据库 创建数据库有两种方式,手动创建和编写sql脚本创建,在这里我采用脚本的方式创建一个名称为TSQLFundamentals2008的数据库.脚本如下: ...
- Django学习笔记(10)——Book单表的增删改查页面
一,项目题目:Book单表的增删改查页面 该项目主要练习使用Django开发一个Book单表的增删改查页面,通过这个项目巩固自己这段时间学习Django知识. 二,项目需求: 开发一个简单的Book增 ...
- MyBatis-Plus学习笔记(1):环境搭建以及基本的CRUD操作
MyBatis-Plus是一个 MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变,使用MyBatis-Plus时,不会影响原来Mybatis方式的使用. SpringBoot+M ...
- Oracle触发器实现监控某表的CRUD操作
前提:请用sys用户dba权限登录 1.创建一个表来存储操作日志 create table trig_sql( LT DATE not null primary key, SID NUMBER, SE ...
- 通过jdbc完成单表的curd操作以及对JDBCUtils的封装
概述:jdbc是oracle公司制定的一套规范(一套接口),驱动是jdbc的实现类,由数据库厂商提供.所以我们可以通过一套规范实现对不同的数据库操作(多态) jdbc的作用:连接数据库,发送sql语句 ...
- JDBC简单增删改查实现(单表)
0.准备工作 开发工具: MySQL数据库, intelliJ IDEA2017. 准备jar包: mysql-connector-java-5.1.28-bin.jar(其他均可) 1. 数据库数据 ...
随机推荐
- java学习(五)--- 方法
方法的定义 修饰符 返回值类型 方法名(参数类型 参数名){ ... 方法体 ... return 返回值; } 注意:非void方法必须有返回值 方法重载: 可以声明方法相同,但是参数类型不同的方法 ...
- 大规模微服务架构下的Service Mesh探索之路
小结: 1. 第一.二代Service Mesh meetup-slides/敖小剑-蚂蚁金服-大规模微服务架构下的Service Mesh探索之路.pdf https://github.com/se ...
- baiduMap & MapV 简单demo
看到 MapV 的一个demo 的底图比较好看,练练手 MapV demos:https://mapv.baidu.com/examples/ 参考的demo:https://mapv.baidu.c ...
- C++/C代码审查注意事项(摘录,非原创)
文件结构 头文件和定义文件的名称是否合理?头文件和定义文件的目录结构是否合理?版权和版本声明是否完整? 重要头文件是否使用了 ifndef/define/endif 预处理块?头文件中是否只存放“声明 ...
- docker-compose介绍
docker-compose 常用命令 Commands: build Build or rebuild services bundle Generate a Docker bundle from t ...
- LigerUi之ligerMenu 右键菜单
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- [ROS]激光驱动安装
参考资料: https://blog.csdn.net/hongliang2009/article/details/73302986 https://blog.csdn.net/bohaijun_12 ...
- 【菜鸟学Python】案例一:汇率换算
汇率换算V1.0 案例描述: 设计一个汇率换算器程序,其功能是将外币换算成人民币,或者相反 案例分析: 分析问题:分析问题的计算部分: 确定问题:将问题划分为输入.处理及输出部分: 设计算法:计算部分 ...
- 使用TCP通信文件上传
客服端读取本地文件,吧文件上传到服务器,服务器在吧上传的文件保存到服务器硬盘上方法分析1:客户端使用本地字节输入流读取要上传的文件 2:客户端使用网络字节输出流,吧读取到的文件上传到服务器 3:服务器 ...
- Linux系统下的网络配置
一.修改配置文件,重启后设置不丢失 [Red Hat Linux/CentOS] 使用ifconfig查看使用的网口: [root@localhost /]# ifconfig 修改对应网口配置文件: ...