时间:2021-07-01 10:21:17 帮助过:3人阅读
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBC {
public static void main(String[] args) {
//1.导入jar包 项目名---->buid path----->Add E~
//2.加载驱动
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
//3.创建链接对象
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL","xkq","123456");
//4.创建执行对象
PreparedStatement pst =con.prepareStatement("select * from stuInfo");
//5.执行sql语句
ResultSet rs=pst.executeQuery();
//6.获取结果集合
while(rs.next()){
System.out.println(rs.getInt(1));
System.out.println(rs.getString(2));
System.out.println(rs.getInt(3));
System.out.println(rs.getString(4));
System.out.println(rs.getString(5));
System.out.println(rs.getString(6));
System.out.println(rs.getString(7));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
简单的JDBC连接oracle数据库例子
标签: