import java.sql.*; class Conn { public static void main (String[] args) throws Exception { Class.forName ("oracle.jdbc.OracleDriver"); Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@localhost:1521:ORCL","scott","tiger"); try { Statement stmt = conn.createStatement(); try { ResultSet rset = stmt.executeQuery("select BANNER from SYS.V_$VERSION"); try { while (rset.next()) System.out.println (rset.getString(1)); // Print col 1 } finally { try { rset.close(); } catch (Exception ignore) {} } } finally { try { stmt.close(); } catch (Exception ignore) {} } } finally { try { conn.close(); } catch (Exception ignore) {} } } }
ps: Change host, port, SID, user and password to your value in connection url
("jdbc:oracle:thin:@localhost:1521:ORCL","scott","tiger");
Compile java
javac -cp .:$ORACLE_HOME/jdbc/lib/ojdbc5.jar:$ORACLE_HOME/jlib/orai18n.jar Conn.java
Run program
java -cp .:$ORACLE_HOME/jdbc/lib/ojdbc5.jar:$ORACLE_HOME/jlib/orai18n.jar Conn
PS : for compile and run in windows os, change columns(:) to semicolons(;) like
.;$ORACLE_HOME/jdbc/lib/ojdbc5.jar;$ORACLE_HOME/jlib/orai18n.jar
also change jar names in their directories the correct ones.
simple java tricks
Hiç yorum yok:
Yorum Gönder