JSP link MySQL

Source: Internet
Author: User
Test_mysql.jsp page, Code As follows:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page Language = "Java" %>
<% @ Page import = "com. MySQL. JDBC. Driver" %>
<% @ Page import = "Java. SQL. *" %>
<%

String drivername = "com. MySQL. JDBC. Driver"; // driverProgramName
String username = "cl41"; // database username
String userpasswd = "880710"; // Password
String dbname = "DB"; // Database Name
String tablename = "dbtest"; // table name

string url = "JDBC: mysql: // localhost/"+ dbname + "? User = "+ username +" & Password = "+ userpasswd; // concatenate a string
class. forname ("com. mySQL. JDBC. driver "). newinstance ();
connection = drivermanager. getconnection (URL);
statement = connection. createstatement ();
string SQL = "select * from" + tablename;
resultset rs = statement.exe cutequery (SQL);
resultsetmetadata rmeta = Rs. getmetadata (); // set of data results
int numcolumns = rmeta. getcolumncount (); // determines the number of columns in the dataset and the number of fields.

// Output each data value
Out. Print ("ID ");
Out. Print ("| ");
Out. Print ("num ");
Out. Print ("<br> ");
While (Rs. Next ()){
Out. Print (Rs. getstring (1) + "");
Out. Print ("| ");
Out. Print (Rs. getstring (2 ));
Out. Print ("<br> ");
}
Out. Print ("<br> ");
Out. Print ("database operation successful, congratulations ");
Rs. Close ();
Statement. Close ();
Connection. Close ();
%>

Then, deploy test _ mysql. jsp to Tomcat. For details about how to deploy it, see "Configure eclpise + Tomcat and compile and deploy jsp". The result is displayed in the browser.

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.