jdbc连接mysql并执行简单的CRUD的步骤:

1.注册驱动(需要抛出/捕获异常)

  1. Class.forName("com.mysql.jdbc.Driver");

2.建立连接需要抛出/捕获异常)

  1. connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/testdb", "root", "Danny2036");

3.创建statement

  1. statement = connection.createStatement();

4.创建sql语句

  1. String sql = "insert into user values(10, \"noname\", 31)";

5.执行sql语句,如果是查询需要用ResultSet接收结果集

  1. int affectrows = statement.executeUpdate(sql);
  2. System.out.println(affectrows);

6.关闭资源

  1. if (statement != null) {
  2. statement.close();
  3. }
  4. if (connection != null) {
  5. connection.close();
  6. }

————————————————————————分割线——————————————————————————————————

最后贴出全部代码(包括insert,selectOne和selectAll)

  1. package com.colin.dao;
  2.  
  3. import java.sql.*;
  4.  
  5. public class JdbcTest {
  6.  
  7. /**
  8. * 插入语句
  9. * @param id 主键id int
  10. * @param name 姓名 String
  11. * @param age 年龄 int
  12. */
  13. public static void update(int id, String name, int age) throws SQLException {
  14. long starttime = System.currentTimeMillis();
  15.  
  16. Connection connection = null;
  17. Statement statement = null;
  18. try {
  19. Class.forName("com.mysql.jdbc.Driver");
  20. connection = DriverManager.getConnection(
  21. "jdbc:mysql://localhost:3306/testdb",
  22. "root", "Danny2036");
  23. statement = connection.createStatement();
  24. String sql = "update user set name = \'" + name + "\', age = "+ age +" where id = " + id;
  25. System.out.println(sql);
  26. int affectrows = statement.executeUpdate(sql);
  27. System.out.println(affectrows);
  28.  
  29. } catch (ClassNotFoundException e) {
  30. e.printStackTrace();
  31. } catch (SQLException e) {
  32. e.printStackTrace();
  33. } finally {
  34. if (connection != null) {
  35. connection.close();
  36. }
  37. if (statement != null) {
  38. statement.close();
  39. }
  40. }
  41.  
  42. System.out.println(System.currentTimeMillis() - starttime);
  43.  
  44. }
  45.  
  46. public static void selectOne(int id) throws ClassNotFoundException, SQLException {
  47.  
  48. Statement statement = null;
  49. Connection connection = null;
  50.  
  51. try {
  52. // 注册驱动
  53. Class.forName("com.mysql.jdbc.Driver");
  54. // 建立连接
  55. connection = DriverManager.getConnection(
  56. "jdbc:mysql://localhost:3306/testdb",
  57. "root", "Danny2036"
  58. );
  59. // 创建statement
  60. statement = connection.createStatement();
  61. // 定义sql语句
  62. String sql = "SELECT id, name, age FROM user where id = " + id;
  63. // 执行sql语句并获得结果
  64. ResultSet resultSet = statement.executeQuery(sql);
  65. // 遍历结果集
  66. while (resultSet.next()) {
  67. int idResult = (int) resultSet.getInt("id");
  68. String nameResult = (String) resultSet.getString("name");
  69. int ageResult = (int) resultSet.getInt("age");
  70.  
  71. System.out.println(idResult + "\t" + nameResult + "\t" + ageResult);
  72. }
  73. } finally {
  74. if (statement != null) {
  75. statement.close();
  76. }
  77. if (connection != null) {
  78. connection.close();
  79. }
  80. }
  81.  
  82. }
  83.  
  84. public static void selectAll() throws ClassNotFoundException, SQLException {
  85.  
  86. Statement statement = null;
  87. Connection connection = null;
  88.  
  89. try {
  90. // 注册驱动
  91. Class.forName("com.mysql.jdbc.Driver");
  92. // 建立连接
  93. connection = DriverManager.getConnection(
  94. "jdbc:mysql://localhost:3306/testdb",
  95. "root", "Danny2036"
  96. );
  97. // 创建statement
  98. statement = connection.createStatement();
  99. // 定义sql语句
  100. String sql = "SELECT id, name, age FROM user";
  101. // 执行sql语句并获得结果
  102. ResultSet resultSet = statement.executeQuery(sql);
  103. // 遍历结果集
  104. while (resultSet.next()) {
  105. int idResult = (int) resultSet.getInt("id");
  106. String nameResult = (String) resultSet.getString("name");
  107. int ageResult = (int) resultSet.getInt("age");
  108.  
  109. System.out.println(idResult + "\t" + nameResult + "\t" + ageResult);
  110. }
  111. } finally {
  112. if (statement != null) {
  113. statement.close();
  114. }
  115. if (connection != null) {
  116. connection.close();
  117. }
  118. }
  119.  
  120. }
  121.  
  122. public static void main(String[] args) throws SQLException, ClassNotFoundException {
  123. selectAll();
  124. }
  125.  
  126. /*
  127.  
  128. // 向上抛异常的方式
  129. public static void main(String[] args)
  130. throws ClassNotFoundException, SQLException {
  131.  
  132. long starttime = System.currentTimeMillis();
  133.  
  134. Connection connection = null;
  135. Statement statement = null;
  136.  
  137. try {
  138. //注册驱动
  139. Class.forName("com.mysql.jdbc.Driver");
  140. //建立连接
  141. connection = DriverManager.getConnection(
  142. "jdbc:mysql://localhost:3306/testdb",
  143. "root", "Danny2036");
  144. //创建statement
  145. statement = connection.createStatement();
  146. //定义sql
  147. String sql = "insert into user values(10, \"noname\", 31)";
  148. //执行sql,如果是查询需返回结果集
  149. int affectrows = statement.executeUpdate(sql);
  150. System.out.println(affectrows);
  151.  
  152. } finally {
  153.  
  154. //关闭资源
  155. if (statement != null) {
  156. statement.close();
  157. }
  158. if (connection != null) {
  159. connection.close();
  160. }
  161.  
  162. }
  163.  
  164. System.out.println("总执行时间: " + (System.currentTimeMillis() - starttime));
  165.  
  166. }
  167. */
  168.  
  169. /*
  170.  
  171. // 捕获异常的方式
  172. public static void main(String[] args) throws SQLException {
  173.  
  174. long starttime = System.currentTimeMillis();
  175.  
  176. Statement statement = null;
  177. Connection connection = null;
  178. try {
  179. // 注册驱动
  180. Class.forName("com.mysql.jdbc.Driver");
  181. // 建立连接
  182. connection = DriverManager.getConnection(
  183. "jdbc:mysql://localhost:3306/testdb?useSSL=true",
  184. "root", "Danny2036");
  185. // 创建statement
  186. statement = connection.createStatement();
  187. // 定义sql
  188. String sql = "insert into user values(9, \"ahaha\", 22)";
  189. // 执行sql,如果是查询,遍历结果集
  190. int affectrows = statement.executeUpdate(sql);
  191. System.out.println(affectrows);
  192.  
  193. } catch (ClassNotFoundException e) {
  194. e.printStackTrace();
  195. } catch (SQLException e) {
  196. e.printStackTrace();
  197. } finally {
  198.  
  199. // 关闭资源
  200. if (statement != null) {
  201. statement.close();
  202. }
  203. if (connection != null) {
  204. connection.close();
  205. }
  206.  
  207. }
  208.  
  209. System.out.println("总用时 : " + (System.currentTimeMillis() - starttime));
  210.  
  211. }
  212. */
  213.  
  214. }

——————————————————**********最后附上bean代码***********————————————————————

  1. package com.colin.bean;
  2.  
  3. public class User {
  4.  
  5. private int id;
  6. private String name;
  7. private int age;
  8.  
  9. public User(int id, String name, int age) {
  10. this.id = id;
  11. this.name = name;
  12. this.age = age;
  13. }
  14.  
  15. public User(String name, int age) {
  16. this.name = name;
  17. this.age = age;
  18. }
  19.  
  20. public int getId() {
  21. return id;
  22. }
  23.  
  24. public void setId(int id) {
  25. this.id = id;
  26. }
  27.  
  28. public String getName() {
  29. return name;
  30. }
  31.  
  32. public void setName(String name) {
  33. this.name = name;
  34. }
  35.  
  36. public int getAge() {
  37. return age;
  38. }
  39.  
  40. public void setAge(int age) {
  41. this.age = age;
  42. }
  43.  
  44. @Override
  45. public String toString() {
  46. return "User{" +
  47. "id=" + id +
  48. ", name='" + name + '\'' +
  49. ", age=" + age +
  50. '}';
  51. }
  52. }

jdbc笔记(一) 使用Statement对单表的CRUD操作的更多相关文章

  1. jdbc笔记(二) 使用PreparedStatement对单表的CRUD操作

    首先声明,本文只给出代码,并不是做教程用,如有不便之处,还请各位见谅. PreparedStatement相较于Statement,概括来说,共有三个优势: 1. 代码的可读性和易维护性:Prepar ...

  2. 【Java EE 学习 44】【Hibernate学习第一天】【Hibernate对单表的CRUD操作】

    一.Hibernate简介 1.hibernate是对jdbc的二次开发 2.jdbc没有缓存机制,但是hibernate有. 3.hibernate的有点和缺点 (1)优点:有缓存,而且是二级缓存: ...

  3. 6.单表的CRUD操作

    1.插入后用新id初始化被插入对象 <insert id="insertStudentCatchId"> insert into student (age,name,s ...

  4. SQLServer学习笔记<>.基础知识,一些基本命令,单表查询(null top用法,with ties附加属性,over开窗函数),排名函数

    Sqlserver基础知识 (1)创建数据库 创建数据库有两种方式,手动创建和编写sql脚本创建,在这里我采用脚本的方式创建一个名称为TSQLFundamentals2008的数据库.脚本如下:   ...

  5. Django学习笔记(10)——Book单表的增删改查页面

    一,项目题目:Book单表的增删改查页面 该项目主要练习使用Django开发一个Book单表的增删改查页面,通过这个项目巩固自己这段时间学习Django知识. 二,项目需求: 开发一个简单的Book增 ...

  6. MyBatis-Plus学习笔记(1):环境搭建以及基本的CRUD操作

    MyBatis-Plus是一个 MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变,使用MyBatis-Plus时,不会影响原来Mybatis方式的使用. SpringBoot+M ...

  7. Oracle触发器实现监控某表的CRUD操作

    前提:请用sys用户dba权限登录 1.创建一个表来存储操作日志 create table trig_sql( LT DATE not null primary key, SID NUMBER, SE ...

  8. 通过jdbc完成单表的curd操作以及对JDBCUtils的封装

    概述:jdbc是oracle公司制定的一套规范(一套接口),驱动是jdbc的实现类,由数据库厂商提供.所以我们可以通过一套规范实现对不同的数据库操作(多态) jdbc的作用:连接数据库,发送sql语句 ...

  9. JDBC简单增删改查实现(单表)

    0.准备工作 开发工具: MySQL数据库, intelliJ IDEA2017. 准备jar包: mysql-connector-java-5.1.28-bin.jar(其他均可) 1. 数据库数据 ...

随机推荐

  1. java学习(五)--- 方法

    方法的定义 修饰符 返回值类型 方法名(参数类型 参数名){ ... 方法体 ... return 返回值; } 注意:非void方法必须有返回值 方法重载: 可以声明方法相同,但是参数类型不同的方法 ...

  2. 大规模微服务架构下的Service Mesh探索之路

    小结: 1. 第一.二代Service Mesh meetup-slides/敖小剑-蚂蚁金服-大规模微服务架构下的Service Mesh探索之路.pdf https://github.com/se ...

  3. baiduMap & MapV 简单demo

    看到 MapV 的一个demo 的底图比较好看,练练手 MapV demos:https://mapv.baidu.com/examples/ 参考的demo:https://mapv.baidu.c ...

  4. C++/C代码审查注意事项(摘录,非原创)

    文件结构 头文件和定义文件的名称是否合理?头文件和定义文件的目录结构是否合理?版权和版本声明是否完整? 重要头文件是否使用了 ifndef/define/endif 预处理块?头文件中是否只存放“声明 ...

  5. docker-compose介绍

    docker-compose 常用命令 Commands: build Build or rebuild services bundle Generate a Docker bundle from t ...

  6. LigerUi之ligerMenu 右键菜单

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. [ROS]激光驱动安装

    参考资料: https://blog.csdn.net/hongliang2009/article/details/73302986 https://blog.csdn.net/bohaijun_12 ...

  8. 【菜鸟学Python】案例一:汇率换算

    汇率换算V1.0 案例描述: 设计一个汇率换算器程序,其功能是将外币换算成人民币,或者相反 案例分析: 分析问题:分析问题的计算部分: 确定问题:将问题划分为输入.处理及输出部分: 设计算法:计算部分 ...

  9. 使用TCP通信文件上传

    客服端读取本地文件,吧文件上传到服务器,服务器在吧上传的文件保存到服务器硬盘上方法分析1:客户端使用本地字节输入流读取要上传的文件 2:客户端使用网络字节输出流,吧读取到的文件上传到服务器 3:服务器 ...

  10. Linux系统下的网络配置

    一.修改配置文件,重启后设置不丢失 [Red Hat Linux/CentOS] 使用ifconfig查看使用的网口: [root@localhost /]# ifconfig 修改对应网口配置文件: ...