最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
RESTful+MySQL程序安裝講義

本文主要給大家介紹RESTful+MySQL程序安裝講義,希望可以給大家補充和更新些知識,如有其它問題需要了解的可以持續(xù)在創(chuàng)新互聯(lián)行業(yè)資訊里面關(guān)注我的更新文章的。    

10年積累的成都網(wǎng)站設(shè)計、成都網(wǎng)站制作經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認識你,你也不認識我。但先網(wǎng)站設(shè)計后付款的網(wǎng)站建設(shè)流程,更有林芝免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

準備工作

1、安裝mysql。

2、安裝mysql可視化工具Navicat。(由于本人偏好,所以暫時用這個可視化工具)。

3、Intellij安裝mysql jdbc驅(qū)動。

4、在GlassFish中加入mysql jdbc驅(qū)動。

RESTful+MySQL程序安裝講義

 

安裝啟動mysql

1、下載地址https://www.mysql.com/downloads/ (雖然你可以搜到很多下載的渠道,但是建議在官方下載,你懂的)。

2、根據(jù)提示進行安裝配置。(這不是重點,不清楚的自己google)。

3、如果出現(xiàn)安裝不上那應(yīng)該是系統(tǒng)權(quán)限問題,采用系統(tǒng)管理員權(quán)限安裝就可以了。(我在win10下進行安裝遇到了權(quán)限問題)。

4、啟動mysql服務(wù)。

5、加入測試數(shù)據(jù)。數(shù)據(jù)庫我們命名成RESTful,加一個表Product,里面包括4個字段:id、name、cover、price。具體如圖:

RESTful+MySQL程序安裝講義

為了方便測試就先加了一條記錄。

 

安裝Navicat

我們用Navicat進行可視化操作,當然你可以直接在mysql提供的工具或命令行進行數(shù)據(jù)操作。

1、下載地址https://www.navicat.com/download,至于是否付費或者破解就看你自己了,你懂的。

2、根據(jù)提示進行安裝。

 

Intellij安裝mysql jdbc驅(qū)動

1、在Intellij在主菜單中選擇View|Tool Windows|Databases。

RESTful+MySQL程序安裝講義

2、右邊彈出Database欄目,選擇上面的“+”,依次選擇DataSource|MySQL,此時彈出一個Data Source and Drive的對話框。

RESTful+MySQL程序安裝講義

 

RESTful+MySQL程序安裝講義

3、點擊左上方“+”,選擇MySQL。根據(jù)提示填寫右邊的字段。

RESTful+MySQL程序安裝講義

4、點擊右邊面板的Driver:MySQL,下載MySQL驅(qū)動。

 

在GlassFish中加入mysql jdbc驅(qū)動。

1、把mysql的驅(qū)動(eg:mysql-connector-java-5.1.35-bin.jar)放入..\glassfish5\glassfish\lib (具體參考你的GlassFish的安裝目錄)。

 

編碼

1、加入mysql驅(qū)動jar包。

2、目錄結(jié)構(gòu)如下:

RESTful+MySQL程序安裝講義

3、目錄結(jié)構(gòu)介紹。bo里面是實體類,dao是數(shù)據(jù)庫相關(guān)的操作類(為了方便理解流程所以不涉及ORM之類的東西)。下面來看看每個類的具體情況:

BoProduct.java

RESTful+MySQL程序安裝講義

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 +
                '}';
    }
}

RESTful+MySQL程序安裝講義

 

DbConnection.java

RESTful+MySQL程序安裝講義

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;
        }
    }
}

RESTful+MySQL程序安裝講義

 

DbProductManager.java

RESTful+MySQL程序安裝講義

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;
    }
}

RESTful+MySQL程序安裝講義

 

HelloWorld.java

RESTful+MySQL程序安裝講義

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();
    }
}

RESTful+MySQL程序安裝講義

 

MyApplication

RESTful+MySQL程序安裝講義

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> getClasses() {
        HashSet h = new HashSet>();
        h.add(HelloWorld.class);        return h;
    }
}

RESTful+MySQL程序安裝講義

運行程序

1、點擊運行按鈕。

RESTful+MySQL程序安裝講義

2、此時IDE為你做了一些你并不需要關(guān)系的事情(非業(yè)務(wù)的):編譯并部署到云服務(wù)器,啟動瀏覽器。

3、此時看到瀏覽器如下顯示則說明你成功了。

RESTful+MySQL程序安裝講義

看了以上關(guān)于RESTful+MySQL程序安裝講義,希望能給大家在實際運用中帶來一定的幫助。本文由于篇幅有限,難免會有不足和需要補充的地方,如有需要更加專業(yè)的解答,可在官網(wǎng)聯(lián)系我們的24小時售前售后,隨時幫您解答問題的。

 

 


文章題目:RESTful+MySQL程序安裝講義
本文路徑:http://fisionsoft.com.cn/article/gioghp.html