package com.db; import java.sql.connection; import java.sql.drivermanager; import java.sql.PreparedStatement; import Java.sql.resultset; public class DBConnection { // Connect to Oracle database public void oracleconnection () { Connection con = Null; preparedstatement pre = null; ResultSet rs = null; try { // 1. Load Oracle Drivers class.forname ("Oracle.jdbc.driver.OracleDriver"); // 2. set up Oracle database basic information String url = "JDBC:ORACLE:THIN:@127.0.0.1:1521:ORCL"; String user = "Scott"; &nbsP; string password = "Goodluck"; // 2. Get Connections con = Drivermanager.getconnection (Url, user, password); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("----> Connection success! "); // 3. Execute SQL statements String sql = "select * from emp"; Pre = con.preparestatement (SQL); // 4. Get result sets rs = pre.executequery (); while (Rs.next ()) { system.out.println ("No.:" + rs.getstring ("Empno") + "; Name:" + rs.getstring ("ename") + "; work:" + Rs.getstring ("Job") + "; leader:" + rs.getstring ("Mgr") + "; Employment Date: " + rs.getstring (" HireDate ") + "; Salary:" + rs.getstring ("Sal") + "; Bonus: " + rs.getstring (" comm ") + "; Department:" + rs.getstring ("Deptno")); } } catch ( Exception e) { E.printstacktrace (); } finally { try { if (rs != null) rs.close (); if (pre != null) pre.close (); if (con != null) Con.close (); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("----> Connection End <-----"); } catch (exception e) { e.printstacktrace (); } } } public static Void main (String[] args) { dbconnection db = new dbconnection (); db. OracleConnection (); } }
Java Connection to Oracle database