準備工做java
一、安裝mysql。mysql
二、安裝mysql可視化工具Navicat。(因爲本人偏好,因此暫時用這個可視化工具)。sql
三、Intellij安裝mysql jdbc驅動。數據庫
四、在GlassFish中加入mysql jdbc驅動。瀏覽器
安裝啓動mysql服務器
一、下載地址https://www.mysql.com/downloads/ (雖然你能夠搜到不少下載的渠道,可是建議在官方下載,你懂的)。app
二、根據提示進行安裝配置。(這不是重點,不清楚的本身google)。ide
三、若是出現安裝不上那應該是系統權限問題,採用系統管理員權限安裝就能夠了。(我在win10下進行安裝遇到了權限問題)。工具
四、啓動mysql服務。測試
五、加入測試數據。數據庫咱們命名成RESTful,加一個表Product,裏面包括4個字段:id、name、cover、price。具體如圖:
爲了方便測試就先加了一條記錄。
安裝Navicat
咱們用Navicat進行可視化操做,固然你能夠直接在mysql提供的工具或命令行進行數據操做。
一、下載地址https://www.navicat.com/download,至因而否付費或者破解就看你本身了,你懂的。
二、根據提示進行安裝。
Intellij安裝mysql jdbc驅動
一、在Intellij在主菜單中選擇View|Tool Windows|Databases。
二、右邊彈出Database欄目,選擇上面的「+」,依次選擇DataSource|MySQL,此時彈出一個Data Source and Drive的對話框。
三、點擊左上方「+」,選擇MySQL。根據提示填寫右邊的字段。
四、點擊右邊面板的Driver:MySQL,下載MySQL驅動。
在GlassFish中加入mysql jdbc驅動。
一、把mysql的驅動(eg:mysql-connector-java-5.1.35-bin.jar)放入..\glassfish4\glassfish\lib (具體參考你的GlassFish的安裝目錄)。
編碼
一、加入mysql驅動jar包。
二、目錄結構以下:
三、目錄結構介紹。bo裏面是實體類,dao是數據庫相關的操做類(爲了方便理解流程因此不涉及ORM之類的東西)。下面來看看每一個類的具體狀況:
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<BoProduct> getAllProduct() { List<BoProduct> 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<BoProduct> 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<Class<?>> getClasses() { HashSet h = new HashSet<Class<?>>(); h.add(HelloWorld.class); return h; } }
運行程序
一、點擊運行按鈕。
二、此時IDE爲你作了一些你並不須要關係的事情(非業務的):編譯並部署到服務器,啓動瀏覽器。
三、此時看到瀏覽器以下顯示則說明你成功了。