import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; Public class OracleConnectionTest {private String URL =" JDBC: Oracle :thin:@127.0.0.1:1521:YOOTK "; private String dbUsername="SCOTT "; private String dbPassword="tiger "; Conn =null; conn=null; conn=null; private Statement statement=null; private ResultSet rs=null; private String sql=" "; public void selectTest(){ try { Class.forName("oracle.jdbc.OracleDriver "); conn=DriverManager.getConnection(url, dbUsername, dbPassword); sql="select * from emp "; statement=conn.createStatement(); rs=statement.executeQuery(sql); while(rs.next()){ System.out.println(rs.getString(1)); } } catch (Exception e) { e.printStackTrace(); }finally { try { if(rs==null) { rs.close(); } if(statement==null) { statement.close(); } if(conn==null) { conn.close(); } } catch (Exception e2) { // TODO: handle exception e2.printStackTrace(); } } } public static void main(String[] args) { new OracleConnectionTest().selectTest(); }}Copy the code
Run successfully, the console outputs all values of the first column of the EMP table of the database.