Summary of JSP website development 3 and jsp website development 3
After summing up the previous two articles, I think you can't wait to learn about the connection between jsp and mysql Databases today. Since you need to connect to the mysql database, you must first ensure that your computer has installed the mysql database. For the installation steps of the mysql database, you can ask du Niang, there are countless blogs on the Internet. For mysql installation files, you can download the latest version from the Oricle website. To facilitate database management, we recommend SQLyog, a mysql database management software, for you here. You can also search for it on the Internet. The installation is very simple and the next step is the best. With the mysql database, we can start the next step.
This article explains how to use jsp to interact with the mysql database.
1. Create a database:
Click the SQLyou we have installed. Right-click the left blank area and select create database in the pop-up window. In the new window, enter the name of the database you want to create in the data item, select UTF-8 for the character set below, and then click OK.
2. Create a table:
With SQLyog, the operation for creating a table becomes very simple. Click the database we just created, select the table item, right-click it, create a table, and enter relevant content in the right area.
3. database connection:
Through the above two steps, we now have our database, and then start our connection operation. First, create a java file under the src directory of our project. The file name can be set as needed. To facilitate management, we recommend that you classify the java files when creating them, place different java files in different packages.
DBO. java:
Public class DBO {private static final String DBDRIVER = "org. gjt. mm. mysql. driver "; // database Driver address private static final String DBURL =" jdbc: mysql: // localhost: 3306/blog "; // database connection address private static final String DBNAME = "root"; // database account private static final String DBPAW = "root"; // Database Password static {try {Class. forName (DBDRIVER); // load the database driver} catch (ClassNotFoundException e) {e. printStackTrace () ;}} public static Connect Ion getConnection () {Connection conn = null; try {conn = DriverManager. getConnection (DBURL, DBNAME, DBPAW); // obtain the database connection object} catch (SQLException e) {e. printStackTrace () ;}return conn ;}// disable the public static void close (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 to overload public static void close (Statement st, Connection conn) {DBO. close (null, st, conn );}}
4. database connection test:
Compile a Test. java program:
Public class Test {/*** @ param database connection Test */@ SuppressWarnings ("static-access") public static void main (String [] args) {DBO db = new DBO (); Connection conn = null; conn = db. getConnection (); if (conn! = Null) {System. out. println ("the database connection is normal! ");}}}
5. Add the MySQL database jar package:
You can download the mysql database jar package online. You only need to download one package, then we need to add the jar file to the lib directory under WEB-INFO of our project. Then we open the test code and run the program. When the console outputs: the database connection is normal, the database connection is complete.
This blog is a summary of this Article. If you have any questions, you can leave a message. Next, add, delete, modify, and query mysql databases.