"Cicada Hall Study notes" Eclipse,myeclipse connecting MySQL database and Oracle database

Source: Internet
Author: User

I. Connecting to the MySQL database

1. Since eclipse,myeclipse does not have a rack to connect MySQL data, we need to download the MySQL Connection Rack pack mysql-connector (official link: http://dev.mysql.com/downloads/ connector/j/5.0.html), the download version is best updated.

2. After downloading, copy to any location in your project, then right-click Rack Package Select Build path, add to Build path, then click on referenced in Project Libraries Libraries if a "bottle" appears Mysql-connector ... , indicating that the import was successful, we can begin to write a method to connect to the database.

  

3. After the import is successful, our first step is to: load and register the driver. Class.forName ("Com.mysql.jdbc.Driver");

4. Get Database Link: drivermanager.getconnection (url, user, pwd); URL is the address of your MySQL database, the local database is generally: "jdbc:mysql://localhost:3306/database Name", USER,PWD is your MySQL database user name and password.

Specific code:

 Public classJdbcutils_mysql {Private StaticString user = "root";//your MySQL user name    Private StaticString pwd = "123456";//Password    Private StaticString url = "Jdbc:mysql://localhost:3306/ofo";//your database address Ofo is the database name    /** Load database-driven static to let the database load only once*/    Static {        Try{class.forname ("Com.mysql.jdbc.Driver"); } Catch(ClassNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }    /** Get a database link*/     Public StaticConnection getconnection () {Try{Connection con=drivermanager.getconnection (URL, user, pwd); returncon; } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        return NULL; }}

Method class for connecting to a database well, let's just use it for a little bit.

  

 Public classMysqltest { Public Static voidMain (string[] args) {//TODO auto-generated Method StubConnection conn = Jdbcutils_mysql.getconnection ();//call the connection method to get a database linkString sql= "Select username from user where uid= ' 1 '";//the SQL statement to execute        Try{PreparedStatement PS=conn.preparestatement (SQL); ResultSet RS=Ps.executequery (); if(Rs.next ()) {System.out.println ("User name:" +rs.getstring (1)); }        } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }}

I'm here: querying the user name with ID 1 in the user table, and I'm not going to map it.

Two. Linking Oracle databases

1.Oracle database is not dedicated to download the driver package, the installation of the Oracle directory has a driver package (Ojdbc5.jar), the Oracle driver package in the installation directory: oracle\product\11.2.0\dbhome_1\jdbc\ LIB (My Oracle version is 11g), the package name is: Ojdbc5.jar, we go directly to the introduction of the line. Copy to your project, operate and import the MySQL driver package,

The steps to connect to the Oracle database are similar to that of MySQL, I directly post code:

 Public classjdbcutils_oracle {Private StaticString user = "CJL";//Oracle User name    Private StaticString pwd = "123456";//User Password    Private StaticString url = "Jdbc:oracle:thin: @localhost: 1521:ORCL";//localhost is because the database is local, 1521 is the port number    /** Load database-driven static to let the database load only once*/    Static {        Try{class.forname ("Oracle.jdbc.driver.OracleDriver"); } Catch(ClassNotFoundException e) {e.printstacktrace (); }    }    /**get a database connection*/     Public StaticConnection getconnection () {Try{Connection conn=drivermanager.getconnection (URL, user, pwd); returnConn; } Catch(SQLException e) {e.printstacktrace (); }        return NULL; }}

So we do a test.

 Public classOracletest { Public Static voidMain (string[] args) {//TODO auto-generated Method StubConnection conn = Jdbcutils_oracle.getconnection ();//methods like MySQL, just use different method classesString sql= "Select username from user where uid= ' 1 '";//the SQL statement to execute        Try{PreparedStatement PS=conn.preparestatement (SQL); ResultSet RS=Ps.executequery (); if(Rs.next ()) {System.out.println ("User name:" +rs.getstring (1)); }        } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }}

Welcome to the Cicada community--a fun and interesting learning community: http://www.zhiliaotang.com

"Cicada Hall Study notes" Eclipse,myeclipse connecting MySQL database and Oracle database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.