解决报错问题:java.sql.SQLException: Could not retrieve transation read-only status server

一、报错显示

 二、问题源码

package cn.tedu.test;

import java.sql.*;
import java.util.Scanner;

public class Test3 {
    public static void main(String[] args) {
        method();
    }

    private static void method() {
        Connection c = null;
        PreparedStatement p = null;
        try {
            c = getConnection();
            System.out.print("输入要删除的id:");
            int id = new Scanner(System.in).nextInt();
            String str = "delete from user where id=?";
            p = c.prepareStatement(str);
            p.setInt(1,id);
            //执行增删改的SQL--executeUpdate()
            p.executeUpdate();
            System.out.println("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("删除失败");
        }finally {
            close(p,c);
        }
    }

    public static Connection getConnection() throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        Connection c = DriverManager.getConnection(
                "jdbc:mysql:///day0831?characterEncoding=utf8",
                "root",
                "123");
        return c;
    }

    public static void close(PreparedStatement p,Connection c) {
        try {
            p.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        try {
            c.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}



三、报错分析

驱动不匹配,数据库版本6.0,jar包版本5.1.32,无法使用executeUpate()方法

四、问题解决

jar包改为5.1.38版本

 在url后添加&useSSL=false

 再测试代码即可以使用executeUpate()方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值