Oracle的简单查询跟修改

 ̄綄美尐妖づ 提交于 2020-03-09 04:39:08

maven依赖

       <dependency>
            <groupId>com.jslsolucoes</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.1.0</version>
        </dependency>
public class oracle {
    Connection connection=null;
    PreparedStatement preparedStatement=null;
    ResultSet resultSet=null;

    @Test
    public void jdbc(){
        try {
            //1.注册驱动
            Class.forName("oracle.jdbc.driver.OracleDriver");
            //2.获取数据库连接
            connection = DriverManager.getConnection(
                    "jdbc:oracle:thin:@//localhost:1521/ORCL", "scott", "123456"
            );
            //3.创建Statement
            String sql="select * from books";
            preparedStatement = connection.prepareStatement(sql);
            //4.执行查询语句
            resultSet=preparedStatement.executeQuery();
            //5.处理结果
            while (resultSet.next()){
                System.out.println(resultSet.getString("uname"));
            }
            //JDBC修改
            sql="update books set uname=? where uuid=1";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1,"Java2");
            int i = preparedStatement.executeUpdate();
            System.out.println("i="+i);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //6.关闭
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }finally {
                    try {
                        resultSet.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

结果:
Java
C++
Python
i=1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!