Java Connection to Oracle database
JDBC (Java Data Base Connectivity,java database connection), then how do we use Java to connect the database?
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. Importing Jar Packages Project names---->buid Path----->ADD E~//2. Load driver Try {class.forname ("Oracle.jdbc.driver.OracleDriver"); //3. Create a linked object connection con=drivermanager.getconnection (" Jdbc:oracle:thin: @localhost: 1521:orcl "," XKQ "," 123456 ")//4. Creating an Execution Object preparedstatement pst = Con.preparestatement ("Select * from stuinfo");//5. Execute SQL statement resultset rs= Pst.executequery ();//6. Get result Collection 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 ();}}
Simple JDBC Connection Oracle Database example