0

So, I'm trying to recreate an older PHP program of mine partially in Java however I've hit a roadblock in my code. No matter what I try to do, I can't seem to send information from my Java class to my PHP script. The PHP script is hosted on an online server, while my Java class is on a client machine. My code goes as following:

            String query = SearchTextBox.getText();

            Connection conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost/wc_wca?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC","root","pass" );
                String sql = "SELECT * FROM Persons WHERE id = ?";        
                PreparedStatement ps = conn.prepareStatement(sql);
                ps.setString(1, query);
                ResultSet rs = ps.executeQuery();
                while (rs.next())
                {
                    try{
                    URL url = new URL("http://worldcubers.com/setup/index.php");
                    URLConnection con = url.openConnection();
                    String name = rs.getString("name");
                    String id = rs.getString("id");
                    // activate the output
                    con.setDoOutput(true);
                    PrintStream printstream = new PrintStream(con.getOutputStream());
                    // send your parameters to your site
                    printstream.print("&name=" + name);
                    printstream.print("&id=" + id);

                    // we have to get the input stream in order to actually send the request
                    con.getInputStream();

                    // close the print stream
                    ps.close();
                    }           
                    catch (IOException ex) {
                                Logger.getLogger(MainUI.class.getName()).log(Level.SEVERE, null, ex);
                            }
                    initAndShowGUI();
                    this.dispose(); //to close the current jframe
                } 

In the PHP script, the lines that access the variables are the following:

$s = $_SESSION['name'];
$q = $_SESSION['id'];

I've tried many different articles here on StackOverflow, but to no avail, no matter what I try, I can't seem to send the name & id variables to the PHP script.

1 Answers1

0

I think its high time you to start to think of using JSON to fix the problem you are facing Also check here How to parse JSON in Java

I am using JSON to send variables back and forth for android app. I think the same can apply here.

Then think of using $_POST or $_GET but not $_SESSION

Jack Siro
  • 677
  • 10
  • 29
  • That helps with reading JSON, but how exactly would I write files from a client side Java app to a server hosted PHP script? Also, I only want to send 2 variables, is this the most optimal method for sending only 2 variables to the server? – William Siauw Mar 27 '19 at 02:52