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 make your Java application Program Find the MySQL driver.
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!
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