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
  • 相关阅读:
    表单提交时,更新的操作
    提交后刷新本页面与移除本页面的JS写法
    jquery的$.each如何退出循环和退出本次循环
    修改本机host文件,使upf报表操作变的快
    代码记录
    JQuery简介
    Ubuntu安装
    PHP笔记(PHP高级篇)
    将Session写入Memcache
    将Session写入数据库
  • 原文地址:https://www.cnblogs.com/lanshanxiao/p/7375873.html
Copyright © 2011-2022 走看看