The following articles describe how to correctly connect to Oracle databases. If you are a beginner in Oracle database application, you can use the following articles to have a better understanding of the two basic methods for connecting to the Oracle database, which are described in detail in this article.
1. the following code uses the jdbc_odbc bridge to connect to Oracle:
- Public class Test2 {
- Public static void main (String [] args ){
- String driver = "sun. jdbc. odbc. JdbcOdbcDriver ";
- String url = "jdbc: odbc: test1 ";
- String uname = "scott ";
- String password = "tiger ";
- Connection conn = null;
- String SQL = "select * from emp ";
- Try {
- Class. forName (driver );
- Conn = DriverManager. getConnection (url, uname, password );
- Statement sm = conn. createStatement ();
- ResultSet rs = sm.exe cuteQuery (SQL );
- While (rs. next ()){
- String sname = rs. getString (2 );
- System. out. println ("***** name =" + sname );
- }
- } Catch (Exception e ){
- E. printStackTrace ();
- }
- }
- **************************************** ***********************************
2. The code for connecting to the Oracle database using jdbc is as follows:
- Public class Test3 {
- Public static void main (String [] args ){
- String driver = "Oracle. jdbc. driver. OracleDriver ";
- String url = "jdbc: Oracle: thin :@ localhost: 1521: orcl ";
- String uname = "scott ";
- String password = "tiger ";
- Connection conn = null;
- String SQL = "select * from emp ";
- Try {
- Class. forName (driver );
- Conn = DriverManager. getConnection (url, uname, password );
- PreparedStatement ptmt = conn. prepareStatement (SQL );
- ResultSet rs1_ptmt.exe cuteQuery ();
- While (rs. next ()){
- System. out. println ("$ name $ =" + rs. getString (2 ));
- }
- Conn. close ();
- } Catch (Exception e ){
- E. printStackTrace ();
- }
- }
- ========================================================== ========================
What is the difference between PreparedStatement and Statement? It is best to use PreparedStatement when understanding Oracle databases because PreparedStatement has the role of precompilation.