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 ();}