zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    基于.NET平台常用的框架整理
    简单的linq语法
    Newtonsoft.Json高级用法
    C# 语言历史版本特性(C# 1.0到C# 7.1汇总更新)
    HTML URL 编码
    sql(SqlServer)编程基本语法
    正则表达式
    添加vs模板注释
    js实现无刷新表单提交文件,将ajax请求转换为form请求方法
    HTML5 手机端动态适配
  • 原文地址:https://www.cnblogs.com/lanshanxiao/p/7375873.html
Copyright © 2011-2022 走看看