Configure and use JSP to connect to MySQL

Source: Internet
Author: User

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!

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.