How to connect ecplise and myecplise to the oracle database
Ecplise connect to database 1. Import jar package
For the oracle version of GB, find the corresponding jar in the following directory and import it to the project runtime environment.
Ojdbc6.jar under app \ oracle \ product \ 11.2.0 \ dbhome_1 \ jdbc \ lib
App \ oracle \ product \ 11.2.0 \ client_1 \ oui \ jlib \ classes12.jar
2. Create a jdbc class and perform the following operations:
Package com_it_JDBC; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. SQLException; import java. SQL. statement; import java. util. concurrent. countDownLatch; public class JdbcDemo {public static void main (String [] args) {// defines the oracle database Driver class String driverclass = "oracle. jdbc. oracleDriver "; // define the Url String url to connect to oracle =" jdbc: oracle: thin: @ 192.168.0.1: 1521/orcl "; // login username String usern Ame = "admin"; // password String password = "root"; Connection conn = null; Statement stmt = null; ResultSet rs = null; string querysql = "select * from person"; try {// register the database driver and load the Class into the memory Class. forName (driverclass); // obtain the database connection conn = DriverManager. getConnection (url, username, password); // create an SQL Execution object stmt = conn. createStatement (); // execute the SQL statement and return the result stmt.exe cuteQuery (querysql); while (rs. next () {String na Me = rs. getString ("name"); String age = rs. getString ("age"); String adress = rs. getString ("adress"); String phone = rs. getString ("phone"); System. out. println ("name" + name + "age" + age + "Address" + adress + "phone" + phone);} catch (Exception e) {e. printStackTrace ();} finally {try {if (stmt! = Null) {stmt. close ();} if (conn! = Null) {conn. close () ;}} catch (SQLException e) {e. printStackTrace ();}}}}
3. jdbc configuration of myecplise
As shown above;
The difference is that the URL format is different.
''
String url = "jdbc: oracle: thin :@ localhost: 1521: orcl (sid );
''
4. several Common Errors: 1. IO exception: Connection refused (DESCRIPTION = (TMP =) (VSNNUM = 186647552) (ERR = 12505) (ERROR_STACK = (ERROR = (CODE = 12505) (EMFI = 4 ))))
Solution: run the following statement in the database to be connected:
Select INSTANCE_NAME from v $ instance; change sid to the running result.
2. Io exception: Invalid connection string format, a valid format is: "host: port: sid"
URL format problem: myecplise is in the following format:
Jdbc: oracle: thin: @ localhost: 1521: orcl