1.加载驱动程序
Class.forName("oracle.jdbc.driver.OracleDriver");
Class.forName("oracle.jdbc.driver.OracleDriver");
2. 建立连接
Connection conn = DriverMananger.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:xe","用户名","密码");
3. 执行查询
4. 关闭数据库连接
==============实际的代码=================
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCExample {
public static void main(String[] args) {
? try {
?? Class.forName("oracle.jdbc.driver.OracleDriver");
?? String url="jdbc:oracle:thin:@127.0.0.1:1521:ora9";
?? try {
??? Connection conn=DriverManager.getConnection(url,"scott","tiger");
??? Statement stmt=conn.createStatement();
??? ResultSet rs=stmt.executeQuery("select * from dept");
??? while(rs.next()){
???? System.out.println("deptno"+rs.getInt(1));
???? System.out.println("\tDname: "+rs.getString(2));
???? System.out.println("\tLoc: "+rs.getString(3));
??? }
??? rs.close();
??? stmt.close();
??? conn.close();
?? } catch (SQLException e) {
??? // TODO Auto-generated catch block
??? e.printStackTrace();
?? }
? } catch (ClassNotFoundException e) {
?? // TODO Auto-generated catch block
?? e.printStackTrace();
? }
}
}
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCExample {
public static void main(String[] args) {
? try {
?? Class.forName("oracle.jdbc.driver.OracleDriver");
?? String url="jdbc:oracle:thin:@127.0.0.1:1521:ora9";
?? try {
??? Connection conn=DriverManager.getConnection(url,"scott","tiger");
??? Statement stmt=conn.createStatement();
??? ResultSet rs=stmt.executeQuery("select * from dept");
??? while(rs.next()){
???? System.out.println("deptno"+rs.getInt(1));
???? System.out.println("\tDname: "+rs.getString(2));
???? System.out.println("\tLoc: "+rs.getString(3));
??? }
??? rs.close();
??? stmt.close();
??? conn.close();
?? } catch (SQLException e) {
??? // TODO Auto-generated catch block
??? e.printStackTrace();
?? }
? } catch (ClassNotFoundException e) {
?? // TODO Auto-generated catch block
?? e.printStackTrace();
? }
}
}
thin VS oci
接下来再找找oci和thin的其他区别,发现有如下解释:
Oracle provides four different types of JDBC drivers, for use in different deployment scenarios. The 10.1.0 drivers can access Oracle 8.1.7 and higher. While all Oracle JDBC drivers are similar, some features apply only to JDBC OCI drivers and some apply only to the JDBC Thin driver.
JDBC OCI client-side driver: This is a JDBC Type 2 driver that uses Java native methods to call entrypoints in an underlying C library. That C library, called OCI (Oracle Call Interface), interacts with an Oracle database. The JDBC OCI driver requires an Oracle client installation of the same version as the driver.
The use of native methods makes the JDBC OCI driver platform specific. Oracle supports Solaris, Windows, and many other platforms. This means that the Oracle JDBC OCI driver is not appropriate for Java applets, because it depends on a C library.
Starting from 10.1.0, the JDBC OCI driver is available for install with the OCI Instant Client feature, which does not require a complete Oracle client-installation. Please refer to Oracle Call Interface for more information.
JDBC Thin client-side driver: This is a JDBC Type 4 driver that uses Java to connect directly to Oracle. It implements Oracle's SQL*Net Net8 and TTC adapters using its own TCP/IP based Java socket implementation. The JDBC Thin driver does not require Oracle client software to be installed, but does require the server to be configured with a TCP/IP listener.
Because it is written entirely in Java, this driver is platform-independent. The JDBC Thin driver can be downloaded into any browser as part of a Java application. (Note that if running in a client browser, that browser must allow the applet to open a Java socket connection back to the server.)
JDBC Thin server-side driver: This is another JDBC T
引用
Oracle provides four different types of JDBC drivers, for use in different deployment scenarios. The 10.1.0 drivers can access Oracle 8.1.7 and higher. While all Oracle JDBC drivers are similar, some features apply only to JDBC OCI drivers and some apply only to the JDBC Thin driver.
JDBC OCI client-side driver: This is a JDBC Type 2 driver that uses Java native methods to call entrypoints in an underlying C library. That C library, called OCI (Oracle Call Interface), interacts with an Oracle database. The JDBC OCI driver requires an Oracle client installation of the same version as the driver.
The use of native methods makes the JDBC OCI driver platform specific. Oracle supports Solaris, Windows, and many other platforms. This means that the Oracle JDBC OCI driver is not appropriate for Java applets, because it depends on a C library.
Starting from 10.1.0, the JDBC OCI driver is available for install with the OCI Instant Client feature, which does not require a complete Oracle client-installation. Please refer to Oracle Call Interface for more information.
JDBC Thin client-side driver: This is a JDBC Type 4 driver that uses Java to connect directly to Oracle. It implements Oracle's SQL*Net Net8 and TTC adapters using its own TCP/IP based Java socket implementation. The JDBC Thin driver does not require Oracle client software to be installed, but does require the server to be configured with a TCP/IP listener.
Because it is written entirely in Java, this driver is platform-independent. The JDBC Thin driver can be downloaded into any browser as part of a Java application. (Note that if running in a client browser, that browser must allow the applet to open a Java socket connection back to the server.)
JDBC Thin server-side driver: This is another JDBC T