1.输入命令行参数;
2.使用DML语句将命令行参数写入数据库。
实例:
TestDML2.java
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- public class TestDML2 {
- public static void main(String[] args) {
- Connection conn = null;
- Statement stmt = null;
- String sql = null;
- int id = 0;
- String name = null;
- if (args.length != 2) {
- System.exit(-1);
- } else {
- id = Integer.parseInt(args[0]);
- name = args[1];
- }
- try {
- Class.forName("oracle.jdbc.driver.OracleDriver");
- conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:mgc", "system", "admin");
- stmt = conn.createStatement();
- sql = "Insert INTO member(id,name) VALUES(" + id + ",'" + name + "')";
- 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();
- }
- }
- }
- }