/** * 演示JDBC的操作步骤: * 1.加载驱动程序 * 2.创建连接 * 3.创建语句对象 * 4.发送SQL语句 * 5.如果发送select语句,需要处理结果集 * 6.关闭连接 * @author Cher_du * */ public class JdbcDemo01 { public static void main(String[] args) { // 需求:创建一个员工, // 员工号,员工名,工资,部门号 // 1. Connection conn = null; try { Class.forName("oracle.jdbc.driver.OracleDriver"); System.out.println("驱动加载成功!"); // 2. conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "learn", "learn"); // 3. // Statement 语句对象。发送并执行SQL语句。 /* * int executeUpdate(String sql); * 发送insert,update,delete语句 返回值int * 表示影响数据库表的行数 */ Statement stmt = conn.createStatement(); String sql = "insert into emp(empno,ename,sal,deptno)" + "values(1000,'鲁迅',1500,10)"; int count = stmt.executeUpdate(sql); if (count > 0) { System.out.println("保存成功!"); } System.out.println(conn.getClass().getName()); } catch (ClassNotFoundException e) { // 1.记录异常日志 // 2.上报:通知调用者 throw new RuntimeException("加载驱动错误!", e); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("创建连接失败!", e); } finally { // 关闭连接 if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }
执行程序后数据库表中数据变化
: