[英]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. 我想创建一个Java应用程序,该应用程序将从MySQL接收数据并将其存储到列表中,但是对于开始存储到JTextArea来说会很好。
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脚本,当该脚本被激活时,它会从表“ orders”获取数据并以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. 当我在本地主机上对其进行测试时,它可以正常工作,但是我不知道如何从Java调用此php脚本,并以JSON格式格式化该文本并将其存储在Java的JTextArea中。
I don't know much about JSON. 我对JSON不太了解。 Sorry, noob here.
抱歉,这是菜鸟。
Java class: Java类:
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. 您需要做的几件事。
Invoke the php script from Java 从Java调用php脚本
To do this you'll need to know what the URL for the script is, this includes the protocol
, port
, host
and path
. 为此,您需要知道脚本的URL是什么,其中包括
protocol
, port
, host
和path
。 In other words something like: http://localhost:8000/orders.php
. 换句话说,例如:
http://localhost:8000/orders.php
。 Once you have the URL, you'll need to make an HTTP request to it. 拥有网址后,您需要向其发出HTTP请求 。
Parse the response into a JSON data structure. 将响应解析为JSON数据结构。
You can do this using an existing library like Jackson 您可以使用现有的库(例如Jackson)来执行此操作
Use the JSONObject
to populate your UI 使用
JSONObject
填充您的UI
Figure out how you want to display the data and call setText
on your JTextArea
弄清楚如何显示数据并在
JTextArea
上调用setText
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.