Java使用jdbc连接实现对MySQL增删改查操作的全过程
目录
- 1.新建项目
- 2.添加jar包
- 3.jdbc的连接
- 4.简单的MySQL增删改查操作
- 总结
1.新建项目
新建一个项目,fileànewàproject如下图:

选择Javaà下一步,如下图:(注意如果jdk推荐使用jdk1.8版本哦,如果不是可以在project SDK中更换,Add JDK,找到自己电脑上放JDK1.8的地方,没有的话自行下载哦)


继续下一步

创建项目名字(自己起就行,注意项目名不要大写),找一个存放的地址,也自己决定就行。

2.添加jar包
一般默认位置是在如下位置:C:\Program Files (x86)\MySQL\Connector J 8.0

Fileàproject Structureàmodulesàdepencenlesà加号添加jar包



找到c盘下C:\Program Files (x86)\MySQL\Connector J 8.0的位置

选中后OK。完成


添加jar包成功
3.jdbc的连接

再在包中新建一个Java文件如下

实现JDBC连接然后我的代码和结果截图如下:
| package com.wang.dao; |
| |
| import java.sql.*; |
| |
| public class Test1Demo { |
| public static void main(String[]args){ |
| String url="jdbc:mysql://localhost:3306/ishop?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8"; |
| String name="root"; |
| String password="root"; |
| String sql="SELECT *from tbl_commoditytype"; |
| |
| try { |
| Class.forName("com.mysql.cj.jdbc.Driver"); |
| try { |
| |
| Connection connection= DriverManager.getConnection(url,name,password); |
| |
| Statement statement = connection.createStatement(); |
| |
| ResultSet resultSet = statement.executeQuery(sql); |
| |
| while (resultSet.next()){ |
| |
| System.out.println(resultSet.getInt(1)+"\t"+resultSet.getString(2)); |
| } |
| |
| resultSet.close(); |
| statement.close(); |
| connection.close(); |
| } catch (SQLException e) { |
| e.printStackTrace(); |
| } |
| } catch (ClassNotFoundException e) { |
| e.printStackTrace(); |
| } |
| } |
| } |

要注意以下位置数据库和SQL语句是否存在自己的MySQL中,以及是否匹配。还有用户名和密码是否是自己的。


4.简单的MySQL增删改查操作
实现MySQL增删改操作如下:(仔细看注释掉的东西,这三个操作是换了在注释里的部分代码运行了三次啊)
| package com.wang.demo; |
| |
| import java.sql.Connection; |
| import java.sql.DriverManager; |
| import java.sql.SQLException; |
| import java.sql.Statement; |
| |
| public class Test01 { |
| |
| public static void main(String[] args) { |
| |
| |
| try { |
| Class.forName("com.mysql.cj.jdbc.Driver"); |
| |
| } catch (ClassNotFoundException e) { |
| |
| e.printStackTrace(); |
| |
| } |
| |
| |
| String url="jdbc:mysql://localhost:3306/ishop?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8"; |
| String user="root"; |
| String password="root"; |
| |
| Connection connection = null; |
| try { |
| connection = DriverManager.getConnection(url, user, password); |
| |
| } catch (SQLException e) { |
| e.printStackTrace(); |
| } |
| |
| |
| Statement statement = null; |
| try { |
| statement = connection.createStatement(); |
| |
| } catch (SQLException e) { |
| e.printStackTrace(); |
| } |
| |
| |
| |
| |
| String sql="delete from tbl_commoditytype where id=6"; |
| int i = 0; |
| |
| |
| |
| |
| try { |
| i = statement.executeUpdate(sql); |
| |
| } catch (SQLException e) { |
| e.printStackTrace(); |
| } |
| |
| System.out.println(i); |
| |
| |
| |
| |
| |
| try { |
| statement.close(); |
| |
| } catch (SQLException e) { |
| e.printStackTrace(); |
| } |
| |
| try { |
| connection.close(); |
| |
| } catch (SQLException e) { |
| |
| e.printStackTrace(); |
| |
| } |
| } |
| } |



实现简单的查询操作
先把增删改操作的代码都复制粘贴过来(程序员怎么能不会复制粘贴嘿嘿),再把增删改变成查询语句String sql=”select*from tbl_commoditytype”;对应的executeUpdate();换为了executQuery()。具体如下
| package com.wang.demo; |
| |
| import java.sql.*; |
| |
| |
| |
| |
| |
| public class Test02 { |
| public static void main(String[] args) { |
| |
| try { |
| Class.forName("com.mysql.cj.jdbc.Driver"); |
| } catch (ClassNotFoundException e) { |
| e.printStackTrace(); |
| } |
| |
| String url="jdbc:mysql://localhost:3306/ishop?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8"; |
| String user="root"; |
| String password="root"; |
| |
| Connection connection = null; |
| try { |
| connection = DriverManager.getConnection(url, user, password); |
| } catch (SQLException e) { |
| e.printStackTrace(); |
| } |
| |
| Statement statement = null; |
| try { |
| statement = connection.createStatement(); |
| } catch (SQLException e) { |
| e.printStackTrace(); |
| } |
| |
| |
| |
| String sql="select *from tbl_commoditytype"; |
| ResultSet resultSet=null; |
| |
| |
| try { |
| resultSet = statement.executeQuery(sql); |
| |
| while(resultSet.next()){ |
| String o= resultSet.getInt(1)+"\t"+resultSet.getString(2); |
| |
| System.out.println(o); |
| } |
| } catch (SQLException e) { |
| e.printStackTrace(); |
| } |
| |
| |
| |
| |
| |
| try { |
| statement.close(); |
| } catch (SQLException e) { |
| e.printStackTrace(); |
| } |
| try { |
| connection.close(); |
| } catch (SQLException e) { |
| e.printStackTrace(); |
| } |
| } |
| } |
