7、java jdbc如何连接oracle12

1、导入依赖

<dependency>
     <groupId>com.oracle.database.jdbc</groupId>
     <artifactId>ojdbc8</artifactId>
     <version>12.2.0.1</version>
</dependency>

2、创建jdbc连接实现查询

public class Test01 {
    public static void main(String[] args) throws Exception {
        //加载数据库驱动
        Class.forName("oracle.jdbc.driver.OracleDriver");
        //得到Connection连接
        Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:ORCL",
                "c##boat", "c##boat");
        //得到预编译的Statement对象
        PreparedStatement pstm = connection.prepareStatement("select * from student where id = ?");
        //给参数赋值
        pstm.setObject(1, 1);
        //执行数据库查询操作
        ResultSet rs = pstm.executeQuery();
        //输出结果
        while(rs.next()){
            System.out.println(rs.getString("name"));
        }
        //释放资源
        rs.close();
        pstm.close();
        connection.close();
    }
}

 

上一篇:python 对 mysql 操作


下一篇:三十四、java基于jdbc操作MySQL数据库之查询方法封装