package com.company.jdbc;
import java.sql.*;
public class JdbcDemo01 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1.注册驱动,第一种方式通过new对象会对OracleDriver直接产生依赖,如果程序中没有引入相应的包,编译期会报错
//DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Class.forName("oracle.jdbc.driver.OracleDriver");//加载驱动
//2.获取连接
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:studydb","username","tiger");
//3.获取操作数据库的预处理对象
PreparedStatement pstm = conn.prepareStatement("select * from accout");
//4.执行sql,得到结果集
ResultSet rs = pstm.executeQuery();
//5.遍历结果集
while(rs.next()){
System.out.println(rs.getString("name"));
}
//6.释放资源,后打开的先关闭
rs.close();
pstm.close();
conn.close();
}
}
jdbc连接oracle数据库
于 2022-11-22 21:25:15 首次发布