Used to learn. NET connection to a database, it also makes a variety of connection configurations. Now, while using Java to manipulate Oracle, there are also some operations. Here's a summary.
1.JDBC_ODBC Connection Database
This method requires an ODBC data source to be configured, and the native must have an Oracle database
1. Configure the data source
Open Control Panel--management tools--data source
2. Next connect in the code
To connect using JDBC_ODBC bridging, you need to configure the data source public static void main (String [] args) {try{//1. Load driver Class.forName (" Sun.jdbc.odbc.JdbcOdbcDriver ")//2. Gets the connection, where Testdemo is the name of the configured data source connection ct=drivermanager.getconnection (" Jdbc:o Dbc:testdemo "," Scott "," Tiger ");//start with SQL Server statement sm=ct.createstatement (); ResultSet rs=sm.executequery ("SELECT * from T_user"), while (Rs.next ()) {//output user name System.out.println (rs.getstring (2));}} catch (Exception e) {e.printstacktrace ();}}
2. Pure JDBC Driver Connection Oracle
This approach requires the loading of Oracle's jar packages without the need to configure a data source, which can be remotely connected without an Oracle database. The code is as follows:
Use JDBC to connect to the database public static void main (String [] args) {try{//1. Load drive Class.forName ("Oracle.jdbc.driver.OracleDriver"); /2. Get the connection, where Testdemo is the name of the configured data source, the ORCL above is the instance of the database connection ct=drivermanager.getconnection ("jdbc:oracle:thin:@ 192.168.24.202:1521:orcl "," Scott "," Tiger ");//start with SQL Server statement sm=ct.createstatement (); ResultSet rs=sm.executequery ("SELECT * from T_user"), while (Rs.next ()) {//output user name System.out.println (rs.getstring (2));}} catch (Exception e) {e.printstacktrace ();}}
Java two ways to connect to Oracle