8_12上午&下午复习

来源:互联网 发布:淘宝联盟提现要交税吗 编辑:程序博客网 时间:2024/04/28 06:53

Oracle命令行中的操作

解锁所有的用户密码èalter user scott account unlock

 

连接Oracle语句:

Sqlplus system/ora@orcl----------------系统账号

Sqlplus scott/ora@orcl

 

建表语句:

Create table student(

Id int primary key,

Name varchar(50),

Age int

);

 

查看表中所有数据:

Desc  student

 

插入行:

Insert into student (id,name,age)values(1,’uij’,22);

 

查询语句:

Select * from student;

 

 

 

Java中操作用Oracle:

1. 在选中的Java包中加入jar 文件:

Buid pathàconfigurebuild pathàlibrariesàadd extemal jarsàoracle安装路径àjdbcàclass12.jar

 

2. Java连接数据库

Connection con=null//--------连接

       PreparedStatementpstm=null;//------生成sql语句对象

 

      

可定义成全局变量

try {

           //1,注册驱动

           Class.forName("oracle.jdbc.driver.OracleDriver");

           //2,建立数据库连接

           //localhost连接oracle数据库的ip地址 orcl数据库名 scott 用户名  ora 密码

            con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","ora");

          

           //3,生成发送sql语句的对象

              //pst=con.preparestatement(sql);

 

           //添加数据---------------sql操作

            String sql="insert into student(id,name,age) values(?,?,?)";//一般不先赋值,在后来赋值

            pstm=con.prepareStatement(sql);

            //给?赋值

            pstm.setInt(1, 33);

            pstm.setString(2, "bbbb");

            pstm.setInt(3, 32);

          

            //删除----------------------sql操作

            String sql3="delete from student where id>?";

            pstm=con.prepareStatement(sql3);

            pstm.setInt(1, 10);

            

            //修改--------------------sql操作

            String sql2="update student set name=?,age=? where id>?";//要写?,不赋值

            pstm=con.prepareStatement(sql2);

            pstm.setString(1,"啊啊啊");

            pstm.setInt(2,9);

            pstm.setInt(3, 5);

            

           //4,执行sql语句

           int i=pstm.executeUpdate();//执行查询

          

           if(i>0){

              System.out.println("执行成功");

           }else{

              System.out.println("执行失败");

           }

          

           } catch (Exception e) {

           e.printStackTrace();//打印异常

       }finally{

           //5,,关闭资源

           try {

              if(pstm!=null) pstm.close();

              if(con!=null) con.close(); //关闭资源是有先后顺序的==>pstm,con,先建立的后写,后建立的先写

           } catch (SQLException e) {

              e.printStackTrace();//打印异常

           }

       }

 

0 0