Java-database connection Java-MySQL connection ④: JDBC driver loading / September 2017

When using a text editor (Notepad) and a command prompt

At the command prompt, enter notepad ChkLoadJdbc.java to open Notepad

When using Eclipse

Open the Java project folder (ChkLoadJdbc) you have already created Right click on the src folder New-> Select Class The New Java Class screen is displayed

Name: ChkLoadJdbc □ Check "public static void main (String [] args)" □ Check "Generate comment" Press the "Finish" button

public class ChkLoadJdbc {
   public static void main(String[] args) throws InstantiationException, IllegalAccessException {
   String msg = "";
   try {
     Class.forName("com.mysql.jdbc.Driver").newInstance();
     msg = "The driver was successfully loaded";
   } catch (ClassNotFoundException e){
     msg = "Failed to load the driver";
   }
     System.out.println(msg);
   }
}

Run after programming

If you have successfully called the JDBC driver, "Successfully loaded the driver" If it fails, "Failed to load driver" is displayed.

In case of failure, check if the CLASSPATH setting is correct and correct.


Java-database connection / Java-MySQL connection ① Overview of JDBC and JDBC driver (2) JDBC driver acquisition method (for MySQL) and data connection preparation ③-1 How to set CLASSPATH in environment variable ③-2 How to set CLASSPATH to Eclipse build busLoad JDBC driverConnect to database (MySQL) to search table information and display search results


Recommended Posts

Java-database connection Java-MySQL connection ④: JDBC driver loading / September 2017
Java-database connection Java-MySQL connection ①: JDBC and JDBC driver overview / September 2017
Java-database connection Java-MySQL connection (2): JDBC driver acquisition method (for MySQL) and data connection preparation / September 2017
Java-database connection Java-MySQL connection ⑤: Connect to the database (MySQL) to search table information and display search results / September 2017