This is the test file I used to connect to MySQL. It is for reference only;
<! -- First import some necessary packages -->
<% @ Page import = "Java. Io. *" %>
<% @ Page import = "Java. util. *" %>
<! -- Tell the compiler to use the SQL package -->
<% @ Page import = "Java. SQL. *" %>
<! -- Set Chinese output -->
<% @ Page contenttype = "text/html; charset = gb2312" %>
<HTML>
<Head>
<Title> dbjsp. jsp </title>
</Head>
<Body>
<%
// Start with try
Try
{
Connection con;
Statement stmt;
Resultset RS;
// Load the driver. The following code loads the MySQL driver.
Class. forname ("com. MySQL. JDBC. Driver ");
// Register the MySQL driver
Drivermanager. registerdriver (new COM. MySQL. JDBC. Driver ());
// Connect to the database with the appropriate driver
String dburl = "JDBC: mysql: // localhost: 3306/bookdb? Useunicode = true & characterencoding = gb2312 ";
String dbuser = "dbuser ";
String dbpwds = "19820223 ";
// Establish a database connection
Con = java. SQL. drivermanager. getconnection (dburl, dbuser, dbpwd );
// Create a JDBC Statement
Stmt = con. createstatement ();
// Add a new record
Stmt.exe cuteupdate ("insert into books (ID, name, title, price) values ('000000', 'Tom ', 'tomcat le le', 999 )");
// Query records
Rs = stmt.exe cutequery ("select ID, name, title, price from books ");
// Output query results
Out. println ("<Table border = 1 width = 400> ");
While (Rs. Next ())
{
String col1 = Rs. getstring (1 );
String col2 = Rs. getstring (2 );
String col3 = Rs. getstring (3 );
Float col4 = Rs. getfloat (4 );
// Print the displayed data
Out. println ("<tr> <TD>" + col1 + "</TD> <TD>" + col2 + "</TD> <TD>" + col3 + "</ TD> <TD> "+ col4 +" </TD> </tr> ");
}
Out. println ("</table> ");
// Delete the newly added record
Stmt.exe cuteupdate ("delete from books where id = '000000 ′");
// Close the database connection
Rs. Close ();
Stmt. Close ();
Con. Close ();
}
// Capture error information
Catch (exception e) {out. println (E. getmessage ());}
%>
</Body>
</Html>