1、引入jar包:
2、DEMO:
package jdbc;import java.sql.*;public class OracleConnectionExample {public static void main(String[] args) throws SQLException {Connection conn = null;PreparedStatement statement = null;try {// Register JDBC driverClass.forName("oracle.jdbc.driver.OracleDriver");// Open a connectionSystem.out.println("Connecting to database...");conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:xe", "username", "password");System.out.println("Connected!");// Do something with the Database hereString sql = "select * from aa_student";statement = conn.prepareStatement(sql);ResultSet resultSet = statement.executeQuery();while (resultSet.next()) {System.out.println("id:" + resultSet.getLong("id"));}statement.close();// Close the connectionconn.close();} catch (SQLException se) {// Handle errors for JDBCse.printStackTrace();} catch (Exception e) {// Handle errors for Class.forNamee.printStackTrace();} finally {// finally block used to close resourcestry {if (statement != null)statement.close();if (conn != null)conn.close();} catch (SQLException se) {se.printStackTrace();} // end finally try} // end trySystem.out.println("Goodbye!");}// end main
}// end OracleConnectionExample