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.