JDBC之Oracle访问

  1 package com.ding;
  2 
  3 import java.sql.Connection;
  4 import java.sql.DriverManager;
  5 import java.sql.ResultSet;
  6 import java.sql.SQLException;
  7 import java.sql.Statement;
  8 
  9 /** 
 10  * <p>Title: JDBCDemoOracle</p>  
 11  * <p>Description: </p>  
 12  * @author: 丁帅帅
 13  * @date: 2021-4-4 10:54:27
 14  * @version v1.0 
 15  */
 16 public class JDBCDemoOracle {
 17     
 18     private static final String URL="jdbc:oracle:thin:@localhost:1521:ORCL";
 19     private static final String USERNAME="scott";
 20     private static final String PWD="tiger";
 21     
 22     /**
 23      * 
 24      * <p>Title: update</p>  
 25      * <p>Description:增删改</p>
 26      */
 27     public static void update() {
 28         Statement stmt =null;
 29         Connection  connection=null;
 30         
 31         try {
 32             //a.导入驱动,加载具体的驱动类
 33             Class.forName("oracle.jdbc.OracleDriver");//加载具体的驱动类
 34             //b.与数据库建立连接
 35             connection=DriverManager.getConnection(URL, USERNAME, PWD);
 36             //c.发送sql,执行增删改查
 37             stmt=connection.createStatement();
 38             String sql="insert into student values(9,'大傻',18,'男','东京',66,76)";
 39             //String sql="delete from student where id=9";
 40             //String sql="update student set name='小傻' where id=9";
 41             //执行sql
 42             int count=stmt.executeUpdate(sql);// 返回值表示 增删改 几条数据
 43             //d.处理结果
 44             if(count>0) {
 45                 System.out.println(count);
 46                 System.out.println("操作成功");
 47             }
 48             
 49         } catch (ClassNotFoundException e) {
 50             // TODO Auto-generated catch block
 51             e.printStackTrace();
 52         }//
 53         catch (SQLException e) {
 54             // TODO Auto-generated catch block
 55             e.printStackTrace();
 56         }
 57         finally {
 58             try {
 59                 if(stmt!=null)
 60                   stmt.close();
 61                 if(connection!=null)
 62                     connection.close();
 63             } catch (SQLException e) {
 64                 // TODO Auto-generated catch block
 65                 e.printStackTrace();
 66             }
 67         }
 68         
 69     }
 70     
 71     /**
 72      * <p>Title: query</p>  
 73      * <p>Description: 查</p>
 74      */
 75     public static void query() {
 76         Statement stmt =null;
 77         Connection  connection=null;
 78         ResultSet rs=null;
 79         try {
 80             //a.导入驱动,加载具体的驱动类
 81             Class.forName("oracle.jdbc.OracleDriver");//加载具体的驱动类
 82             //b.与数据库建立连接
 83             connection=DriverManager.getConnection(URL, USERNAME, PWD);
 84             //c.发送sql,执行增删改查
 85             stmt=connection.createStatement();
 86             String sql="select id,name from student";
 87             //执行sql
 88             rs=stmt.executeQuery(sql);
 89             //d.处理结果
 90             while(rs.next()) {
 91                 int id=rs.getInt("id");
 92                 String name=rs.getString("name");
 93                 System.out.println(id+"--"+name);
 94             }
 95             
 96         } catch (ClassNotFoundException e) {
 97             // TODO Auto-generated catch block
 98             e.printStackTrace();
 99         }//
100         catch (SQLException e) {
101             // TODO Auto-generated catch block
102             e.printStackTrace();
103         }
104         finally {
105             try {
106                 if(rs!=null)
107                     rs.close();
108                 if(stmt!=null)
109                   stmt.close();
110                 if(connection!=null)
111                     connection.close();
112             } catch (SQLException e) {
113                 // TODO Auto-generated catch block
114                 e.printStackTrace();
115             }
116         }
117         
118     }
119     public static void main(String[] args) {
120         //update() ;
121         query();
122     }
123 }

 

上一篇:JDBC之Mysql访问


下一篇:PHP简单连接操作数据库