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

相关文章:

  • 2022-12-23
  • 2021-07-31
  • 2022-01-05
  • 2022-12-23
  • 2022-12-23
  • 2022-01-17
  • 2021-06-15
  • 2021-07-12
猜你喜欢
  • 2022-03-02
  • 2022-12-23
  • 2022-12-23
  • 2022-01-02
  • 2021-06-10
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案