• jdbc调试sql语句方法


    在main命令行输入三个参数到oracle 的 dept2表(自己建的 和dept一样(deptno,dname,loc)),插入到数据库中去。通过本例子,学习在java里调试sql的方法。

    写完sql语句后,在下边把它打印出来,有错误时,把这句sql语句粘贴到sqlplus里去,会详细显示哪个位置出错了。因为myeclipse里是不会提示具体的错误位置的,如果sql语句比较长,错误难以被发现。

    要注意的是sql的insert into 语句里的细节,如字符串的 'dname','loc'的单引号等。

    变量的声明问题:一般要求声明在头上,那是为了在用该变量时容易找到它的声明之处。现在myeclipse能够提示了,有人认为什么时候用什么时候声明变量。但面试时,还是声明在头上的好。

     1 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 }

    欢迎关注个人公众号一起交流学习:

  • 相关阅读:
    hdu 3006 The Number of set(思维+壮压DP)
    Mysql-SQL优化-统计某种类型的个数
    canvas.clipPath canvas.clipRect() 无效的原因
    linux下alias命令具体解释
    使用带粒子效果的 CAEmitterLayer
    Wordpress 建站(一)
    一个有趣的问题:ls -l显示的内容中total究竟是什么?
    (转)奇妙的数据挖掘
    android几个高速打包命令
    hdu3336解读KMP算法的next数组
  • 原文地址:https://www.cnblogs.com/lihaoyang/p/4473354.html
Copyright © 2020-2023  润新知