How to connect to MySQL database in JSP

Source: Internet
Author: User
Tags mysql commands mysql command line

First, the JSP program written on Eclipse to connect to the MySQL database always reports an error. I have all been down and marshal is ready to give up MySQL,... as in the TV series, the transfer always appears at this time. First, I saw saulzy's Installation Introduction to MySQL4.1.11, and then I found a good MySQL interface plug-in, finally, the JDBC driver of MySQL was configured and connected with the help of netizens. five days in a row, it was really hard to get through, but in the process, I also felt that I had learned a lot, huh, and I had to keep moving forward. now, let's make a summary over the past few days, hoping to help the same person who is learning Java technology.

I. JSP connection to MySQL software download

MySQL

Download version: 4.1.11

Http://dev.MySQL.com/downloads/MySQL/4.1.html

JDBC driver

Download version: 3.1.8

Http://dev.MySQL.com/downloads/connector/j/3.1.html

MySQL interface plug-in: MySQL-front

Download the version image: HongKong, which is the Chinese version)

Http://www.MySQLfront.de/download.html

Ii. JSP connection to MySQL software installation

1. Install MySQL

See related articles and recommended articles:

Http://blog.csdn.net/saulzy/archive/2005/04/23/359648.aspx

2. JDBC driver: MySQL-connector-Java-3.1.8

This is just a compressed package, does not need to install, as long as it is unzipped, what I use is folder MySQL-connector-Java-3.1.8 file: MySQL-connector-Java-3.1.8-bin.jar.

3. MySQL interface plug-in: MySQL-front

This is an installation program. Follow the prompts to install it.

Iii. JSP connection to MySQL environment Configuration

First, I want to explain that my current tomcat installation path is: D: \ Program Files \ Java \ Tomcat; JDK installation path is: D: \ Program Files \ Java \ j2sdk.

The JDBC driver needs to be configured here. before configuring, first put the MySQL-connector-Java-3.1.8-bin.jar local hard drive I put somewhere: D: \ Program Files \ Java \ MySQLforjdbc), and then according to your place, configure classpath, my configuration is as follows:

 
 
  1. D:\Program files\Java\j2sdk\lib\tools.jar;  
  2. D:\Program Files\Java\j2sdk\lib\MySQL-connector-Java-3.1.8-bin-g.jar;  
  3. D:\Program Files\Java\MySQLforjdbc\MySQL-connector-Java-3.1.8-bin.jar 

The purpose of this configuration is to allow your Java application to find the driver connecting to MySQL.

After configuring the environment variables, there is a very important step is to configure the driver for JSP Connection database, this is actually very simple, is to copy the MySQL-connector-Java-3.1.8-bin.jar to some folders on the line, I read a lot of information on the Internet and asked a lot of people about the various statements. I have integrated them and I have done everything for the sake of insurance, anyway, it is to copy a K file, now list the folder to copy the MySQL-connector-Java-3.1.8-bin.jar, as follows:

D: \ Program Files \ Java \ Tomcat \ common \ lib

D: \ Program Files \ Java \ Tomcat \ shared \ lib

Iv. database usage

After MySQL is installed, some recommended articles should be noted ):

Http://blog.csdn.net/saulzy/archive/2005/04/23/359811.aspx

As mentioned in the article, the most important thing after MySQL is installed is to check whether the database has been started as a system service. Therefore, before performing database operations, you should check whether the database is started, at the start of the operating system, choose run> enter services. msc. Make sure that the MySQL service you set has been started during installation. In this way, no connection error will be reported during database operations.

I mentioned a more convenient MySQL interface plug-in, but this interface was found only after I started using MySQL. At the beginning, I started to operate on it using command lines in dos. although the interface can also be used for database creation and permission setting, I think it is also an important skill to know how to use the command line. So let's start with the command line, how to use MySQL. I will talk about MySQL-front later.

Now I want to create a database shujuku in MySQL and a table biao in the database. The specific command is as follows: I have just installed MySQL)

1. Enter the dos Status. Remember the command line to run in the bin directory under the MySQL installation directory)

2. Connect to MySQL

Input: MySQL-h localhost-u root-p

Enter the password set during installation, and enter the MySQL Command editing interface.

3. Use Basic MySQL commands (after each command is entered in the MySQL command line, there must be a semicolon (; otherwise, an error will be reported)

Display Database: show databases;

Database used: use Database Name;

4. Create a database

Command: create database shujuku;

5. Set the database permission user and password)

Command: grant all privileges on shujuku. *

 
 
  1. [url=mailto:test@localhost]test@localhost[/url]  
  2. identified by “123456”; 

After you execute this command, you only need to operate the database shujuku when you log on to shujuku with the username: test and password: 123456. This avoids the use of root, this is of great help to database security.

6. Create a table

Command: create table biao (id int (8) primary key, name varchar (10 ));

The remaining sqsl commands are basically the same as standard sqsl commands.

It is worth mentioning that you enter "? ", There will be a simple help of the MySQL Command, as shown below:

Well, we can also know to exit, that is, "exit!

V. Use of MySQL-front

I found several MySQL interface tools and thought it was the most concise and convenient MySQL-front, but it was a pity that I had to pay for it, but fortunately I had a trial period, the most important thing is that MySQL-front has a Simplified Chinese version. If the English is not good, I will be much more comfortable to use. the following is a brief introduction.

First of all, the installation is needless to say, there is a wizard, and it is very simple. after installation, a dialog box is displayed during the first running. Here you can add shujuku as set above. The process is as follows:

After you fill in the user name and password you set in MySQL above in the check box for registration, there will be a database for shujuku in the select database box. Select and press OK. after entering MySQL-fron, you will see the following interface. You can perform this operation.

It should be noted that you can also add the root user, which requires you to select Settings on the MySQL-fron interface-> dialog-> Create, and then press the above button, with root, you can add more users. The method is the same. Setting different users makes it easy to manage different databases, do not allow others to use your root user to ensure the security of your database.

Vi. JSP connection to MySQL

Now we are trying to connect to MySQL Using jsp.

I created a test_MySQL.jsp page in eclipse. The Code is as follows:

 
 
  1. // Driver name
  2. StringDriverName="Com. MySQL. jdbc. Driver";
  3. // Database username
  4. StringUserName="Cl41";
  5. // Password www.knowsky.com
  6. StringUserPasswd="123456";
  7. // Database Name
  8. StringDbName="Db";
  9. // Table name
  10. StringTableName="Dbtest";
  11. // Concatenates strings
  12. StringUrl="Jdbc: MySQL: // localhost /"+ DbName + "?User="+ UserName +"&Password= "+ UserPasswd;
  13. Class. forName ("com. MySQL. jdbc. Driver"). newInstance ();
  14. ConnectionConnection=DriverManager. GetConnection (url );
  15. StatementStatement=Connection. CreateStatement ();
  16. StringSQL="SELECT * FROM"+ TableName;
  17. ResultSetRs=Statement. ExecuteQuery (SQL );
  18. // Obtain the data result set
  19. ResultSetMetaDataRmeta=Rs. GetMetaData ();
  20. // Determine the number of columns and fields of the dataset.
  21. IntNumColumns=Rmeta. GetColumnCount ();
  22. // Output each data value
  23. Out. print ("id ");
  24. Out. print ("| ");
  25. Out. print ("num ");
  26. Out. print ("
  27. ");
  28. While (rs. next ()){
  29. Out. print (rs. getString (1) + "");
  30. Out. print ("| ");
  31. Out. print (rs. getString (2 ));
  32. Out. print ("
  33. ");
  34. }
  35. Out. print ("
  36. ");
  37. Out. print ("database operation successful, congratulations ");
  38. Rs. close ();
  39. Statement. close ();
  40. Connection. close ();
  41. %> 

Then, deploy test _ MySQL. jsp to tomcat. For details about how to deploy it, see "Configure Eclpise + tomcat and compile and deploy JSP". The result is displayed in the browser.

  1. Introduction to the Forward and sendRedirect methods of JSP
  2. Use JSP pages to generate PDF reports
  3. Step for customizing JSP labels
  4. Detailed test of JSP containers
  5. Describes the following features of jsp http Server

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.