Java uses jdbc connection to realize the operation of adding, deleting, modifying and checking MySQL

 

content

foreword

1. New project

2. Add jar package

3.jdbc connection

4. Simple MySQL CRUD operations


foreword

Hello everyone, I am the color of ice three points. Personal homepage: blog of ice three colors

The Idea demo used in this article mainly talks about how to use the idea to complete the jdbc connection, and Java implements the simple addition, deletion, modification and query operation of MySQL.

Friends passing by, please like and follow before walking. Welcome to the comment area to communicate. It is never too late to start working hard, so it is better to start with this article!

Let's grow together! refill

d43bb622c0504ef88fe8a3c597263ceb.png


 

1. New project

Create a new project, fileànewàproject as shown below:

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAaWNl5LiJ5YiG6aKc6Imy,size_20,color_FFFFFF,t_70,g_se,x_16

Select Java→Next, as shown in the figure below: (Note that if jdk recommends using jdk1.8 version, if not, you can replace it in the project SDK, Add JDK, find the place where you put JDK1.8 on your computer, if you don’t have it, download it yourself)

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAaWNl5LiJ5YiG6aKc6Imy,size_20,color_FFFFFF,t_70,g_se,x_16

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAaWNl5LiJ5YiG6aKc6Imy,size_20,color_FFFFFF,t_70,g_se,x_16

continue to the next step

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAaWNl5LiJ5YiG6aKc6Imy,size_20,color_FFFFFF,t_70,g_se,x_16

Create a project name (you can start it yourself, pay attention not to capitalize the project name), find a storage address, and decide by yourself.

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAaWNl5LiJ5YiG6aKc6Imy,size_20,color_FFFFFF,t_70,g_se,x_16

2. Add jar package

The general default location is the following location: C:\Program Files (x86)\MySQL\Connector J 8.0

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAaWNl5LiJ5YiG6aKc6Imy,size_20,color_FFFFFF,t_70,g_se,x_16

Fileàproject Structureàmodulesàdepencenlesàplus add jar package

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAaWNl5LiJ5YiG6aKc6Imy,size_10,color_FFFFFF,t_70,g_se,x_16

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAaWNl5LiJ5YiG6aKc6Imy,size_20,color_FFFFFF,t_70,g_se,x_16

2a9f4cbb77b94d7bbb4dd7245ee23f1c.png

Find the location of C:\Program Files (x86)\MySQL\Connector J 8.0 under the c drive

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAaWNl5LiJ5YiG6aKc6Imy,size_11,color_FFFFFF,t_70,g_se,x_16

OK after selection. Finish

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAaWNl5LiJ5YiG6aKc6Imy,size_20,color_FFFFFF,t_70,g_se,x_16

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAaWNl5LiJ5YiG6aKc6Imy,size_8,color_FFFFFF,t_70,g_se,x_16

Add jar package successfully

3.jdbc connection

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAaWNl5LiJ5YiG6aKc6Imy,size_20,color_FFFFFF,t_70,g_se,x_16

Then create a new Java file in the package as follows

09c967d20cf44b72b2dc36af30b5ecf4.png

Implement JDBC connection then my code and result screenshot are as follows:

package com.wang.dao;

import java.sql.*;
//用Java实现MySQL的增删改查操作
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";//mysql8的连接字符串,多了时区比之前的5
        String name="root";
        String password="root";
        String sql="SELECT *from tbl_commoditytype";
        //1.加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");//在有错误提示的时候光标移到错误处,alt+enter,
            try {
                //2.创建连接
                Connection  connection= DriverManager.getConnection(url,name,password);
                //3.创建命令窗口
                Statement statement = connection.createStatement();
                //4.执行命令窗口里的语句
                ResultSet resultSet = statement.executeQuery(sql);
                //5.处理返回的结果集
                while (resultSet.next()){
                    //打印行的每一列
                  System.out.println(resultSet.getInt(1)+"\t"+resultSet.getString(2));
                }
                //6.关闭资源
                resultSet.close();
                statement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAaWNl5LiJ5YiG6aKc6Imy,size_13,color_FFFFFF,t_70,g_se,x_16

Pay attention to whether the database and SQL statements in the following locations exist in your own MySQL, and whether they match. Also whether the username and password are your own.

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAaWNl5LiJ5YiG6aKc6Imy,size_20,color_FFFFFF,t_70,g_se,x_16

 

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAaWNl5LiJ5YiG6aKc6Imy,size_17,color_FFFFFF,t_70,g_se,x_16

4. Simple MySQL CRUD operations

The operation of adding, deleting, and modifying MySQL is as follows: (Look carefully at the commented out things, these three operations are run three times by replacing some of the code in the comments)

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) {//psvm回车可以直接敲出来哦

        //1.加载驱动

        try {

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

        } catch (ClassNotFoundException e) {

            e.printStackTrace();

        }

        //2.获取链接,驱动管理器

        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();

        }

        //3.获取命令

        Statement statement = null;

        try {

            statement = connection.createStatement();

        } catch (SQLException e) {

            e.printStackTrace();

        }

       // String sql="insert into tbl_commoditytype (id,name) values (6,'AA')"; //这个地方我后来发现我的数据库表中id忘记设置自增长,就直接在这里直接写上了id的值。之后增删改操作依次执行可查看表得三个结果图

        //String sql="update tbl_commoditytype set name ='bb' where id=6";

        String sql="delete from tbl_commoditytype where id=6";

        int i = 0;

        //executeUpdate是做增删改的

        //4.得到结果集并处理 

        try {

            i = statement.executeUpdate(sql);

        } catch (SQLException e) {

            e.printStackTrace();

        }

        System.out.println(i);//sout回车可快速创建System.out.println()哦

       

        //5.关闭资源

        //用到的connection(连接),statement(命令窗口),两个接口,resultSet一个实现类(结果集)



        try {

            statement.close();

        } catch (SQLException e) {

            e.printStackTrace();

        }

        try {

            connection.close();

        } catch (SQLException e) {

            e.printStackTrace();

        }

    }



}

aa838171f1da42ebb2e6a8a6d0e2badc.png7c453aaf70314ebe8deb41df1fd62322.png61f427436acd4a568d166723a6e5ac90.png 

Implement simple query operations

First copy and paste the code of the addition, deletion and modification operation (how can the programmer not copy and paste hehe), and then change the addition and deletion into a query statement String sql=”select*from tbl_commoditytype”; the corresponding executeUpdate(); is replaced by executeQuery( ). details as follows

package com.wang.demo;

import java.sql.*;

/**
 * 斜杠双星回车即可得这种注释
 * 使用Statement进行查询操作
 */
public class Test02 {
    public static void main(String[] args) {
        //1.加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //2.获取链接,驱动管理器
        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();
        }
        //3.获取命令
        Statement statement = null;
        try {
            statement = connection.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        // String sql="insert into tbl_commoditytype (id,name) values (6,'AA')";
        //String sql="update tbl_commoditytype set name ='bb' where id=6";
        //String sql="delete from tbl_commoditytype where id=6";
        String sql="select *from tbl_commoditytype";
        ResultSet resultSet=null;
        //executeUpdate是做增删改的
        // 4.得到结果集并处理
        try {
            resultSet = statement.executeQuery(sql);
            //处理结果集,两种方法,if(一条记录)/while(不确定或者多条数据)
            while(resultSet.next()){
               String o= resultSet.getInt(1)+"\t"+resultSet.getString(2);
                //因为我的表第一列是int,第二列是string。也可以把2换成name,也就是把索引(columnindex)换成列名(columnlabel}
                System.out.println(o);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //sout回车可快速创建System.out.println()哦

        //5.关闭资源
        //用到的connection(连接),statement(命令窗口),两个接口,resultSet一个实现类(结果集)

        try {
            statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAaWNl5LiJ5YiG6aKc6Imy,size_13,color_FFFFFF,t_70,g_se,x_16

 

 

 

Guess you like

Origin blog.csdn.net/qq_46007633/article/details/124156099