I. Software Download
Go to the MySQL official website to download the following two tools: mysql-5.1.32-win32.msi, mysql-gui-tools-5.0-r17-win32.msi
The former is the MySQL installation file, and the latter is the MySQL Tool installation file, including JDBC.
II. Environment Configuration
Copy the mysql-connector-java-5.0.4-bin.jar from MySQL \ MySQL Tools for 5.0 \ java \ lib to D: \ Tomcat 6.0 \ lib, and then add D: \ Tomcat 6.0 \ lib \ mysql-connector-java-5.0.4-bin.jar in classpath.
The purpose of this configuration is to allow your java application to find the driver connecting to mysql.
Copy this step to configure the driver for connecting to the database through JSP.
Iii. JSP connection to MySQL
Create the database Education and table UserInfo. Now we try to use jsp to connect to mysql.
Create test page TestLinkDateBase. jsp
<% @ Page contentType = "text/html; charset = gb2312" %>
<% @ Page language = "java" %>
<% @ Page import = "com. mysql. jdbc. Driver" %>
<% @ Page import = "java. SQL. *" %>
<%
// Driver name
String driverName = "com. mysql. jdbc. Driver ";
// Database username
String userName = "root ";
// Password
String userPasswd = "123 ";
// Database Name
String dbName = "education ";
// Table name
String tableName = "UserInfo ";
// Concatenates strings
String url = "jdbc: mysql: // localhost/" + dbName + "? User = "+ userName +" & password = "+ userPasswd;
Class. forName ("com. mysql. jdbc. Driver"). newInstance ();
Connection connection = DriverManager. getConnection (url );
Statement statement = connection. createStatement ();
String SQL = "SELECT * FROM" + tableName;
ResultSet rs = statement.exe cuteQuery (SQL );
// Obtain the data result set
ResultSetMetaData rmeta = rs. getMetaData ();
// Determine the number of columns and fields of the dataset.
Int numColumns = rmeta. getColumnCount ();
// Output each data value
Out. print ("id ");
Out. print ("| ");
Out. print ("num ");
Out. print ("<br> ");
While (rs. next ()){
Out. print (rs. getString (2) + "");
Out. print ("| ");
Out. print (rs. getString (3 ));
Out. print ("<br> ");
}
Out. print ("<br> ");
Out. print ("database operation successful, congratulations ");
Rs. close ();
Statement. close ();
Connection. close ();
%>
4. You can view the data displayed successfully on the page!