JSP connection to MySQL database

Source: Internet
Author: User

1. database connection and operation notes:

1. initialize the connection string

2. Send request connection

3. If you need to perform operations on the database, such as query, add, delete, and modify, send the database operation request and return the result if the operation succeeds. If the operation fails

4. Shut down database operations without any operation, and shut down database sending requests

 

2. Configure the environment

To do this, you must first sharpen the tool. "Before configuration, you must prepare several necessary files: Java JDK (I use 1.5), Tomcat (I use 5.5), and mysql(5.0), jdbc(mysql-connector-java-3.1.14.zip, of course, there are also necessary tools such as Dreamweaver.

First thing: Install JDK 1.5, install Tomcat and MySQL, decompress JDBC to the package (download from the official website and register one)

Second thing: Configure JDK, tomcat, and JDBC

JDBC configuration: My unzipping directory is I:/The mysql-connector-java-3.1.14 can see a mysql-connector-java-3.1.14-bin.jar of this jar file. Right-click computer Properties> advanced> environment variable> Find the system variable .. Click classpath, "edit" to join I:/mysql-connector-java-3.1.14/mysql-connector-java-3.1.14-bin.jar this path, here I am a disc idisk, JDBC can be unzipped to that disk all lines, the specified path is.

Step 2: JDBC unzipping package mysql-connector-java-3.1.14-bin.jar this file into the tomcat installation directory

Tomcat 5.5/common/lib

Tomcat 5.5/shared/lib

These two folders.

Basically, the work is done. Haha (of course I want to rewrite the online materials here. It's so bitter that I didn't have to copy it)

The basic work is ready. Find a MySQL database interface and operate the GUI software named navicat. Open MySQL and test whether your MySQL can be connected, if yes, you can create a new database test. A table named mytest has the ID and myname fields)

 

 

3. Test your connection to MySQL

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

Test (Database Name) dbuser (MySQL user) dbpwd (password) should be changed here ).

4. Start testing your own database

Create a login page: Login. jsp

<% @ Page contenttype = "text/html; charset = gb2312" Language = "Java" Import = "Java. SQL. *" errorpage = "" %> <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml"> 

Receiving and Processing page: checklogin. jsp

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

Finished. The function is incomplete. The basic work can start like this.

The process here can be summarized:

1. Load the JDBC driver

2. Define a connection

3. Establish a connection

4. Create a statement object

5. Execute query or update

6. Result Processing

7. Close the connection

References: Servlet and JSP core programming (version 2) page 1

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.