Connection of Mysql database and oracal database

Source: Internet
Author: User

MySQL Database connection: (This example I'm just testing if connection is null, checking if the connection is successful)

 PackageJDBC;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.SQLException; Public classSearch {private static final String driver= "Com.mysql.jdbc.Driver"; private static final String url= "jdbc:mysql://localhost:3306/"
+ "Json?usessl=false&useunicode=true&characterencoding=utf-8"; Private Static FinalString user= "Root"; Private Static FinalString password= "1234"; Private StaticConnection conn=NULL; Static { Try{class.forname (driver); //Registration Driver}Catch(ClassNotFoundException e) {e.printstacktrace (); } } Public StaticConnection getconnection () {Try{conn=drivermanager.getconnection (URL, user, password);//Establish a connection returnConn; } Catch(SQLException e) {e.printstacktrace (); return NULL; } } Public Static voidmain (String [] args) {Connection conn=search.getconnection (); if(conn!=NULL) {System.out.println ("Connection Database succeeded"); }Else{System.out.println ("Failed to connect to database"); } }}

Oracle Data connection:

 Packagehello.ant;ImportJava.sql.*;  Public classjdbc{//set some parametersString url= "JDBC:ORACLE:THIN:@127.0.0.1:1521:ORCL";  String User= "Root"; String Password= "1234"; Connection Conn=NULL; Statement s; ResultSet R=NULL;  Publicjdbc () {Try{ class.forname (  "Oracle.jdbc.driver.OracleDriver"). newinstance ();  //Registration Driverconn = Drivermanager.getconnection (Url,user,password);//Establish a connectionS=c.createstatement ();//Create statement}Catch(Exception e) {e.printstacktrace (); } }  Public BooleanExecuteupdate (String sql) {//update the database data based on the SQL statement passed inTry{s.executeupdate (SQL);return true; }Catch(SQLException e) {e.printstacktrace (); return false; } }  PublicResultSet executeQuery (String sql) {//queries the database data based on the incoming SQL statement and returns the result set RTry{R=s.executequery (SQL);}Catch(SQLException e) {e.printstacktrace (); } returnR;}  Public voidClose () {//to close the result set and statementTry{r.close (); S.close (); }Catch(Exception e) {e.printstacktrace (); } }  Public Static voidMain (string[] args) {ResultSet R; JDBC J=NewJDBC ();//Instantiate model classR=j.executequery ("SELECT * from Test");//call the method of the query and pass the SQL statement overTry{      while(R.next ()) {//traversing result setsSystem.out.println (r.getstring ("id")); System.out.println (R.getstring ("Name"));//print a query to the data   } }Catch(Exception e) {e.printstacktrace ();}} } 

Connection of Mysql database and oracal database

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.