1. Where it appears
1 PackageCom.jdbc;2 3 Importjava.sql.Connection;4 ImportJava.sql.DriverManager;5 Importjava.sql.SQLException;6 7 Public classJDBC {8 9 //connect to MySQL database, user name and password are rootTen Private StaticString url = "Jdbc:mysql://localhost:3306/mydb" ; One Private StaticString username = "root" ; A Private StaticString password = "123456" ; - - Private StaticConnection Conn; the - Public StaticConnection getconnection () { - //1. Load the JDBC driver: Before connecting to the database, first load the driver of the database you want to connect to the JVM (Java Virtual machine) - + Try { - //load the MySQL driver class +Class.forName ("Com.mysql.jdbc.Driver") ; Aconn = at drivermanager.getconnection (URL, username, password); -}Catch(ClassNotFoundException e) { -System.out.println ("Driver class not found, load driver failed!") "); - e.printstacktrace (); -}Catch(SQLException e) { -SYSTEM.OUT.PRINTLN ("Database connection failed! "); in e.printstacktrace (); - } to + returnConn; - } the *}
(1) Error 1 driver class not found, load driver failed!
Java.lang.ClassNotFoundException:com.mysql.jdbc.Driver
error Reason: No JDBC driver jar package in Java project or Web project
Solution: Download the jar package Mysql-connector-java-5.1.7-bin.jar and import the project
(2) error 2 Database connection Failed
Access denied for user ' root ' @ ' localhost ' (using Password:yes)
Solution: Restart your computer
Errors that occur in the Java project JDBC Link database