Errors that occur in the Java project JDBC Link database

Source: Internet
Author: User

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

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.