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.