mySQL 命令行不会完全更新数据库(my SQL command line d

我在 java 程序中使用的数据库更新方法不会更新数据库。只有当我在同一个类中运行 read 方法时,它才会显示数据库的更新版本,但实际上并没有更新数据库。我所做的是在我的主类中运行 (2) 方法,然后它不会更新数据库(它应该将 ID=1 的行的状态列更改为 2,但它在数据库中)。然后我用读表的方法,显示更新的数字,但数据库实际上并没有更新。

package com.company;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.*;
public class Stdn implements AutoCloseable{
    private Connection connection;
    private PreparedStatement preparedStatement;
    private ResultSet resultSet;
    public Stdn() throws SQLException {
        connection = DriverManager
                .getConnection("jdbc:mysql://localhost:1522/juni_project" , "root", "password");
        connection.setAutoCommit(false);
    }
    public void showName(String A , String B) throws SQLException{
        preparedStatement = connection.prepareStatement("select "+ A + " from " + B + ";" );
        resultSet = preparedStatement.executeQuery();
        while (resultSet.next()){
            System.out.println(resultSet.getString(1));
        }
    }
    public void update(String A , String B , int C , int D)  {
        try{
            PreparedStatement preparedStatement = connection.prepareStatement("UPDATE "+ A + " SET " + B + " =? WHERE ID = ? ;");
            preparedStatement.setInt(1,C);
            preparedStatement.setInt(2, D);
            preparedStatement.executeUpdate();
        }
        catch (SQLException e){
            System.out.println( " Could not update data to the database " + e.getMessage());
        }}
        public void unit_elimination(int X) throws SQLException {
        update("main_table" ,"status", X , 1 );
        }
    @Override
    public void close() throws Exception {
        preparedStatement.close();
        connection.close();
    }}

你能帮我找出问题所在吗?我被这个问题困扰了几天,几乎没有空闲时间。

© 版权声明
THE END
喜欢就支持一下吧
点赞96赞赏 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容