Welcome, Guest. Please login or register.
Did you miss your activation email?
Pages: [1]   Go Down
  Print  
Author Topic: PHP and Java's communicating trough URL  (Read 2284 times)
0 Members and 1 Guest are viewing this topic.
jurka
Moderator
Member
*
*

Reputation: 5
Offline Offline
Posts: 188
Referrals: 0

Awards
« on: December 30, 2008, 05:05:35 PM »

In my current project i need to make database which can be used by Swing clients. So i didn't know any better solution then to use php $_GET variable for sending data from Java. If anyone has better ideas how to do this, then i am waiting for your post. I don't got any mysql table or any custom server, it all has to be free.

add.php
Code
GeSHi (php):
  1. <?php
  2.    define('EOL', "\n<br />");
  3.  
  4.    if (isset($_GET['name']) && isset($_GET['result'])
  5.          && isset($_GET['field'])) {
  6.  
  7.        $array = array($_GET['name'], $_GET['result'], $_GET['field']);
  8.        $fieldSizes = array(60, 60, 30);
  9.  
  10.        for ($i = 0 ; $i < count($array) ; $i++) {
  11.            if (strlen($array[$i]) < $fieldSizes[$i]) {
  12.                $remove = array('\n', '\r', '\t');
  13.                $return = trim(htmlspecialchars(str_replace($remove, "", $array[$i])));
  14.  
  15.                $array[$i] = $return;
  16.            } else {
  17.                exit();
  18.            }
  19.        }
  20.  
  21.        //$line = $array[0] ."•". $array[1] ."•". $array[2];
  22.        echo $line;
  23.        require_once('TextFunctions.php');
  24.        $t = new TextFunctions();
  25.        $t->write_file($line);
  26.    }
  27.  
  28.  
  29. ?>
  30.  
  31. //read.php
  32.  
  33. <?php
  34. require_once('TextFunctions.php');
  35.  
  36. $t = new TextFunctions();
  37. //$t->write_file("Kaiki");
  38. //        print_r($t->read_file());
  39. echo $t->read_file();
  40. //        echo("Koik on korras");
  41. ?>
  42.  
  43. //TextFunctions.php
  44. <?php
  45.    class TextFunctions
  46.    {
  47.        var $filename;
  48.  
  49.        function TextFunctions( $_filename = 'tekst.txt') {
  50.            $this->filename = $_filename;
  51.        }
  52.  
  53.        function read_file() {
  54.            if (is_readable($this->filename)) {
  55.                $fp = fopen($this->filename, 'r');
  56.                $array = array();
  57.  
  58.                while (!feof($fp)) {
  59.                    $array .= fread($fp, 1024 * 4);
  60.  
  61.                }
  62.                fclose($fp);
  63.                return $array;
  64.            } else {
  65.                die("File is not readable!");
  66.            }
  67.        }
  68.  
  69.        function write_file($line) {
  70.            if (is_writable($this->filename)) {
  71.                $fp = fopen($this->filename, 'a');
  72.  
  73.                if (flock($fp, LOCK_EX)) {
  74.                    fwrite($fp, $line ."\r\n", 1024 * 4);
  75.                    flock($fp, LOCK_UN);
  76.                    fclose($fp);
  77.                } else {
  78.                    die('Couldn\'t open or lock file!');
  79.                }
  80.            } else {
  81.                die('You cant write in this fail!');
  82.            }
  83.  
  84.        }
  85. }
  86.  
  87. ?>
  88.  
  89.  
Created by GeSHI 1.0.7.20

Code
GeSHi (java):
  1.  
  2. package applettesting;
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.net.MalformedURLException;
  8. import java.net.URL;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11.  
  12. /**
  13.  *
  14.  * @author Risto
  15.  */
  16. public class DataReader {
  17.  
  18.    public void makeConnection(String site) {
  19.        try {
  20.            URL url = new URL(site);
  21.            BufferedReader in;
  22.            String inputLine;
  23.  
  24.            in = new BufferedReader(new InputStreamReader(url.openStream()));
  25.  
  26.            while ((inputLine = in.readLine()) != null) {
  27.                System.out.println(inputLine);
  28.            }
  29.  
  30.            in.close();
  31.        } catch (MalformedURLException ex) {
  32.            Logger.getLogger(DataReader.class.getName()).log(Level.SEVERE, null, ex);
  33.        } catch (IOException ex) {
  34.                Logger.getLogger(DataReader.class.getName()).log(Level.SEVERE, null, ex);
  35.        }
  36.  
  37.    }
  38. }
  39.  
  40.  
Created by GeSHI 1.0.7.20

Usage in java
Code
GeSHi (java):
  1.        DataReader d = new DataReader();
  2.        d.makeConnection("http://localhost/PHPproj/web/read.php");
  3.  
  4.        //lets add another entry
  5.        d.makeConnection("http://localhost/PHPproj/web/add.php?name=Manni&field=programming&result=12121");
  6.  
  7.  
Created by GeSHI 1.0.7.20
Logged
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2593
Referrals: 13

WWW Awards
« Reply #1 on: December 30, 2008, 05:16:24 PM »

Uhm, what about using the JDBC to connect to a database? I'm no PHP coder but this doesn't seem right. You can use insert queries using:


Code
GeSHi (java):
  1. Connection connect = getConnection();
  2. Statement stmt = connect.createStatement();
  3. String sqlStatement = "Your sql statement here";
  4. stmt.executeUpdate(sqlStatement );
  5. ...
  6. stmt.close();
  7. con.close();
Created by GeSHI 1.0.7.20
« Last Edit: December 30, 2008, 05:20:38 PM by HappyFace » Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
jurka
Moderator
Member
*
*

Reputation: 5
Offline Offline
Posts: 188
Referrals: 0

Awards
« Reply #2 on: December 30, 2008, 05:28:42 PM »

yes but i cant use online any databases so i only got php safe mode to use.
Logged
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2593
Referrals: 13

WWW Awards
« Reply #3 on: December 30, 2008, 05:33:42 PM »

yes but i cant use online any databases so i only got php safe mode to use.

Well, how are you storing your data now? and what exactly do you want to do?
Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
jurka
Moderator
Member
*
*

Reputation: 5
Offline Offline
Posts: 188
Referrals: 0

Awards
« Reply #4 on: December 31, 2008, 04:35:39 AM »

Basically it's internet database which can be accessed anywhere, not elegant solution, but should work if you don't have much data. I am going to use this example for hiscore.

I have added a graph for explanation.
Logged
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2593
Referrals: 13

WWW Awards
« Reply #5 on: December 31, 2008, 12:10:53 PM »

Basically it's internet database which can be accessed anywhere, not elegant solution, but should work if you don't have much data. I am going to use this example for hiscore.

I have added a graph for explanation.

I don't think there is a direct way to write a textfile from a java client to a file on the http server without ftp access but i've looked around and found this article that might help you: http://www.devdaily.com/java/edu/pj/pj010023/ but then again they are doing (almost) the same as you are doing now but instead of letting the php file manage the writing they are using a cgi script.

But it's pretty interesting to try to find a better way to write it to the http server, ill look into it when i have the time and write a demo.

ps: Happy new year!
« Last Edit: December 31, 2008, 12:20:16 PM by HappyFace » Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
jurka
Moderator
Member
*
*

Reputation: 5
Offline Offline
Posts: 188
Referrals: 0

Awards
« Reply #6 on: January 01, 2009, 07:28:54 AM »

Basically it's internet database which can be accessed anywhere, not elegant solution, but should work if you don't have much data. I am going to use this example for hiscore.

I have added a graph for explanation.

I don't think there is a direct way to write a textfile from a java client to a file on the http server without ftp access but i've looked around and found this article that might help you: http://www.devdaily.com/java/edu/pj/pj010023/ but then again they are doing (almost) the same as you are doing now but instead of letting the php file manage the writing they are using a cgi script.

But it's pretty interesting to try to find a better way to write it to the http server, ill look into it when i have the time and write a demo.

ps: Happy new year!

Happy new year to all who reads this !

I haven't found any good free hosts who would support mysql, this would give much wider possibilities. Searching and returning result would be used much more effectively, but due to the Estonian host providers there is no mysql support. :(
Logged
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2593
Referrals: 13

WWW Awards
« Reply #7 on: January 06, 2009, 10:48:30 AM »

What about installing a webserver including php/mysql etc on your local machine? With xampp all you need  to do is to install it and u have your own webserver http://sourceforge.net/projects/xampp/
Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
jurka
Moderator
Member
*
*

Reputation: 5
Offline Offline
Posts: 188
Referrals: 0

Awards
« Reply #8 on: January 06, 2009, 11:31:52 AM »

What about installing a webserver including php/mysql etc on your local machine? With xampp all you need  to do is to install it and u have your own webserver http://sourceforge.net/projects/xampp/
Yeah this would be solution but i got problems with porting, because there is no open port so i have to pay for to get open ports.
Logged
priya_gurnani166
Junior Member
*

Reputation: 0
Offline Offline
Posts: 4
Referrals: 0

Awards
« Reply #9 on: January 15, 2009, 05:04:30 AM »

actualy i need help
i m making a software in java swing
i hv a problem in retriving values from database in JTable,
i m inserting values in database ms access it works,
following is my code one is for database another one for table
...
import java.sql.*;
public class JDta
{
public void putz(String x,String y)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:pproject","","");
PreparedStatement stmt=con.prepareStatement("insert into data values(?,?)");
stmt.setString(1,x);
stmt.setString(2,y);
stmt.executeUpdate();
stmt.close();
con.close();
}
catch(Exception er)
{
}
}

public boolean checking(String x,String y)
{
int coun=0;
boolean bb;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:exams","","");
PreparedStatement stmt=con.prepareStatement("select * from data where name=? and pass=?");
stmt.setString(1,x);
stmt.setString(2,y);
ResultSet rs=stmt.executeQuery();
while(rs.next())
{
coun++;
}
stmt.close();
con.close();
}
catch(Exception er)
{
}
if(coun>0)
bb=true;
else
bb=false;
return bb;
}
}
...
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Logger extends JFrame implements ActionListener
{
JButton jb1,jb2;
JLabel jl1,jl2,jl3;
JTextField jtf;
JPasswordField jp;
String x;
String y;
public Logger()
{
jl1=new JLabel("<html><center><font face=Maiandra GD size=5>Welcome to<p>School Management System</font></html>");
Container cont=getContentPane();
jl2=new JLabel("Enter Name");
jl3=new JLabel("Enter Password");
JPanel jpp=new JPanel();
jpp.setLayout(null);
jtf=new JTextField(10);
jp=new JPasswordField(10);
jpp.add(jl1);
jpp.add(jl2);
jpp.add(jl3);
jpp.add(jtf);
jpp.add(jp);
jb1=new JButton("Save");
jb1.addActionListener(this);
jpp.add(jb1);
jl1.setBounds(80,5,300,80);
jl2.setBounds(100,75,120,10);
jl3.setBounds(100,78,300,80);
jtf.setBounds(205,75,120,20);
jp.setBounds(205,105,120,20);
jb1.setBounds(205,135,70,29);
cont.add(jpp);
setTitle("Log In");
setSize(500,200);
setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
x=jtf.getText();
y=jp.getText();
JDta jxy=new JDta();
jxy.putting(x,y);
dispose();

}
public static void main(String aa[])
{
new Logger();
}
}
...
Logged
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2593
Referrals: 13

WWW Awards
« Reply #10 on: January 15, 2009, 09:22:15 AM »

actualy i need help
i m making a software in java swing
i hv a problem in retriving values from database in JTable,
i m inserting values in database ms access it works,
following is my code one is for database another one for table
...
code stripped
...

please make a new topic for your own question and don't make unrelevant posts in this topic. Thanks
Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
Pages: [1]   Go Up
  Print  
 
Jump to:  

Your Ad Here