简体   繁体   中英

How to get data from MySQL and store it to Java program using PHP script and JSON

I want to create Java application that will recieve data from MySQL and store it to the list, but for the beginning storing to the JTextArea would be fine.

I have database with tables on the localhost, and PHP script that when it's activated, gets data from table "orders" and returns data in JSON.

<?php
    /*
 * Following code will list all orders
 */

// array for JSON response
$response = array();

// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

// get all products from orders table
$result = mysql_query("SELECT* FROM orders") or die(mysql_error());

// check for empty result
if (mysql_num_rows($result) > 0) {
    // looping through all results
    // products node
    $response["orders"] = array();

    while ($row = mysql_fetch_array($result)) {
        // temp user array
        $order = array();
        $order["id"] = $row["id"];
        $order["tablle"] = $row["tablle"];
        $order["drink"] = $row["drink"];
        $order["created_at"] = $result["created_at"];

        // push single product into final response array
        array_push($response["orders"], $order);
    }
    // success
    $response["success"] = 1;

    // echoing JSON response
    echo json_encode($response);
} else {
    // no products found 
    $response["success"] = 0;
    $response["message"] = "No products found";

    // echo no users JSON
    echo json_encode($response);;
}
?>

When I test it on the localhost, it works fine, but I don't know how to call this php script from java and take that text formatted in JSON and store it in the JTextArea in Java.

I don't know much about JSON. Sorry, noob here.

Java class:

package newpackage;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Prozor extends JFrame implements ActionListener{

    JPanel panel = new JPanel();
    JPanel panel2 = new JPanel();
    JTextArea ta = new JTextArea();
    JButton dugme = new JButton("Get data");

    public Prozor(){
        this.setBounds(500, 200, 500, 500);
        this.setTitle("naslov");
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.getContentPane().add(panel, BorderLayout.NORTH);
        this.getContentPane().add(panel2, BorderLayout.SOUTH);

        panel.setLayout(new FlowLayout(FlowLayout.CENTER));
        panel2.setLayout(new FlowLayout(FlowLayout.CENTER));

        ta.setPreferredSize(new Dimension(450,400));
        panel.add(ta);
        panel2.add(dugme);

        dugme.addActionListener(this);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(dugme)) {

        }
    }

    public static void main(String[] args) {
        Prozor p = new Prozor();
        p.setVisible(true);
    }
}

A few things you'll have to do.

  1. Invoke the php script from Java

    To do this you'll need to know what the URL for the script is, this includes the protocol , port , host and path . In other words something like: http://localhost:8000/orders.php . Once you have the URL, you'll need to make an HTTP request to it.

  2. Parse the response into a JSON data structure.

    You can do this using an existing library like Jackson

  3. Use the JSONObject to populate your UI

    Figure out how you want to display the data and call setText on your JTextArea

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM