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
The page code is as follows:
Copy codeThe 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