Connect to mysql in idea and insert data successfully, but refresh the table in navicat and there is no data?

Table of contents

1. There is a problem:

Second, try to solve:

3. Found the problem:

Fourth, the solution:


1. There is a problem:

        When writing a large database job, after connecting to mysql in the idea, test the dao method of insert, there is no error in the console, and the display question adds data successfully. However, there is no data when refreshing the table in navicat. (This program is an ordinary one. Three-tier architecture, hand rubbing JDBC)

 

Second, try to solve:

        1. Modify the jdbc.properties configuration file.

        2. Modify the database configuration.

        3. Check the code everywhere.

        So after breaking my head all night, I used my classmate's code the next day and found that it can be connected normally, so after replacing the codes one by one, the same jdbc.properties, the same database table... finally found out that it is a util package The problem with the code below.

3. Found the problem:

        1. Since the code written when learning JavaWeb in Shang Silicon Valley is directly transferred, in the JdbcUtils class, the database connection method of Filter processing transaction is used, because the commit and rollback of the transaction originally used in the filter are not available. The problem arises when using filters.

        2.

connection.close();//Close the automatic commit of the transaction.

         In the transaction management, the line of code "close the automatic commit of the transaction" of the transaction will make the obtained database connection no longer automatically commit the transaction.


    /**
     * 提交事务,并关闭释放连接.
     */
    public static void commitAndClose() {
        Connection connection = conns.get();
        if (connection != null) {
            try {
                connection.commit();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    connection.close();//关闭事务的自动提交.
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        conns.remove();
    }

        JdbcUtils source code:

package com.my.util;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author sihua
 * @create 2022-10-30-20:51
 */
public class JdbcUtilsFilter {

    private static DruidDataSource dataSource;
    private static ThreadLocal<Connection> conns = new ThreadLocal<>();

    static {
        try {
            Properties properties = new Properties();
            //读取jdbc.properties属性配置文件.
            InputStream inputStream = JdbcUtilsFilter.class.getClassLoader().
                    getResourceAsStream("jdbc.properties");
            //从流中加载数据
            properties.load(inputStream);
            //创建数据库连接池.
            dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);

//            System.out.println(dataSource.getConnection());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取数据库连接池中的连接.
     * 如果返回null,说明获取连接失败.
     */
    public static Connection getConnection() {
        Connection conn = conns.get();
        if (conn == null) {
            try {
                conn = dataSource.getConnection();
                conns.set(conn);
                conn.setAutoCommit(false);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return conn;
    }

    /**
     * 提交事务,并关闭释放连接.
     */
    public static void commitAndClose() {
        Connection connection = conns.get();
        if (connection != null) {
            try {
                connection.commit();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    connection.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        conns.remove();
    }


    /**
     * 关闭连接,放回数据库连接池
     */
//    public static void close(Connection conn) {
//        if (conn != null) {
//            try {
//                conn.close();
//            } catch (SQLException e) {
//                e.printStackTrace();
//            }
//        }
//    }

    /**
     * 回滚事务,并关闭释放连接.
     */
    public static void rollBackAndClose() {
        Connection connection = conns.get();
        if (connection != null) {
            try {
                connection.rollback();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    connection.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        conns.remove();
    }

}

Fourth, the solution:

        Remove the filter get connection method.

package com.my.util;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class JdbcUtils {

    private static DruidDataSource dataSource;

    static {
        try {
            Properties properties = new Properties();
            // 读取 jdbc.properties属性配置文件
            InputStream inputStream = JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
            // 从流中加载数据
            properties.load(inputStream);
            // 创建 数据库连接 池
            dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }



    /**
     * 获取数据库连接池中的连接
     * @return 如果返回null,说明获取连接失败<br/>有值就是获取连接成功
     */
    public static Connection getConnection(){

        Connection conn = null;

        try {
            conn = dataSource.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return conn;
    }

    /**
     * 关闭连接,放回数据库连接池
     * @param conn
     */
    public static void close(Connection conn){
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}

(I would like to use this blog to give me a long memory.)

Guess you like

Origin blog.csdn.net/SIHUAZERO/article/details/131013182