How to connect JSP to MySQL database
First, copy the MySQL database connection driver to the tomcat/common/lib package.
Download mysql-connector-java-5.1.6.zip
Http://mysql.cs.pu.edu.tw/Downloads/Connector-J/mysql-connector-java-5.1.6.zip
Configure the Tomcat virtual directory-Omitted
Create jdbcmysql. jsp page
Page Code As follows:
Copy code The Code is as follows: <% @ page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. SQL. *" %>
<%
Final string mysqldbdriver = "org. gjt. Mm. MySQL. Driver ";
Final string mysqldburl = "JDBC: mysql: // localhost/mldn ";
Final string mysqldbuser = "root ";
Final string mysqldbuserpass = "root ";
Connection con = NULL;
%>
<%
Try {
Class. forname (mysqldbdriver );
Con = drivermanager. getconnection (mysqldburl, mysqldbuser, mysqldbuserpass );
Statement stmt = con. createstatement ();
String SQL = "CREATE TABLE jobs (name varchar (32 ))";
// Execute the create statement
Stmt.exe cuteupdate (SQL );
Out. println ("connection successful! ");
Stmt. Close ();
} Catch (exception e ){
Out. println (E );
}
%>
Start Tomcat
Enter http: // localhost: 8080/demo/jdbcmysql. jsp in the address bar.
Data connection test successful!
Verify Database
Verification Successful!
----- Method for connecting ms SQL Server/Oracle to be continued