Connect to the database (JSP)

Source: Internet
Author: User

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>

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.