Java cannot connect to mysql database =

Source: Internet
Author: User
The code is as follows: importjava. SQL. connection; importjava. SQL. driverManager; importjava. SQL. resultSet; importjava. SQL. SQLException; importjava. SQL. statement; importjavax. swing. JOptionPane; publicclassmysql {publicstaticvoidmain (String [] args) {Strin... mysqljava database

The code is as follows:
Import java. SQL. Connection;
Import java. SQL. DriverManager;
Import java. SQL. ResultSet;
Import java. SQL. SQLException;
Import java. SQL. Statement;

Import javax. swing. JOptionPane;

Public class mysql {

Public static void main (String [] args) {String driver = "com. mysql. jdbc. driver "; // The URL points to the name of the database to be accessed String url =" jdbc: mysql: // localhost: 3306/test "; // username String user = "root" for MySQL configuration; // password String password = "123456" for Java connection to MySQL configuration; try {// load the driver Class. forName (driver); // continuous database Connection conn = DriverManager. getConnection (url, user, password); if (! Conn. isClosed () System. out. println ("Succeeded connecting to the Database! ");} Catch (ClassNotFoundException e) {System. out. println (" Sorry, can't find the Driver! "); E. printStackTrace ();} catch (SQLException e) {e. printStackTrace ();} catch (Exception e) {e. printStackTrace ();}}

}

Then an error is prompted,
Sorry, can't find the Driver!
Java. lang. ClassNotFoundException: com. mysql. jdbc. Driver
At java.net. URLClassLoader $ 1.run( URLClassLoader. java: 366)
At java.net. URLClassLoader $ 1.run( URLClassLoader. java: 355)
At java. security. AccessController. doPrivileged (Native Method)
At java.net. URLClassLoader. findClass (URLClassLoader. java: 354)
At java. lang. ClassLoader. loadClass (ClassLoader. java: 424)
At sun. misc. Launcher $ AppClassLoader. loadClass (Launcher. java: 308)
At java. lang. ClassLoader. loadClass (ClassLoader. java: 357)
At java. lang. Class. forName0 (Native Method)
At java. lang. Class. forName (Class. java: 190)
At mysql. main (mysql. java: 25)
, How to solve it //,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.