0

I want retrieve data using php from my mysql server. I did it in java code using JDBC(after connect to db, using getters and setters):

setMyRes(getMyStat().executeQuery("select * from " + tableName));
            while(!getMyRes().isBeforeFirst()){//this wait if database is null and check if is not null
                setMyRes(getMyStat().executeQuery("select * from " + tableName));
        }
            while (getMyRes().next()) {
                String key = getMyRes().getString(columnOneName);
                String value = getMyRes().getString(columnTwoName);
                map.put(key, value);
        }

I want do similar things using php script. I have conncetion.php that connect to my database and it works fine. And i have json_get_data.php

<?php
require "connection.php";
$table_name = $_POST["table_name"];

$sql = "select * from $table_name;";
$result = $connection->query($sql);
if($result){
    while($row = mysqli_fetch_array($result)){
        $response[] = $row;
    }
    echo json_encode($response);
}
else{
    echo "Error: " . $sql . "<br>" . $connection->error;
}

mysqli_close($connection);
?>

I want send table name from my java code and then get the data from this table. So i try in java :

URL urlConn = new URL(url);
         HttpURLConnection conn = (HttpURLConnection) urlConn.openConnection();
         conn.setRequestMethod("POST");
         conn.setDoOutput(true);
         conn.setDoInput(true);
         OutputStream outputStream = conn.getOutputStream();
         BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
        String data = URLEncoder.encode("table_name", "UTF-8") + "=" + URLEncoder.encode(tableName, "UTF-8");
         bufferedWriter.write(data);
         bufferedWriter.flush();
         bufferedWriter.close();
         outputStream.close();
        conn.setRequestMethod("GET");
        conn.setConnectTimeout(150000);
        conn.setDoInput(true);
        InputStream inputStream = new BufferedInputStream(conn.getInputStream());
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String line;
        StringBuffer jsonDataBuffer = new StringBuffer();
        while( (line = bufferedReader.readLine()) != null){
            jsonDataBuffer.append(line+"\n");
        }
        bufferedReader.close();
        inputStream.close();
        String jsonData = jsonDataBuffer.toString();

but i have exception : java.lang.IllegalStateException: connect in progress How can i do this using php and java?

grap
  • 45
  • 1
  • 9
  • Possible duplicate of [simplest way to read json from a URL in java](https://stackoverflow.com/questions/4308554/simplest-way-to-read-json-from-a-url-in-java) – Latheesan Oct 31 '17 at 00:41
  • I think that i have different problem since, i want send table name to php script and then get this table, and my main problem is first send table name and the same time read table – grap Oct 31 '17 at 08:25

0 Answers0