• java 错误: 未报告的异常错误Exception; 必须对其进行捕获或声明以便抛出


     1 import java.io.FileInputStream;
     2 import java.util.Properties;
     3 import java.sql.Connection;
     4 import java.sql.DriverManager;
     5 import java.sql.Statement;
     6 
     7 public class ExecuteDML{
     8     private String driver;
     9     private String url;
    10     private String user;
    11     private String pass;
    12     
    13     public void initParam(String paramFile) throws Exception{
    14         Properties props = new Properties();
    15         props.load(new FileInputStream(paramFile));
    16         driver = props.getProperty("driver");
    17         url = props.getProperty("url");
    18         user = props.getProperty("user");
    19         pass = props.getProperty("pass");
    20     }
    21     
    22     public int insertData(String sql) throws Exception{
    23         //加载驱动
    24         Class.forName(driver);
    25         try(
    26             //获取数据库连接
    27             Connection conn = DriverManager.getConnection(url, user, pass);
    28             //使用Connection来创建一个Statement对象
    29             Statement stmt = conn.createStatement()){
    30                 //执行SQL语句,返回受影响的记录条数
    31                 return stmt.executeUpdate(sql);
    32             }
    33     }
    34     
    35     public static void main(String[] args) {
    36         ExecuteDML ed = new ExecuteDML();
    37         ed.initParam("mysql.ini");
    38         int result = ed.insertData("insert into jdbc_test(jdbc_name,jdbc_desc)"
    39             + "select s.student_name , t.teacher_name "
    40             + "from student_table s , teacher_table t "
    41             + "where s.java_teacher = t.teacher_id;");
    42         System.out.println("------系统中一共有" + result + "条记录受影响------");
    43     }
    44 }
    View Code

    出错是因为,对initParam()方法和insertData()方法抛出的异常没有进行处理,解决办法:

      1.main()方法中在try...catch块中调用initParam()和insertData()方法

      2.main()方法throws抛出异常。

     1 import java.io.FileInputStream;
     2 import java.util.Properties;
     3 import java.sql.Connection;
     4 import java.sql.DriverManager;
     5 import java.sql.Statement;
     6 
     7 public class ExecuteDML{
     8     private String driver;
     9     private String url;
    10     private String user;
    11     private String pass;
    12     
    13     public void initParam(String paramFile) throws Exception{
    14         Properties props = new Properties();
    15         props.load(new FileInputStream(paramFile));
    16         driver = props.getProperty("driver");
    17         url = props.getProperty("url");
    18         user = props.getProperty("user");
    19         pass = props.getProperty("pass");
    20     }
    21     
    22     public int insertData(String sql) throws Exception{
    23         //加载驱动
    24         Class.forName(driver);
    25         try(
    26             //获取数据库连接
    27             Connection conn = DriverManager.getConnection(url, user, pass);
    28             //使用Connection来创建一个Statement对象
    29             Statement stmt = conn.createStatement()){
    30                 //执行SQL语句,返回受影响的记录条数
    31                 return stmt.executeUpdate(sql);
    32             }
    33     }
    34     
    35     public static void main(String[] args) throws Exception{
    36         ExecuteDML ed = new ExecuteDML();
    37         ed.initParam("mysql.ini");
    38         int result = ed.insertData("insert into jdbc_test(jdbc_name,jdbc_desc)"
    39             + "select s.student_name , t.teacher_name "
    40             + "from student_table s , teacher_table t "
    41             + "where s.java_teacher = t.teacher_id;");
    42         System.out.println("------系统中一共有" + result + "条记录受影响------");
    43     }
    44 }
    View Code
  • 相关阅读:
    sklearn—支持向量机
    sklearn—LinearRegression,Ridge,RidgeCV,Lasso线性回归模型简单使用
    技术文档翻译-------glove readme(1)
    python jieba分词(结巴分词)、提取词,加载词,修改词频,定义词库 -转载
    Word2Vec模型参数 详解
    aws ec2 硬盘扩容
    记一个python requests模块请求的报错
    ubuntu安装mysql, 修改初始密码一条龙服务
    mysql 创建用户, 分配权限, 删除用户
    sqlite删除数据
  • 原文地址:https://www.cnblogs.com/lanshanxiao/p/7375873.html
Copyright © 2020-2023  润新知