java异常报错总结

1. 数组越界异常

具体报错

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 23 out of bounds for length 23 	at com.company.Test1$Grant.run(Test1.java:52) 	at com.company.Test1.main(Test1.java:65)

报错原因

是因为数组中的下标过界,超出范围,所以报此错误

  for (int i = 0; i<=arr_grant.length; i++) {
  }

解决方法

public static class Grant {
        public void run() throws SQLException, IOException {
            Connection conn = HostMulti.startThreadConn1();
            Statement st = conn.createStatement();
            BufferedReader retable = null;
            retable = new BufferedReader(new FileReader("/server/scipts/modify_data.sql"));
            // Read one line at a time
            String[] arr_grant = {"update", "insert", "select"};
            List<String> result = new ArrayList<>();
            String lineContent = null;
            while ((lineContent=retable.readLine()) != null) {
//                System.out.println(retable.readLine());
                result.add(lineContent);
                result.toArray();
            }
            for (int i = 0; i<arr_grant.length; i++) {
                for (int j = 0; j < result.toArray().length; j++) {
                    //System.out.println(result[j]);
                    System.out.println("grant "+arr_grant[i] +"(" + result.toArray()[j] +") on test.table_1_utf8_4 to 'user1'@'%'");
                }
                return;
            }
        }
    }

2. jdbc连接mysql驱动错误

com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

解决方法:

//驱动改为
com.mysql.cj.jdbc.Driver

3. java.sql.SQLNonTransientConnectionException: Cannot load connection class because of underlying

解决方法:

//在连接mysql的jdbc中加入
bc:mysql://localhost:3306/tree?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值