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 TestDML { 9 10 public static void main(String[] args) { 11 Connection conn = null; 12 Statement stmt = null; 13 14 try{ 15 //1、注册驱动 16 //new oracle.jdbc.driver.OracleDriver(); 17 Class.forName("oracle.jdbc.driver.OracleDriver"); 18 //2、建立连接 19 conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL","scott", "root"); 20 //3、创建语句 21 stmt = conn.createStatement(); 22 String sql = "insert into dept2 values(80,'GAME','BJ')"; 23 stmt.executeUpdate(sql); 24 }catch (ClassNotFoundException e) { 25 System.out.println("未正常加载jdbc驱动"); 26 e.printStackTrace(); 27 }catch(SQLException e){ 28 e.printStackTrace(); //log for java 29 30 }finally{ 31 //6、释放资源 32 try { 33 if(stmt != null){ 34 stmt.close(); 35 stmt = null; 36 }if(conn != null){ 37 conn.close(); 38 conn = null; 39 } 40 } catch (SQLException e) { 41 e.printStackTrace(); 42 } 43 44 45 } 46 } 47 48 49 }
oracle-jdbc 执行DML语句,不需要 第四步:执行语句 5:处理结果了。表是oracle的dept表。