/**
*Title:TestDML
*Description:
*@Copyright:
*@Company:
*@autor:firefly
*@version:1.0
*@time:2012.10.30
*/
import java.sql.*;
public class TestDML {
static Statement stmt = 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");
stmt = conn.createStatement();
String sql = "insert into dept2 values("+deptno + ", '" + dname + "', '" + loc + "')";
//构造sql语句这里写的比较麻烦,比较简单的写法,可以参考TestDML2.java.
System.out.println(sql);
stmt.executeUpdate(sql);
} catch(ClassNotFoundException e) {
e.printStackTrace();
} catch(SQLException e) {
e.printStackTrace();
} finally {
try {
if(stmt != null) {
stmt.close();
stmt = null;
}
if(conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}