A restful+mysql program

Source: Internet
Author: User

Preparatory work

1, install MySQL.

2. Install MySQL visualization tool navicat. (Because of my preference, use this visualizer for the time being).

3, IntelliJ install MySQL JDBC driver.

4. Add MySQL JDBC driver to GlassFish.

Install start MySQL

1, https://www.mysql.com/downloads/(although you can search a lot of download channels, but suggested in the official download, you understand).

2, according to the prompts to install the configuration. (This is not the point, not the clear self Google).

3, if the installation is not on that should be system permissions problems, the use of system administrator permissions to install it. (I encountered a permissions issue with the installation under WIN10).

4. Start the MySQL service.

5, add the test data. Database We name it a restful, plus a table product that contains 4 fields: ID, name, cover, price. Specific

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/318952/201702/318952-20170219152947722-386513125. PNG "style=" border:0px; "/>

A record is added to facilitate the test.

Installing Navicat

We use Navicat for visualization, and of course you can do data manipulation directly from the tool or command line provided by MySQL.

1, Https://www.navicat.com/download, as to whether to pay or crack to see yourself, you understand.

2, according to the prompt installation.

IntelliJ installing the MySQL JDBC driver

1. In IntelliJ, select View| in the main menu. Tool windows| Databases.

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/318952/201702/318952-20170219151630644-1217014447. PNG "style=" border:0px; "/>

2, the right pop-up database column, select the above "+", select datasource| MySQL, a dialog box for data Source and drive pops up.

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/318952/201702/318952-20170219151641113-363338631. PNG "style=" border:0px; "/>

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/318952/201702/318952-20170219151822504-1327060849. PNG "style=" border:0px; "/>

3, click on the top left "+", select MySQL. Fill in the fields to the right as prompted.

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/318952/201702/318952-20170219151832316-745128356. PNG "style=" border:0px; "/>

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

Add the MySQL JDBC driver to the GlassFish.

1, put the MySQL driver (Eg:mysql-connector-java-5.1.35-bin.jar) into the. \glassfish4\glassfish\lib (refer to your GlassFish's installation directory for details).

Coding

1. Join the MySQL driver jar package.

2. The directory structure is as follows:

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/318952/201702/318952-20170219151854925-1622704536. PNG "style=" border:0px; "/>

3. Introduction of directory structure. Bo is the entity class, DAO is the database related operation class (in order to facilitate the understanding of the process so that does not involve ORM and other things). Here's a look at the specifics of each class:

Boproduct.java

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Border:none; "/>

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

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Border:none; "/>

Dbconnection.java

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Border:none; "/>

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

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Border:none; "/>

Dbproductmanager.java

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Border:none; "/>

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 p s = 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;    }}

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Border:none; "/>

Helloworld.java

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Border:none; "/>

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 ();  & nbsp;      if  (null != allproduct && ! Allproduct.isempty ())  {            for  ( BOPRODUCT&NBSP;ITEM&NBSP;:&NBSP;ALLPRODUCT)  {                 stringbuilder.append (Item.tostring ()). Append ("\ n");             }        }         stringbuilder.append ("data end\n");         return stringbuilder.tostring ();     }}

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Border:none; "/>

MyApplication

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Border:none; "/>

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 [email protected] ("/")//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;     }}

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Border:none; "/>

Run the program

1. Click the Run button.

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/318952/201702/318952-20170219153226410-1794808645. PNG "style=" border:0px; "/>

2. At this point the IDE does something you do not need to have a relationship with (non-business): Compile and deploy to the server and launch the browser.

3. When you see the browser shown below, you are successful.

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/318952/201702/318952-20170219153526191-763700142. PNG "style=" border:0px; "/>


A restful+mysql program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.