1

now result of SQL query is inside System.out.println(),

public class MyMain{

public static void main(String[] args) throws Exception  {

    Class.forName("com.mysql.jdbc.Driver");

    Connection con = DriverManager.getConnection("jdbc:mysql://______8080/test-app","______","______");

    PreparedStatement statement = con.prepareStatement("SELECT * FROM users");

    ResultSet result = statement.executeQuery();

    while(result.next()){
        System.out.println(result.getString(1)+ " "+result.getString(2));
    }
}

but how send result to client? Like image or smth. I should use "return"+"FileOutputStream"?But it safety if is large data?
i can parce SQL result on the server side, and then send it in bytearray,but how send it inside http

UPD image

  • Android can't connect directly to a MySQL database.. -> https://stackoverflow.com/questions/15732853/how-to-connect-android-app-to-mysql-database – Raymond Nijland Apr 23 '18 at 17:13
  • Try modifying your code to fit to use Socket Programming. – Jamil Apr 23 '18 at 17:33
  • If you need to send the SQL result to HTTP. why not use servlet? https://stackoverflow.com/questions/7213541/what-is-java-servlet – yoav Apr 23 '18 at 18:19

1 Answers1

1

OK, you may have a bit of a confusion here. You show a Java program that connects to a database. What you need to have is:

  • A Java program (1) running on an Android device, that connects to...
  • A server program (2) that runs on a server, that connects to...
  • A database (3) (same or other server) that stores the data.

You need three things. Do you see it well?

Now the Java program (1) connects to (2) on some domain name/IP address and port using some kind of network protocol. Most likely it will be some type of HTTP or HTTPS, but can also be something like UDP or anything really.

The server program listens on the port for requests and connects to the database to retrieve data. Once retrieved, it respond over the same port.

The program you just wrote looks like a base for the server program (2). You need to add the network listener part. Then you can write your client program, that call this one (most likely over HTTP).

The Impaler
  • 30,269
  • 7
  • 28
  • 55
  • I go beyond the scope of my question, but I have some considerations on this topic and something else is not clear - that all clients connect to the server on a single port does not affect the operation of the server? Ie it does not matter how many devices are connected to the server on the same port? I also drew the alleged skeleton of my application. it's not embarrassing to add it to the production? – Олег Котенко Apr 23 '18 at 20:05
  • Connecting to the same port? No, by no means. Servers are really good at that. There are multiple strategies behind the scenes. At least **load balancers**, **round robin clusters**, **port redirectioning**, **non-blocking I/O ports**. Just to name a few. – The Impaler Apr 23 '18 at 20:13
  • so i can set server runs on 8080,and clients to connect to server on 8080? and I wanted to know your opinion about the client-server interaction architecture in the attached image – Олег Котенко Apr 23 '18 at 20:18
  • Yeah, that image looks good. In more detail, you'll need to figure out how to **serialize** (in the server) and **deserialize** (in the client) the data. The *wire* transfers only bits and bytes. That's why serialization is so useful to transform data structures into bytes (in the server) and bytes back into data structures (in the client), in order to actually use them. – The Impaler Apr 23 '18 at 20:21
  • Thank you very much! The data on the server is stored in a blob format, and the client is written to the file. but between serialization and deserialization of the use of io-streams is this a normal phenomenon? is it like a return in the usual method? this is what I was most worried about - what method / way to send data to the client – Олег Котенко Apr 23 '18 at 20:31