菜鸟学习Oracle数据库,使用Java代码链接数据库。
首先要配置Eclipse,在新建的工程中,Package Explorer->工程名->Build path->Add external archives->Oracle安装盘X:\app\admin\product\11.2.0\dbhome_1\jdbc\lib\ojdbc*.jar
ojdbc的jar包的选择参照readme文件。
For all platforms: [ORACLE_HOME]/jdbc/lib contains: - ojdbc5.jar - ojdbc5_g.jar - ojdbc5dms.jar - ojdbc5dms_g.jar - ojdbc6.jar - ojdbc6_g.jar - ojdbc6dms.jar - ojdbc6dms_g.jar Note: The dms versions of the jar files are the same as [ORACLE_HOME]/jdbc/doc/javadoc.tar contains the JDBC Javadoc [ORACLE_HOME]/jdbc/demo/demo.tar contains sample JDBC programs. [ORACLE_HOME]/jlib/orai18n.jar For the Windows platform: [ORACLE_HOME]\bin directory contains ocijdbc11.dll and For non-Windows platforms: [ORACLE_HOME]/lib directory contains libocijdbc11.so, |
Java代码如下:
package Connection.Oracle; import java.sql.*; public class java_ConnectOracle_jdb { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub try { // 加载驱动 Class.forName("oracle.jdbc.driver.OracleDriver"); // 得到连接 Connection ct = DriverManager.getConnection( "jdbc:oracle:thin:@127.0.0.1:1521:orcl", "scott", "123456"); // Connection // ct=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:实例名(SID)", // "用户名", "密码"); Statement sm = ct.createStatement(); ResultSet rs = sm.executeQuery("select * from salgrade"); System.out.println(" Grade-Losal-Hisal"); while (rs.next()) { System.out.println("Salgrade: " + rs.getString(1) + " - " + rs.getString(2) + " - " + rs.getString(3)); } rs.close(); sm.close(); ct.close(); } catch (Exception e) { e.printStackTrace(); } } }
效果图如下:
JDBC的使用:
Some Useful Hints In Using the JDBC Drivers ------------------------------------------- Please refer to "JDBC Developer‘s Guide and Reference" for details regarding usage of Oracle‘s JDBC Drivers. This section only offers useful hints. These hints are not meant to be exhaustive. These are a few simple things that you should do in your JDBC program: 1. Import the necessary JDBC classes in your programs that use JDBC. For example: import java.sql.*; import java.math.*; // if needed To use OracleDataSource, you need to do: import oracle.jdbc.pool.OracleDataSource; 2. Create an OracleDataSource instance. OracleDataSource ods = new OracleDataSource(); 3. set the desired properties if you don‘t want to use the default properties. Different connection URLs should be used for different JDBC drivers. ods.setUser("my_user"); ods.setPassword("my_password"); For the JDBC OCI Driver: To make a bequeath connection, set URL as: ods.setURL("jdbc:oracle:oci:@"); To make a remote connection, set URL as: ods.setURL("jdbc:oracle:oci:@<database>"); where <database> is either a TNSEntryName or a SQL*net name-value pair defined in tnsnames.ora. For the JDBC Thin Driver, or Server-side Thin Driver: ods.setURL("jdbc:oracle:thin:@<database>"); where <database> is either a string of the form //<host>:<port>/<service_name>, or a SQL*net name-value pair, or a TNSEntryName. For the JDBC Server-side Internal Driver: ods.setURL("jdbc:oracle:kprb:"); Note that the trailing ‘:‘ is necessary. When you use the Server-side Internal Driver, you always connect to the database you are executing in. You can also do this: Connection conn = new oracle.jdbc.OracleDriver().defaultConnection(); 4. Open a connection to the database with getConnection() methods defined in OracleDataSource class. Connection conn = ods.getConnection(); -----------------------------------------------------------------------