JSP connection to Mysql

Source: Internet
Author: User
Tags mysql functions
[Original] Tack 2003-10-05
It is an ideal runtime environment for beginners to build j2ee Using jsp + tomcat + mysql. In the past, I used to use php's built-in mysql functions. The connection between jsp and mysql seems a little troublesome, and there is also a lack of such information, therefore, the connection between jsp and database has become the first threshold for beginners. jsp cannot connect to the database, which will undoubtedly cause a huge obstacle to learning. As a matter of fact, it will be very simple. Next, we will go to the following process to establish a jsp connection to mysql without several steps step by step. Before that, please make sure that your Tomcat can be started, mysql can be logged on, and jsp can be run.
First, click the address below to download the latest mysql Driver (Mysql JDBC Driver) (2003.10)
Http://mysql.linuxforum.net/Downloads/Connector'> http://mysql.linuxforum.net/Downloads/Connector-J/mysql-connector-java-3.1.0-alpha.zip (you can update your driver at www.mysql.com .)
After decompression Mysql-connector-java-3.1.0-alpha-bin.jarCopy files to your Tomcat/common/libRestart Tomcat. the jsp connection to the mysql environment is complete.
Next, we will use a jsp test program to extract database data: <% @ Page contentType = "text/html; charset = gb2312" %>
<% @ Page import = "java. SQL. *" %>
<Html>
<Body>
<%
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 ())
{
String s = rs. getString ("field_name"); // check whether your field is of the struct type. Otherwise, getString cannot be used, and getBoolean can be used based on the field class.
Out. print (s + "<br> ");
}
%>
</Body>
</Html>

Change yourusername and yourpassword to your mysql user name and password, and change the other db_name, field_name, and table_name to the corresponding name.
Have you seen the data in your data table? Is it easy? As long as you know how to use JDBC, the connection to other databases is also a problem. Now you can do a lot of things. Continue on your jsp journey. Have a good trip!

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.