TestPrepStmt(24)

来源:互联网 发布:linux 总线驱动 编辑:程序博客网 时间:2024/05/21 08:59

/**
*Title:TestPreStmt
*Description:
*@Copyright:
*@Company:
*@autor:firefly
*@version:1.0
*@time:2012.10.30
*/

 

import java.sql.*;
public class TestPrepStmt {

 static PreparedStatement pstmt = null;
 static Connection conn = null;
  
 public static void main(String[] args) {
  if (args.length != 3) {
   System.out.println("Parameter Error! Please Input Again!");
   System.exit(-1);
  }
  
  int deptno = 0;
  
  try {
   deptno = Integer.parseInt(args[0]);
  } catch (NumberFormatException e) {
   System.out.println("Parameter Error! Deptno should be Number Formeter!");
   System.exit(-1);
  }
  
  String dname = args[1];
  String loc = args[2];
  try {
   Class.forName("oracle.jdbc.driver.OracleDriver");
   conn = DriverManager.getConnection("jdbc:oracle:thin:@122.207.171.222:1521:ORCL", "scott", "tiger");
   pstmt = conn.prepareStatement("insert into dept2 values(?, ?, ?)");//注意下面几行和TestDML.java中相关语句的区别。
   pstmt.setInt(1, deptno);
   pstmt.setString(2, dname);
   pstmt.setString(3, loc);
   pstmt.executeUpdate();
  } catch(ClassNotFoundException e) {
   e.printStackTrace();
  } catch(SQLException e) {
   e.printStackTrace();
  } finally {
   try {
    if(pstmt != null) {
     pstmt.close();
     pstmt = null;
    }
    if(conn != null) { 
     conn.close();
     conn = null;
    }
   } catch (SQLException e) {
    e.printStackTrace();
   }
  }
  
 }

}

原创粉丝点击