Take MySQL database as an example
1, download the driver, then put the jar into the JRE's Ext folder and the JDK Jre\ext, without setting environment variables, if you understand the loading process of the virtual machine, you will know that a load class will load the package under EXT
2. Write test procedures
Importjava.sql.*; Public class TestDB { Public Static void Main(string[] args) {//1, load drive Try{Class.forName ("Com.mysql.jdbc.Driver"); }Catch(ClassNotFoundException ex) {System.out.println (ex);return; } System.out.println ("Load driver succeeded");//2, Establish connection Try{Connection conn = drivermanager.getconnection ("Jdbc:mysql:///test","Root","123456"); System.out.println ("Connection to test Database" was established);///3, create execution statementStatement Statement = Conn.createstatement ();//4, query statementString sql ="SELECT * from T_person";///5, get result setResultSet result = statement.executequery (SQL);intCount =0; while(Result.next ()) {count++; System.out.println ("section"+count+"article Record:"+ result.getstring (1) +"\ T"+ result.getstring (2) +"\ T"+ result.getstring (3) +"\ T"+ result.getstring (4) +"\ T"+ Result.getfloat (5)); } }Catch(SQLException Sqlex) {System.out.println (Sqlex);return; } }}
Java Installation Database driver