After summing up the first two articles, I think we must be eager to learn today's knowledge of the database connection between JSP and MySQL. Since the need to connect MySQL database, you first need to ensure that your computer has been installed MySQL database, MySQL database installation steps, we can ask the degree Niang, online blog Many countless, MySQL installation files, you can go to oricle official website to download the latest version. In order to facilitate our database management, here for everyone to recommend a MySQL database management software--sqlyog, the same on-line search, installation is very simple, the next step in the end can be. With MySQL database, we can start the next study.
This article explains how to use JSP to interact with the MySQL database.
1. Create a database:
Click on the sqlyou that we installed, open the left blank area to click the right mouse button, in the Pop-up window select Create DATABASE, in the new popup window, the data name entry fill our database name to create, the following character set select UTF-8, and then click OK.
2. Create a table:
The operation of creating a table is very simple, we can click the database we just created, select the table item, right-click, create the table, and fill in the SQLyog in the right area.
3. Database connection:
With the above two steps, now that we have our database, we will begin our connection operation. First we in the SRC directory under our project, create a new Java file, the file name can be arbitrarily set, in order to facilitate management, we recommend that you create a Java file to do a good job of classification, the role of different Java files in different packages.
Dbo.java:
Public classDBO {Private Static FinalString dbdriver= "Org.gjt.mm.mysql.Driver";//database-driven address Private Static FinalString dburl= "Jdbc:mysql://localhost:3306/blog";//Database Connection Address Private Static FinalString dbname= "root";//Database Account Private Static FinalString dbpaw= "root";//Database Password Static{ Try{class.forname (dbdriver);//Load Database driver}Catch(ClassNotFoundException e) {e.printstacktrace (); } } Public StaticConnection getconnection () {Connection conn=NULL; Try{conn= Drivermanager.getconnection (Dburl, DBNAME, Dbpaw);//Get database Connection object}Catch(SQLException e) {e.printstacktrace (); } returnConn; } //Turn off database operations Public Static voidClose (ResultSet rs, Statement St, Connection conn) {Try { if(rs!=NULL) {rs.close (); } if(st!=NULL) {rs.close (); } if(conn!=NULL) {rs.close (); } } Catch(SQLException e) {e.printstacktrace (); } } //Method Overloading Public Static voidClose (Statement St, Connection conn) {Dbo.close (NULL, ST, conn); }}
4. Database connection test:
Write a test program Test.java:
Public classTest {/** * @paramDatabase Connection Test*/@SuppressWarnings ("Static-access") Public Static voidMain (string[] args) {DBO db=NewDBO (); Connection Conn=NULL; Conn=db.getconnection (); if(conn!=NULL) {System.out.println ("The database connection is OK!"); } }}
5. Add MySQL database jar package:
You can download the MySQL database jar package on the Internet, this is nothing, everyone just download one on it, then we need to add the jar file to the Lib directory under the web-info of our project. Then we open the test code, run the program, when the console output: The database connection is OK, so our database connection is done.
This blog is for everyone to summarize here, we have any questions to exchange messages. The next MySQL database to add, delete, change, check operation.
The basic summary of JSP Web site development "three"