Four key steps for JDBC connection to the MySQL database
1. Find the driver
MySQL currently provides the Java driver for Connection/J, can be downloaded from the MySQL official website, and find the mysql-connector-java-3.0.15-ga-bin.jar file, this driver is pure Java driver, no additional configuration is required for connecting to MySQL through JDBC.
2. dynamically specify classpath
If you need to dynamically specify the classpath for execution, use the-cp method during execution. Otherwise, add the above. jar file to the classpath environment variable.
3. Load JDBC to connect to MySQL
Try {
Class. forName (com. mysql. jdbc. Driver );
System. out. println (Success loading Mysql Driver !);
} Catch (Exception e)
{
System. out. println (Error loading Mysql Driver !);
E. printStackTrace ();
}
4. Set the URL for JDBC connection to MySQL
Jdbc: mysql: // localhost/databasename [? Pa = va] [& pa = va]