千家信息网

RESTful+MySQL程序安装讲义

发表于:2024-10-12 作者:千家信息网编辑
千家信息网最后更新 2024年10月12日,本文主要给大家介绍RESTful+MySQL程序安装讲义,希望可以给大家补充和更新些知识,如有其它问题需要了解的可以持续在行业资讯里面关注我的更新文章的。准备工作1、安装mysql。2、安装mysql
千家信息网最后更新 2024年10月12日RESTful+MySQL程序安装讲义

本文主要给大家介绍RESTful+MySQL程序安装讲义,希望可以给大家补充和更新些知识,如有其它问题需要了解的可以持续在行业资讯里面关注我的更新文章的。

准备工作

1、安装mysql

2、安装mysql可视化工具Navicat。(由于本人偏好,所以暂时用这个可视化工具)。

3、Intellij安装mysql jdbc驱动。

4、GlassFish中加入mysql jdbc驱动。

安装启动mysql

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

2、根据提示进行安装配置。(这不是重点,不清楚的自己google)。

3、如果出现安装不上那应该是系统权限问题,采用系统管理员权限安装就可以了。(我在win10下进行安装遇到了权限问题)。

4、启动mysql服务。

5、加入测试数据。数据库我们命名成RESTful,加一个表Product,里面包括4个字段:id、name、cover、price。具体如图:

为了方便测试就先加了一条记录。

安装Navicat

我们用Navicat进行可视化操作,当然你可以直接在mysql提供的工具或命令行进行数据操作。

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

2、根据提示进行安装。

Intellij安装mysql jdbc驱动

1、Intellij在主菜单中选择View|Tool Windows|Databases

2、右边弹出Database栏目,选择上面的"+",依次选择DataSource|MySQL,此时弹出一个Data Source and Drive的对话框。

3、点击左上方"+",选择MySQL。根据提示填写右边的字段。

4、点击右边面板的DriverMySQL,下载MySQL驱动。

GlassFish中加入mysql jdbc驱动。

1、mysql的驱动(egmysql-connector-java-5.1.35-bin.jar)放入..\glassfish5\glassfish\lib (具体参考你的GlassFish的安装目录)。

编码

1、加入mysql驱动jar包。

2、目录结构如下:

3、目录结构介绍。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 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> getClasses() {        HashSet h = new HashSet>();        h.add(HelloWorld.class);        return h;    }}

运行程序

1、点击运行按钮。

2、此时IDE为你做了一些你并不需要关系的事情(非业务的):编译并部署到云服务器,启动浏览器。

3、此时看到浏览器如下显示则说明你成功了。

看了以上关于RESTful+MySQL程序安装讲义,希望能给大家在实际运用中带来一定的帮助。本文由于篇幅有限,难免会有不足和需要补充的地方,如有需要更加专业的解答,可在官网联系我们的24小时售前售后,随时帮您解答问题的。


0