jdbc调试sql语句方法
时间:2021-07-01 10:21:17
帮助过:25人阅读
package com.ayang.jdbc;
2
3 import java.sql.Connection;
4 import java.sql.DriverManager;
5 import java.sql.SQLException;
6 import java.sql.Statement;
7
8 public class TestDML2 {
9
10
11 public static void main(String[] args) {
12 if(args.length!=3
){
13 //判断输入参数个数是否错误
14 System.out.println("Parameter Error! Please Input Again!"
);
15 System.exit(-1);
//系统退出
16 }
17
18 int deptno = 0;
//声明变量。在头上呢?还是啥时候用啥时候声明呢?面试时,声明在头上。
19
20 try{
21 deptno = Integer.parseInt(args[0
]);
22 }
catch(NumberFormatException e){
23 System.out.println("参数类型错误,请输入数字"
);
24 System.exit(-1
);
25
26 }
27 String dname = args[1
];
28 String loc = args[2
];
29
30 Connection conn =
null;
31 Statement stmt =
null;
32
33 try{
34 //1、注册驱动
35 //new oracle.jdbc.driver.OracleDriver();
36 Class.forName("oracle.jdbc.driver.OracleDriver"
);
37 //2、建立连接
38 conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL","scott", "root"
);
39 //3、创建语句
40 stmt =
conn.createStatement();
41 String sql = "insert into dept2 values("+deptno+",‘"+dname+"‘,‘"+loc+"‘)"
;
42 System.out.println(sql);
//打印出sql语句用来调试sql
43 stmt.executeUpdate(sql);
44
45 }
catch (ClassNotFoundException e) {
46 System.out.println("未正常加载jdbc驱动"
);
47 e.printStackTrace();
48 }
catch(SQLException e){
49 e.printStackTrace();
//log for java
50
51 }
finally{
52 //6、释放资源
53 try {
54 if(stmt !=
null){
55 stmt.close();
56 stmt =
null;
57 }
if(conn !=
null){
58 conn.close();
59 conn =
null;
60 }
61 }
catch (SQLException e) {
62 e.printStackTrace();
63 }
64
65
66 }
67
68
69 }
70
71 }
jdbc调试sql语句方法
标签: