【发布时间】:2019-03-26 03:28:10
【问题描述】:
所以,我正在尝试部分用 Java 重新创建我的旧 PHP 程序,但是我在代码中遇到了障碍。无论我尝试做什么,我似乎都无法将信息从我的 Java 类发送到我的 PHP 脚本。 PHP 脚本托管在在线服务器上,而我的 Java 类位于客户端计算机上。 我的代码如下:
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
}
在 PHP 脚本中,访问变量的行如下:
$s = $_SESSION['name'];
$q = $_SESSION['id'];
我在 StackOverflow 上尝试了许多不同的文章,但无济于事,无论我尝试什么,我似乎都无法将 name 和 id 变量发送到 PHP 脚本。
【问题讨论】:
-
ps.close();不会关闭打印流... -
那它有什么作用呢?
-
再看看告诉我。