Connecting to MySQL in JSP

Source: Internet
Author: User
Tags driver manager

Previously, I had the experience of successfully connecting to MySQL through JDBC when writing an application.
Can I use the same code to connect to MySQL on a web page written in JSP?
The result is no. When I copy the same code to a file, an error occurs.
So Baidu fawei found a related article. After research, we only need to make some changes.
Change the original driver manager:
Class. forname ("org. gjt. Mm. MySQL. Driver ");
The following code can be used as a reference:

<HTML>
<Head> <title> helloworld. jsp </title> <Body>
<% @ Page Language = "Java" Import = "Java. SQL. *" %>
<%
Try {
Class. forname ("org. gjt. Mm. MySQL. Driver ");
Out. println ("Success loading MySQL driver! ");
}
Catch (exception E)
{
Out. Print ("error loading MySQL driver! ");
E. printstacktrace ();
}

Try {
Connection connect = drivermanager. getconnection ("JDBC: mysql: // localhost/jdbctest", "userid", "password ");
Out. Print ("Success connect MySQL server! ");

Statement stmt = connect. createstatement ();
Resultset rs1_stmt.exe cutequery ("select * from employees ");
While (Rs. Next ())
{
Out. println (Rs. getint (1 ));
Out. println (Rs. getstring (2 ));
Out. println (Rs. getint (3 ));
Out. println (Rs. getint (4 ));


}
}
Catch (exception E)
{
Out. Print ("Get data error! ");
E. printstacktrace ();
}
%>

</Body> NOTE: For the download of the MySQL JDBC driver, refer to my previous article.
After downloading the JAR file, copy it to the common/lib directory under the tomcat installation directory (I use the Tomcat server, but I have not studied other types of servers ).

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.