oracle常用语句,以及java连接oracle的写法

解锁用户名:如(scott)
    alter user 用户名 account unlock identified by 密码
修改用户密码:
    alter user 用户名 identified by 密码
新建tablespace:
    create tablespace practice
    datafile 'F:/oracle/liuyong.dbf'
    size 10M
    autoextend on next 32M maxsize unlimitied // 文件可自动扩展,每次自动扩展32M
    extent management local // 表空间中的盘区采用本地化管理方式
    segment space management auto // 表空间中段的管理方式为自动化管理方式
新建用户:
    create user liuyong
    identified by liuyong // 密码
    default tablespace practice /// 默认的表空间
给用户赋权限
    grant connect ,resource to liuyong // 把connect,resource角色授予用户liuyong 
撤销权限
    revoke connect from liuyong

方法一:(需将驱动.jar包拷贝到项目目录下)
    Class.forName("oracle.jdbc.driver.OracleDriver"); // 加载驱动
    Connection conn = DriverManaer.getConnection( "jdbc:oracle:thin:@localhost:1521:oracle" );
方法二:(连接池)
    1、修改服务器的配置文件context.xml(同样需要将驱动包拷贝到服务器的lib文件夹下)
        <Resource name="jndi/yituan" auth="Container"
                    type="javax.sql.DataSource" username="liuyong" password="liuyong"
                    url="jdbc:oracle:thin:@localhost:1521:oracle"
                    driverClassName="oracle.jdbc.driver.OracleDriver"
                    maxActive="100" maxIdle="10"
                    maxWait="1000"
        />
    2.代码部分
        eg:
        public BaseDao{
                 Context ctxt = null;
                DataSource ds = null;
                Connection conn = null;
                
                public BaseDao(){
                    try{
                        ctxt = new InitialContext();
                        ds = (DataSource) ctxt.lookup("java:comp/env/jndi/yituan");
                    }  catch (Exception e){
                            e.printStackTrace();
                    }
                }
            // 获取连接
            public Connection getConn(){
                try{
                    conn = ds.getConnection();
                } catch (Exception e){
                            e.printStackTrace();
                }
            }
            // 释放连接
            public void closeConn(Connection conn , PreparedStatement ptmt , ResultSet rs ){
                try{
                    if(rs != null){
                        rs.close();
                    }
                        ptmt.close();
                        conn.close();
                 }
            }

        }
               
                

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值