Hello everyone! The point is the next.

Connecting to the database which is located on the host. Everything is ok, I don’t have any problems) Connection is as follows:
клиент <==>скрипт(Php)<==>Сервер
No problems, everything is OK! But I want to connect to the database through Java itself without using scripts, as far as possible? Methods (Methods), and whether it is a good or bad example (and why). I know the question can cause controversy, but if everyone will give a clue in the amount it will be knowledge! Thank you

    1 answer 1

    Nothing difficult in this, if you have access to the configuration of the host. The MySQL database server program must be configured to accept incoming connections by the host's IP address. The fact is that most often this function is disabled by the server administrator for security reasons.

    If the required setting is available, then you can connect directly. For example, the server "sample.com":

     import java.sql.*; class MysqlCon{ public static void main(String args[]){ try{ Class.forName("com.mysql.jdbc.Driver"); Connection con=DriverManager.getConnection( "jdbc:mysql://sample.com:3306/sonoo","root","root"); //database name, username and password Statement stmt=con.createStatement(); ResultSet rs=stmt.executeQuery("select * from emp"); while(rs.next()) System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3)); con.close(); } catch(Exception e) { System.out.println(e);} } } 
    • Thank you, is there any difference between such a connection and connection via a script? What is more reliable?) - elik
    • The safer is the simpler - the smaller the links in the chain, the less opportunities for failure. But the security issue plays an important role here - I would prefer through a script rather than opening a database port on a real IP address. And PHP scripts are quite smart and stable. Although, if you don’t use PHP anywhere on your server, then there’s probably no sense in keeping it for the sake of translation. In any case, the choice is yours. - bigov
    • yes I actually knew about JDBC for remote control - elik
    • But he decided to give up right away, when decompiling the project, he already leaves too many holes. In the hands of a skilled person, it’s even scary) my level doesn’t allow me to write safer for the sake yet) so I will work with the script) but many thanks) - elik