Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

RESTful+MySQL program installation handout

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the RESTful+MySQL program installation handout, hoping to supplement and update some knowledge, if you have other questions to understand, you can continue to follow my updated article in the industry information.

Preparatory work

1. Install mysql.

2. Install the mysql visualization tool Navicat. Because of my preference, I use this visualization tool for the time being.

3. Install mysql jdbc driver for Intellij.

4. Add mysql jdbc driver to GlassFish.

Install and start mysql

1. Download address https://www.mysql.com/downloads/ (although you can find many download channels, it is recommended to download it officially, you know).

2. Install and configure according to the prompts. (this is not the point, not clear about their own google).

3, if the installation can not be installed, it should be the problem of system permissions, just use the privileges of the system administrator to install it. (I encountered permission issues when installing under win10).

4. Start mysql service.

5. Add test data. The database is named RESTful, plus a table Product, which includes four fields: id, name, cover, price. As shown in the figure:

A record was added to facilitate the test.

Install Navicat

We use Navicat for visual operations, and of course you can manipulate data directly from the tools or command line provided by mysql.

1, download address https://www.navicat.com/download, as to whether to pay or crack it is up to you, you know.

2. Install according to the prompts.

Intellij installs mysql jdbc driver

1. In Intellij, select View | Tool Windows | Databases from the main menu.

2. The Database column pops up on the right, select "+" above, and then select DataSource | MySQL. A DataSource and Drive dialog box pops up at this time.

3. Click "+" at the top left and select MySQL. Fill in the field on the right according to the prompt.

4. Click Driver:MySQL on the right panel to download the MySQL driver.

Add mysql jdbc driver to GlassFish.

1. Put the mysql driver (eg:mysql-connector-java-5.1.35-bin.jar) into..\ glassfish5\ glassfish\ lib (refer to your GlassFish installation directory for details).

Coding

1. Add mysql driver jar package.

2. The directory structure is as follows:

3. Introduction of directory structure. Bo contains entity classes, and dao is a database-related operation class (ORM and the like are not involved in order to make it easier to understand the process). Let's take a look at the details of each class:

BoProduct.java

Package bo;/** * Created by Administrator on 2017-2-19 0019. * / public class BoProduct {private int id; private String name; private String cover; private long price; public BoProduct (int id, String name, String cover, long price) {this.id = id; this.name = name; this.cover = cover; this.price = price;} public int getId () {return id;} public void setId (int id) {this.id = id } public String getName () {return name;} public void setName (String name) {this.name = name;} public String getCover () {return cover;} public void setCover (String cover) {this.cover = cover;} public long getPrice () {return price;} public void setPrice (long price) {this.price = price } @ Override public String toString () {return "BoProduct {" + "id=" + id + ", name='" + name +'\'+ ", cover='" + cover +'\'+ ", price=" + price +'}';}}

DbConnection.java

Package dao;/** * Created by Administrator on 2017-2-19 0019. * / import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;public class DbConnection {public Connection getConnection () throws Exception {try {String connectionURL = "jdbc:mysql://localhost:3306/RESTful"; Connection connection = null; Class.forName ("com.mysql.jdbc.Driver"). NewInstance (); connection = DriverManager.getConnection (connectionURL, "root", "root") Return connection;} catch (SQLException e) {e.printStackTrace (); throw e;} catch (Exception e) {e.printStackTrace (); throw e;}

DbProductManager.java

Package dao;import bo.BoProduct;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.ArrayList;import java.util.List;/** * Created by Administrator on 2017-2-19 0019. * / public class DbProductManager {public List getAllProduct () {List reuslt = null; DbConnection database = new DbConnection (); Connection connection = null; try {connection = database.getConnection (); PreparedStatement ps = connection .preparestatement ("SELECT * FROM product"); ResultSet rs = ps.executeQuery (); reuslt = new ArrayList () While (rs.next ()) {BoProduct item = new BoProduct (rs.getInt ("id"), rs.getString ("name"), rs.getString ("cover"), rs.getLong ("price"); reuslt.add (item);} catch (Exception e) {e.printStackTrace ();} return reuslt;}}

HelloWorld.java

Import bo.BoProduct;import dao.DbProductManager;import javax.ws.rs.GET;import javax.ws.rs.Path;import javax.ws.rs.Produces;import javax.ws.rs.core.MediaType;import java.util.List;/** * Created by Administrator on 2017-2-18 0018. * / @ Path ("/ helloworld") public class HelloWorld {@ GET @ Produces (MediaType.TEXT_PLAIN) public String getClichedMessage () {StringBuilder stringBuilder = new StringBuilder (); stringBuilder.append ("data being\ n"); DbProductManager dbProductManager = new DbProductManager (); List allProduct = dbProductManager.getAllProduct () If (null! = allProduct & &! allProduct.isEmpty ()) {for (BoProduct item: allProduct) {stringBuilder.append (item.toString ()) .append ("\ n");}} stringBuilder.append ("data end\ n"); return stringBuilder.toString ();}}

MyApplication

Import javax.ws.rs.ApplicationPath;import javax.ws.rs.core.Application;import java.util.HashSet;import java.util.Set;/** * Created by Administrator on 2017-2-18 0018. * / Defines the base URI for all resource URIs.@ApplicationPath ("/") / / The java class declares root resource and provider classespublic class MyApplication extends Application {/ / The method returns a non-empty collection with classes, that must be included in the published JAX-RS application @ Override public Set > (); h.add (HelloWorld.class); return h;}}

Run the program

1. Click the run button.

2. At this point, IDE does something for you that you don't need a relationship with (non-business): compile and deploy to the CVM, start the browser.

3. When you see the following display in the browser, it shows that you have succeeded.

Read the above RESTful+MySQL program installation handouts, hoping to give you some help in practical application. Due to the limited space in this article, it is inevitable that there will be deficiencies and need to be supplemented. If you need more professional answers, you can contact us on the official website for 24-hour pre-sales and after-sales to help you answer questions at any time.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report