The packages used in JDBC are almost always in import?java.sql.*;
- Import Oracel or MySQL packages and load drivers in your project:
The classpath of the project and the classpath of the system are put in classpath, if it is placed in the classpath of the system, if the version is different, it may interfere with the different projects in the system, put under the project, reduce the coupling between the projects; Load Oracle Driver:
Class.forName ("Oracle.jdbc.driver.OracleDriver");
Load MySQL driver:
Class.forName ("Com.mysql.jdbc.Driver");
- To establish a connection:
1.1. Establish an Oracle connection:
Connection conn = drivermanager.getconnection ("Jdbc:mysql://host:port/datebase", "User", "password");
2.1. Establish MySQL Connection:
Connection conn = drivermanager.getconnection ("Jdbc:mysql://host:port/datebase", "User", "password");
Import java.sql.*;p Ublic class Testjdbc {public static void Main (string[] args) throws Exception { //class.forna Me ("Com.mysql.jdbc.Driver"); Long start = System.currenttimemillis (); New Com.mysql.jdbc.Driver (); Connection conn = drivermanager.getconnection ("Jdbc:mysql://host:port/datebase", "User", "password"); Connection conn = drivermanager.getconnection ("Jdbc:mysql://localhost:3306/martin", "root", "admin"); Connection conn = drivermanager.getconnection ("jdbc:oracle:[email protected":p ort:datebase "," User "," password "); SYSTEM.OUT.PRINTLN (conn); SYSTEM.OUT.PRINTLN (conn); Long end = System.currenttimemillis (); SYSTEM.OUT.PRINTLN ("Establish connection time:" + (End-start));} }
JDBC Link MySQL and Oracle