1

This is my db class for my JavaSE application.

public class db {
    static Connection c;
    public static Connection Get_Connection() throws ClassNotFoundException,SQLException {
        if (c == null) {
            String ip = "";
            String port = "";
            String username = "";
            String password = "";
            try {
                BufferedReader br = new BufferedReader(new FileReader(System.getProperty("user.home") + "/ConnectionSettings.txt"));   
                ip = br.readLine();
                port = br.readLine();
                username = br.readLine();
                password = br.readLine();
                br.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            Class.forName("com.mysql.jdbc.Driver");
            c = DriverManager.getConnection("jdbc:mysql://" + ip + ":"+port + "/mydb", username, password);
        }
        return c;
    }

I know this isn't a good practice. I'm getting IP address, database password and port from a text file saved in C:/...

My question is, if I want to network the project is it okay to provide IP address of server machine in this db class?

If not tell me why, and how to network JavaSE applications?

It's okay if you provide answers in links.

What are the frameworks I should be using to network the applications?

Will hibernate be a solution for this?

asgs
  • 3,928
  • 6
  • 39
  • 54
  • what do you mean by "networking" here? Is it about co-ordinating the project code with other developers? – asgs Jul 02 '15 at 05:58
  • no i mean database should be in another pc – NullPointer Jul 02 '15 at 06:20
  • 1
    Best place to provide configuration is the app/web container. You could configure a Datasource (or the DB) and refer to it from your code using its JNDI. Check [why-use-jndi-for-data-sources](http://stackoverflow.com/questions/7760712/why-use-jndi-for-data-sources), [Looking Up the Data Source Using JNDI To Obtain a Connection](https://docs.oracle.com/cd/E19830-01/819-4725/abmdz/) and [jndi-resources-howto](http://tomcat.apache.org/tomcat-8.0-doc/jndi-resources-howto.html). – asgs Jul 02 '15 at 06:45
  • Usually it is preferred to use a hostname or even an alias to a server. The reason for this is that machines can be changed without you having to change your code. In big companies it is often difficult to replace an old server with a new one and keeping its ip. Hostnames and aliases can move from the old server to a potential new server easily – Marged Jul 02 '15 at 07:01

1 Answers1

0

If all you're talking about is making a network connection to an existing database server, this code will be adequate. In a real application, you'd want to use a more flexible method of storing the connection parameters, but this is what it boils down to.

Persistence frameworks like JPA (Hibernate) and Spring Data sit on top of this connection and convert between SQL and Java objects. Whether to use one depends on what exactly you're trying to do in your application.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152