Common error: When connecting to a database, this sentence string url = "jdbc:mysql://localhost/database name"; The database name may not be the same as the database name you built
1, install Mysql, remember at this time you set the name of the database, here I set is:mysql, Password: 123,root (one of the common mistakes, the back of the book when writing the database to the name of MYDB3, and I name is Mysql)
2, Myeclise built a Java project, build a class, note that at this time to guide the package, JDBC package, the Guide package operation directly pull the compressed file in, and then also to the right to add to the path (one of the common errors did not right-click Import);
3, the establishment of a good class, run the program; Right outline you dmeo function, right-run as, the console below can see whether the link is successful, and if an exception can occur in JUnit one of the common see exception
The above red line, is my own two mistakes, the next attention.
. Java code
PackageCn.idcast.demo1;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.SQLException;Importorg.junit.Test; Public classDemo1 {/*** ClassNotFoundException: * > No driver package * > * * SQLException: * > Check 3 parameters: URL, username , password is correct * > check if MySQL server is turned on! */@Test Public voidFUN1 ()throwsClassNotFoundException, SQLException {/** JDBC Four configuration parameters: * > DriverClassName:com.mysql.jdbc.Driver * > Url:jdbc:mysql://localhost: 3306/MYDB3 * > Username:root * > Password:123*/ /** All Java.sql.Driver implementations are provided with a static block, and the code inside the block is registering itself in the * drivermanager! */ /** After jdbc4.0, in each drive jar, a file named Java.sql.Driver is provided in the Meta-inf/services directory. * The content of the file is the implementation class name of the interface! */Class.forName ("Com.mysql.jdbc.Driver");//load Driver Class (registered driver)//com.mysql.jdbc.Driver Driver = new Com.mysql.jdbc.Driver ();//drivermanager.registerdriver (driver); //use URL, username, password to get the connection objectConnection con =Drivermanager.getconnection ("Jdbc:mysql://localhost:3306/mysql", "root", "123"); can also be separated to write System.out.println (con); }}
Learn notes _jdbc_1_demo1_ basic operations and procedures for connecting to a database