使用Java中的GET请求更新SQL

时间:2014-01-08 21:56:01

标签: java mysql sql get

我正在尝试更新java中的sql数据库,但它只适用于导航器。我正在使用GET请求,因为托管不允许我激活sql远程访问。

这是java函数:

public static void insertScore(String user, boolean win){
        URL url;
        HttpURLConnection connection=null; 
        try {
            url = new URL("http://mypage/insert.php?user="+user+"&win="+win);
            connection = (HttpURLConnection)url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
        } catch (MalformedURLException ex) {
            Logger.getLogger(SQL.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(SQL.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

和insert.php文件一样,这个文件没关系,但如果它可以帮到你:

<?php
if($_GET["win"]=='true'){
$sql="UPDATE Score SET  `Win` =  `Win` +1 WHERE  `User` = '".$_GET['user']."'";
}
else{ 
$sql="UPDATE Score SET  `Lose` =  `Lose` +1 WHERE  `User` = '".$_GET['user']."'";
}

$link = mysqli_connect('myhost','myuser','mypassword','mydb'); //everything ok here
if (!$link) {
    die('Error: ' . mysql_error());
}
$result=$link->query($sql);
?>

当我在safari http://mypage/insert.php?user=Steve&win=true中粘贴链接时,Sql数据库正确更新,但我不知道如何使用Java执行此操作,它不会更新数据库中的任何内容......

3 个答案:

答案 0 :(得分:0)

假设您正在使用DAO设计模式,使用此代码可以更新数据库中的行:

public class UserDao { 
// you need to call your Singleton Connection class:
  Connection con = MyConnection.getInstanceOf();

   // and then you write a method to update your object, here I'm updating a User Object:
    public void updateUser(User user) {
         try {
            String sql= "Update User  set username='"+user.getUserName()+"',score='"+user.getScore()+"' where userid='"+user.getUserId()+"'";


            Statement ste= con.createStatement();
            ste.executeUpdate(sql);

        } catch (SQLException ex) {
            Logger.getLogger(UserDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

答案 1 :(得分:0)

解决了,下载了一个库,一切正常 代码是:

public static void insertScore(String user, boolean win){
        final WebClient webClient = new WebClient();
        try {
            final HtmlPage page1 = webClient.getPage("http://mywebpage/insert.php?user="+user+"&win="+win);
        } catch (IOException | FailingHttpStatusCodeException ex) {
            Logger.getLogger(SQL.class.getName()).log(Level.SEVERE, null, ex);
        }
        webClient.closeAllWindows();
    }

官方网页:http://htmlunit.sourceforge.net/gettingStarted.html

答案 2 :(得分:0)

有了这个,我不是用坦克来捕猎蚂蚁(我想)

public static void insertScore(String user, boolean win){



    URI uri;
    try {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        uri = new URIBuilder()
                .setScheme("http")
                .setHost("mypage")
                .setPath("/insert.php")
                .setParameter("user", user)
                .setParameter("win", win+"")
                .build();
        System.out.println(uri);
        HttpGet httpget = new HttpGet(uri);
        CloseableHttpResponse response =httpclient.execute(httpget);
        response.close();
    } catch (URISyntaxException | IOException ex) {
        Logger.getLogger(SQL.class.getName()).log(Level.SEVERE, null, ex);
    }
}