Java语言 链接Oracle数据库

时间:2021-03-04 21:05:45
 package com.tao.pojo;

 import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; public class TestOracle001 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("oracle.jdbc.OracleDriver");
// 10.96.113.212 是数据库所在的主机的IP地址 users也是该主机的表名(连谁的数据库就用谁数据库里面的表)
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@//10.96.113.212:1521/orcl", "scott", "tiger");
PreparedStatement ps = conn.prepareStatement("select * from users");
ResultSet rs = ps.executeQuery();
while(rs.next()) {
int id= rs.getInt(1);
String name = rs.getString(2);
System.out.println(id+","+name);
}
rs.close();
ps.close();
conn.close();
} }