Servlet JSP connection to database

Source: Internet
Author: User

I. Environment Configuration

First, add the driver to classpath.

1. storage location of the driver when connecting to MySQL:
Driver name: mysql-connector-java-3.1.5-gamma-bin.jar
Path: tomcat5.5/common/lib
It takes effect after you restart tomcat.

2. storage location of the driver when connecting to Oracle:
Driver name: classes12.jar
Path: tomcat5.5/common/lib
It takes effect after you restart tomcat.

Ii. TestCode


<% @ page contenttype = "text/html; charset = gb2312" %>
<% @ page import = "Java. SQL. * "%>


<%
class. forname ("org. gjt. mm. mySQL. driver "). newinstance ();
string url = "JDBC: mysql: // localhost/db_name? User = yourusername & Password = yourpassword & useunicode = true & characterencoding = gb2312 ";
connection conn = drivermanager. getconnection (URL);
statement stmt = Conn. createstatement ();
string query = "select field_name from table_name order by ID";
resultset rs1_stmt.exe cutequery (query);
while (RS. next ()
{< br> string S = Rs. getstring ("field_name"); // check whether your field is of the struct type. Otherwise, you cannot use getstring. Use getboolean Based on the field class.
out. print (S + "
");
}< BR >%>

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.