Results 1 to 2 of 2

Thread: MySql problem

  1. #1
    Joao Lourenco is offline Member
    Join Date
    Sep 2012
    Posts
    9
    Rep Power
    0

    Default MySql problem

    I have a page in php that gets data from a database add the values like:

    ID Name Points
    1 Joao 1
    2 Joao 10
    3 Joaox 1
    4 Joaoc 1


    Joao = 11
    Joaox = 1
    Joaoc = 1

    code:

    PHP Code:
    <?php
    error_reporting(0);
    include ( '../db_con.php' );
    $player = mysql_real_escape_string(addslashes($_GET['player']));
    
    if(strlen($player) < 2){
    	return;
    }
    
    $sql = 'SELECT player,sum(kills) as tkills ,sum(death) as tdeath,sum(time) as ttime, sum(points) as total_points , sum(position) as tpos, sum(position = 1) as wins, count(*) as tgames FROM '.$dbprefix.'playerstats WHERE player LIKE \'%'.$player.'%\' GROUP BY player ORDER BY total_points desc LIMIT 500';
    
    $result = mysql_query($sql);
    echo mysql_error();
    $index = ($p * $per_page)+1;
    echo '<center><table id="ldr_table"><tr class="lb_tb_hd" align="center"><td>Posicao</td><td>Jogador</td><td>Pontos</td><td>Vitorias</td><td>Posicao Media</td><td>Mortos</td><td>Vezes Morto</td><td>K/D</td><td>Jogos jogados</td><td>Tempo em jogo</td></tr>';
    while($row = mysql_fetch_array($result)){
    
    	
    
    	echo '<tr class="ldr_alt',($index%2) + 1,'"><td>',getRank($row['player']),'</td>';
    
    	echo '<td><a href="index.php?page=player&player=',$row['player'],'">',$row['player'],'</a></td>';
    	echo '<td>',$row['total_points'], '</td>';
    	echo '<td>',$row['wins'],'</td>';
    	echo '<td>',round($row['tpos'] / $row['tgames'], 2),'</td>';
    	echo '<td>',$row['tkills'],'</td>';
    	echo '<td>',$row['tdeath'],'</td>';
    	echo '<td>',round($row['tkills']/$row['tdeath'], 2),'</td>';
    	echo '<td>',$row['tgames'],'</td>';
    	$hours = round($row['ttime']/ (1000*60*60),0);
    	$min = round(($row['ttime']% (1000*60*60)) / (1000*60),0);
    	$sec = round((($row['ttime']% (1000*60*60)) % (1000*60)) / 1000,0);
    	
    
    	echo '<td>',$hours,':',$min,':',(($sec<10)?'0':''),$sec,'</td>';
    	echo '</tr>';
    
    	$index++;
    
    }
    echo '</table>';
    return;
    
    function getRank($rank_search){
    include ( '../db_con.php' );
    
    $sql_rank = 'SELECT player, sum(points) as total_points FROM '.$dbprefix.'playerstats GROUP BY player ORDER BY total_points desc';
    
    $result_rank = mysql_query($sql_rank);
    echo mysql_error();
    
    $rank = 1;
    $finished = false;
    $max = 5000;
    
    while($rank<=$max AND $row=mysql_fetch_array($result_rank)){
    	if($row['player'] == $rank_search){
    		$finished = true;
    		break;
    	}
    	$rank++;
    }
    return $rank;
    }
    
    ?>
    So I want to be abble to get the points and Rank how can I do it in java?


    Java
    Java Code:
    public String getRankandPoints(String player){		
    		String a = null;
    		long points = 0;
    		
    		try {
    			Class.forName("com.mysql.jdbc.Driver").newInstance();
    			con = DriverManager.getConnection("jdbc:mysql://localhost/Joaogl?user=root&password="); 
    			
    			stm = con.createStatement();						
    			
    			status = "Connection Opened";
    			System.out.println(status);
    
     
                            //funcion that I dont know how to do it....
    
    
                            con.close();
    			status = "Connection Close";
    			System.out.println(status);
    
    		} catch (SQLException e){
    			status = e.getMessage();
    		} catch (ClassNotFoundException e) {
    			status = e.getMessage();
    		} catch (Exception e) {
    			status = e.getMessage();
    		}
    		
    		return a;
    	}
    Last edited by Joao Lourenco; 12-20-2012 at 08:52 PM.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: MySql problem

    Did you mean you want to rewrite the PHP code using Java? In your case this would be how to execute a query in your mysql database and get the result. So it will be better to start from here: Trail: JDBC(TM) Database Access (The Java™ Tutorials)

Similar Threads

  1. URL problem in mysql connection in jsp
    By gb.rashu in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 11-19-2010, 07:23 AM
  2. MySQL problem
    By indra-uolles in forum New To Java
    Replies: 14
    Last Post: 08-07-2010, 08:10 AM
  3. Eclipse / Mysql problem
    By jdevm in forum Eclipse
    Replies: 0
    Last Post: 10-09-2008, 01:16 AM
  4. mysql driver problem
    By mokingsu in forum JDBC
    Replies: 4
    Last Post: 01-17-2008, 05:27 PM
  5. Mysql problem
    By Nick15 in forum JDBC
    Replies: 2
    Last Post: 05-15-2007, 05:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •