V
V
Vladimir Sachkov2019-01-16 12:07:23
MySQL
Vladimir Sachkov, 2019-01-16 12:07:23

Connecting to mysql in java?

It is impossible to connect in a Java console application to the database that lies on the PBX. Everything is on the same local network. I connect like this...

Connection conn = null;
    String url = "jdbc:mysql://IP:3306/";
    String dbName = "cdr";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "admin";
    String password = "password";
  try
                        {
                            Class.forName(driver).newInstance();
                            conn = DriverManager.getConnection(url+dbName,userName,password);
                            System.out.println("Connected to the database");
                            conn.close();
                            System.out.println("Disconnected from database");

                        }
                        catch (Exception e)
                        {                           
                            System.out.println("NO CONNECTION =(");
                        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
protven, 2019-01-16
@protven

What a horror...
1) Use some kind of connection pool, not this pornography with DriverManager. For example DBCP2, C3P0, Tomcat JDBC, HicaryCP.
2) Use maven to manage dependencies and build.
3) Build the fat jar using maven, this will save you (most likely) from not putting some kind of dependency in the classpath.
4) Read good books and man guides on java or look for examples on github by keywords, it helps a lot.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question