SQLException and jdbcsqlexception caused by JDBC calls on Jsp pages

Source: Internet
Author: User

SQLException and jdbcsqlexception caused by JDBC calls on Jsp pages

When you connect to the MySQL database locally, you have always used to directly import the JDBC jar package in the project lib. In this way, you only need the following code to get a Connection.

String user = "xxx"; String password = "123"; String url = "jdbc: mysql: // localhost: 3306/xxx"; Connection con = null; try {con = DriverManager. getConnection (url, user, password);} catch (SQLException e) {e. printStackTrace ();} return con;

The above call has been normal locally. However, when writing a Jsp page, import the jar package to lib in the same step. The database operation test function is normal, but the browser will see SQLException when calling the database, after reviewing the Code several times, no problem is found. When the following code is added, the problem is solved. On the server side, the classes used will not be automatically loaded even if the package is imported when the database is called. Please correct me.

Try {// load the MySql Driver Class. forName ("com. mysql. jdbc. driver ");} catch (ClassNotFoundException e) {System. out. println ("The driver class cannot be found. Failed to load the driver! "); E. printStackTrace ();}

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.